{ "cells": [ { "cell_type": "markdown", "id": "213addcc-15bb-447d-8ab2-f8d6218d9482", "metadata": {}, "source": [ "# Understanding Taylor models\n", "(by Sean Cowan)\n", "\n", "Analogously to the arithmetic operators of gduals, the interface with Taylor models in pyaudi is similar, if not identical." ] }, { "cell_type": "markdown", "id": "858713f3-d65f-439c-8b3f-8fc4c132d0a7", "metadata": {}, "source": [ "## Importing stuff" ] }, { "cell_type": "code", "execution_count": 7, "id": "8b879243-90e2-423a-ad3d-0cd5d21b603a", "metadata": {}, "outputs": [], "source": [ "from pyaudi import gdual_double as gdual, taylor_model, int_d" ] }, { "attachments": {}, "cell_type": "markdown", "id": "179c2669-7762-47f8-ad7c-8eaf935b5e7b", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Defining function\n", "\n", "We will try to approximate a function — shown below, as one could also do with gduals, and show how Taylor models differ from gduals.\n", "\n", "$$\n", "f(x) = \\frac{1}{x} + x\n", "$$\n", "\n", "Now, to approximate a function using Taylor expansions, we need to define the expansion order and expansion point:" ] }, { "cell_type": "markdown", "id": "f10c1e6f-8740-43db-880f-4fe98127236a", "metadata": {}, "source": [ "## Taylor polynomial (with gduals)" ] }, { "cell_type": "code", "execution_count": 25, "id": "584da1af-f21c-43b4-b86a-46c08b675ab8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.5+0.125*dx**2+0.03125*dx**4+0.75*dx-0.0625*dx**3\n" ] } ], "source": [ "order = 4\n", "exp_points = {\"x\": 2.0}\n", "\n", "x = gdual(exp_points[\"x\"], \"x\", order)\n", "fx = 1 / x + x\n", "print(fx)" ] }, { "cell_type": "markdown", "id": "049504ba-c789-4068-acd9-48ede4bec944", "metadata": {}, "source": [ "## Taylor model\n", "The result is the expected fourth order Taylor polynomial of function $f(x)$. If however, we would like to do the same computation with Taylor models by encompassing the higher order terms into a remainder interval, we also need to define the domain of validity and initial remainder bound. The same steps are performed, yet now there is a non-zero remainder bound." ] }, { "cell_type": "code", "execution_count": 27, "id": "76b7ca68-4c33-46d8-9813-13c4b4ede6c0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Taylor Polynomial:\n", "2.5+0.125*dx**2+0.03125*dx**4+0.75*dx-0.0625*dx**3\n", "Remainder Bound: [-2.12558e-07, 2.12558e-07]\n", "Expansion Point: {x: 2}\n", "Domain: {x:[1.9, 2.1]}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rem_bound = int_d(0.0, 0.0)\n", "domain = {\"x\": int_d(1.9, 2.1)}\n", "\n", "x = gdual(exp_points[\"x\"], \"x\", order)\n", "tm_x = taylor_model(x, rem_bound, exp_points, domain)\n", "\n", "tm_fx = 1 / tm_x + tm_x\n", "tm_fx" ] }, { "cell_type": "markdown", "id": "09648bf9-297d-4175-b016-ce3230fa70eb", "metadata": {}, "source": [ "This remainder bound means that, given a Taylor polynomial valid within the domain of $[1.9, 2.1]$, one would need to add the lower and upper bound as a constant to that Taylor polynomial to ensure that, whatever $x$ value is chosen, the function value of $f(x)$ will be within that range. This differs fundamentally from the information that the gdual provides, since it will provide no mathematical guarantee on the distance from the actual function." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }