{ "cells": [ { "cell_type": "markdown", "id": "f23e223b-2cd6-4cc2-9a5b-f977b8afce01", "metadata": {}, "source": [ "MCS 320 Project Two, due 19 July 2024, at 2pm." ] }, { "cell_type": "markdown", "id": "126e03ec-e97a-49c5-91a4-9b7b9efbaec2", "metadata": {}, "source": [ "# Taylor Series and Derivatives" ] }, { "cell_type": "markdown", "id": "fbb81bb2-9b3c-42ce-b8fb-38a6c245b17b", "metadata": {}, "source": [ " Computing symbolic derivatives may lead to expression swell,\n", "when many higher order derivatives are required. \n", "The $k$-th coefficient of a Taylor series expansion of a function\n", "$f(x)$ at $x=a$ equals\n", "$$\n", " \\frac{f^{(k)}(a)}{k!}\n", "$$\n", "which allows to recover the $k$-th derivative of $f(x)$ at $x=a$." ] }, { "cell_type": "markdown", "id": "db40f7a0-4c6f-44e4-b032-a008eaa29c88", "metadata": {}, "source": [ "In this project, we use SageMath to explore the connection between\n", "derivatives, Taylor series, and the avoidance of expression swell\n", "via the application of Newton's method." ] }, { "cell_type": "markdown", "id": "0832c69f-56fc-4ea8-af50-6fe3b94b9010", "metadata": {}, "source": [ "The output of" ] }, { "cell_type": "code", "execution_count": 1, "id": "8b26b3ed-50a7-4632-b78a-8febe7efb127", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1/3628800*x^10 + 1/40320*x^8 - 1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "taylor(cos(x), x, 0, 10)" ] }, { "cell_type": "markdown", "id": "672c3514-a888-41e6-b3bd-254f465b0f5f", "metadata": {}, "source": [ "is identical to" ] }, { "cell_type": "code", "execution_count": 2, "id": "9d71bbb9-134d-4981-aebf-a304cf29f96d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1/3628800*x^10 + 1/40320*x^8 - 1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum([diff(cos(x), x, k).subs(x=0)/factorial(k)*x^k for k in range(11)])" ] }, { "cell_type": "markdown", "id": "1de46a37-6aa7-4508-937d-d72b70011333", "metadata": {}, "source": [ "which shows the first 10 terms of the Taylor series expansion\n", "of $\\cos(x)$ at $x = 0$." ] }, { "cell_type": "markdown", "id": "4a89a376-d518-4f6e-84a6-b99429868fa3", "metadata": {}, "source": [ "## 1. Expression Swell" ] }, { "cell_type": "markdown", "id": "c5edbcbb-981f-48c1-82fe-d8ab047eb08e", "metadata": {}, "source": [ "The straighforward way to symbolically compute a Taylor series expansion\n", "would thus be first compute all symbolic derivatives and then to evaluate \n", "all symbolic derivatives at the same point.\n", "This can lead to expression swell, as we explore in the first assignment." ] }, { "cell_type": "markdown", "id": "a8f4ee56-e56c-4d0a-851d-0b61d0fcd5b3", "metadata": {}, "source": [ "## Assignment One" ] }, { "cell_type": "markdown", "id": "25978664-ee67-4060-a808-1a9cbb2965c0", "metadata": {}, "source": [ "Consider\n", "$$\n", " f(x) = \\sqrt{\\frac{x+1}{x-1}}.\n", "$$\n", "\n", "1. Compute the first 15 derivatives of $f(x)$.\n", "\n", " Describe your observations about the size of the 15-th derivative.\n", "\n", "2. Evaluate the 15-derivative at $x=0$ and compare with the coefficient\n", " of $t^{15}$ in the Taylor expansion of $f(x)$ at~$x = 0$.\n", "\n", "\n", " Demonstrate the recovery of $f^{(15)}(0)$ from that coefficient." ] }, { "cell_type": "markdown", "id": "8061d226-1ea5-4948-a716-10b14f7e812d", "metadata": {}, "source": [ "## 2. Newton's Method" ] }, { "cell_type": "markdown", "id": "5e3c284e-053c-4ca7-a665-6bd27d6a76aa", "metadata": {}, "source": [ "To compute the Taylor series of $\\sqrt{1+t}$, we consider the equation\n", "$$\n", " z^2 - (1 + t) = 0\n", "$$\n", "where $t$ is the variable in a power series ring with rational\n", "coefficients, of precision 16, so the truncation error is $O(t^{16})$,\n", "declared as `t` below" ] }, { "cell_type": "code", "execution_count": 3, "id": "c65f0391-21ca-4de8-95c8-34ba19d6ebe7", "metadata": {}, "outputs": [], "source": [ "R. = PowerSeriesRing(QQ, default_prec=16)\n", "P. = R[]" ] }, { "cell_type": "markdown", "id": "915fb47c-bd2e-4904-910e-beb176c46fc9", "metadata": {}, "source": [ "where the second command above declares $z$ as `z`,\n", "as the variable in a polynomial where the coefficients\n", "are power series of $O(t^{16})$ with rational coefficients." ] }, { "cell_type": "markdown", "id": "f8b45204-02fd-4c63-82cc-5652d7de51c6", "metadata": {}, "source": [ "Then we can apply Newton's method on the above equation" ] }, { "cell_type": "code", "execution_count": 4, "id": "2064fe91-b414-49fc-894b-fe1e725f3437", "metadata": {}, "outputs": [], "source": [ "equ = z^2 - (1+t)\n", "newton_step = z - equ/diff(equ, z)" ] }, { "cell_type": "markdown", "id": "84591c03-b3a1-4eff-a4cb-228e4cd4666f", "metadata": {}, "source": [ "by repeated evaluation of the expression `newton_step`\n", "starting at the series `1 + t` as follows:" ] }, { "cell_type": "code", "execution_count": 5, "id": "ec564348-a749-4407-a1d6-65000f466d7a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1 + t,\n", " 1 + 1/2*t + O(t^16),\n", " 1 + 1/2*t - 1/8*t^2 + 1/16*t^3 - 1/32*t^4 + 1/64*t^5 - 1/128*t^6 + 1/256*t^7 - 1/512*t^8 + 1/1024*t^9 - 1/2048*t^10 + 1/4096*t^11 - 1/8192*t^12 + 1/16384*t^13 - 1/32768*t^14 + 1/65536*t^15 + O(t^16),\n", " 1 + 1/2*t - 1/8*t^2 + 1/16*t^3 - 5/128*t^4 + 7/256*t^5 - 21/1024*t^6 + 33/2048*t^7 - 107/8192*t^8 + 177/16384*t^9 - 593/65536*t^10 + 1001/131072*t^11 - 3395/524288*t^12 + 5773/1048576*t^13 - 19665/4194304*t^14 + 33525/8388608*t^15 + O(t^16)]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sL = [1 + t]\n", "for i in range(3):\n", " sL.append(newton_step(sL[-1]))\n", "sL" ] }, { "cell_type": "markdown", "id": "4a4dbf5f-3473-4c06-a006-59e6cf8d134b", "metadata": {}, "source": [ "Because Newton's method converges quadratically, three steps suffice." ] }, { "cell_type": "markdown", "id": "6e0a760d-fcad-4364-950b-0b7b2b7ad573", "metadata": {}, "source": [ "## Assignment Two" ] }, { "cell_type": "markdown", "id": "646d0168-b4b0-403a-a8fa-fad551bdbc8e", "metadata": {}, "source": [ "Consider again $f(x)$ from assignment one.\n", "\n", "1. Apply Newton's method to compute all first 15 derivatives at $x=0$\n", " via the Taylor series. \n", "\n", " Verify the correctness by comparing to the output of `taylor`.\n", "\n", "2. How many steps are needed. Describe the convergence." ] }, { "cell_type": "markdown", "id": "cc6eeb70-dbaa-4be3-aab9-4ffead94af83", "metadata": {}, "source": [ "## 3. Convergence Radius" ] }, { "cell_type": "markdown", "id": "8bf9be88-9e46-4019-8006-5ca6bc021b0e", "metadata": {}, "source": [ "Truncated Taylor series can be applied to approximate functions.\n", "We may expect the Taylor series of a function $f(x)$ about $x = 0$\n", "to be accurate for small values of the parameter $t$.\n", "Taylor series are Pade approximants, which are often much more accurate\n", "for larger values of the parameter $t$." ] }, { "cell_type": "markdown", "id": "69a42e87-cd0a-4b86-8634-2fe0764b1c02", "metadata": {}, "source": [ "## Assignment Three." ] }, { "cell_type": "markdown", "id": "45ede629-9905-4035-bbd4-dee60fecee5a", "metadata": {}, "source": [ "Consider once more $f(x)$ from assignment one.\n", "\n", "1. Use the computed Taylor series to construct Pade approximants \n", " with denominators and numerators of increasing degrees.\n", "\n", " Compare the accurate of the Pade approximant evaluated at $t = 1/2$\n", " to the value of $f(1/2)$, \\newline for increasing degrees.\n", "\n", "2. Use the computed Taylor series to construct Pade approximants \n", " with a linear denominator and a numerator of increasing degree.\n", "\n", " What do you observe about the root of the denominator \n", " of the Pade approximants?\n", "\n", " Compare the root with the expression for $f(x)$." ] }, { "cell_type": "markdown", "id": "7c899e37-11b2-44e0-9e34-958b3a4a5ec0", "metadata": {}, "source": [ "## 4. The Deadline is Friday 19 July, at 2pm." ] }, { "cell_type": "markdown", "id": "f21cd626-2b4f-4993-b2e5-9e3c65acb56e", "metadata": {}, "source": [ "Upload your answer to gradescope at the latest on Friday 19 July,\n", "before 2pm." ] }, { "cell_type": "markdown", "id": "d23bcacd-4302-4e7c-8ca4-4b3b7c418d03", "metadata": {}, "source": [ "The solution consists of one single notebook,\n", "organized according to the assignments.\n", "Mark the start of each solution to an assignment using a heading in a cell.\n", "Your notebook should run from top to bottom as a program without errors.\n", "Apply proper formatting in your notebook so it reads\n", "like a technical report if you would print it.\n", "Document the execution cells with complete sentences,\n", "properly formatted in markdown cells." ] }, { "cell_type": "markdown", "id": "2fb7970c-db7f-4768-af94-f34baf93ec6d", "metadata": {}, "source": [ "You may (not must) work in pairs for this project. \n", "A pair consists of two, not three or more.\n", "If you decide to work in a pair,\n", "then you must send me an email with the name of your partner and\n", "with the email address of your partner in the copy of the email,\n", "before 2pm on Monday 15 July.\n", "If working in a pair, then only one Jupyter notebook should be submitted." ] }, { "cell_type": "markdown", "id": "ec281265-d8ec-44e2-accd-da9eee0e3c2f", "metadata": {}, "source": [ "If you have questions, concerns, or difficulties,\n", "feel free to contact me for help." ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 10.3", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }