<a href="https://colab.research.google.com/github/stephenbeckr/numerical-analysis-class/blob/master/Demos/Ch1_SymbolicTaylorSeries.ipynb" target="_parent"></a>

Manipulate Taylor series using the **SymPy** package in Python, following the tutorial at [scipy-lectures.org/packages/sympy.html](https://scipy-lectures.org/packages/sympy.html).  SymPy is used in [SageMath](https://www.sagemath.org/).

You can do this in Maple, which is a bit old-school, or Mathematica (which we have a license for at CU).  Wolfram Alpha is another choice (by the company that makes Mathematica) though the free version does have some limitations.

The nice thing about Python is that it is free and community supported, so if you take the time to use it, it won't go away. If you learn Mathematica now, you may not be able to use it in your job, since it's an expensive license.


```
# Load the package
import sympy as sym
from sympy import init_printing
init_printing()  # This will make output look very nice!

import math  # has things like math.pi (which is the numerical value of pi)
```


```
# First, all symbolic variables must be declared. This is in contrast to Mathematica
x = sym.Symbol('x')

# Now, let's ask for a Taylor series expansion
sym.series( sym.cos(x), x )
```


```
# We can see the Docstring for the function to see all the options
?sym.series
```

Before going further, let's play with cos(pi/4)


```
print( math.cos( math.pi/4 ) )
# or, ask to print out 20 decimal places
print( '%.20f' % math.cos( math.pi/4 ) )  # this is old-school: see https://docs.python.org/3/tutorial/inputoutput.html#old-string-formatting
print( math.cos( sym.pi/4) )
print( sym.cos( math.pi/4) )
print( sym.cos( sym.pi/4) )
sym.cos( sym.pi/4)  # same as above, but use fancy formatting
```

Now let's explore the series. In particular, let's try to evaluate cos( .75 ), using only addition and multiplication, and a few "memorized" values of sine/cosine (let's assume that we memorize the fact that $\cos( \pi/4 ) = \sqrt{2}/2$ and that $\sin( \pi/4 ) = \sqrt{2}/2$.

Since .75 is not that far from $\pi/4 \approx 0.785$, let's do a Taylor expansion of $\cos$ around $\pi/4$.


```
Taylor = sym.series( sym.cos(x), x, x0=sym.pi/4, n=5)
Taylor
```


```
Taylor.subs(x,.75) # Not what we want
```


```
Taylor.evalf( subs={x: .75})
```


```
# Numerically evaluating this was a problem, because of the O( x^5 ) term. Let's remove it.
# See https://docs.sympy.org/latest/tutorial/calculus.html
Taylor.removeO()
```


```
guess = Taylor.removeO().subs(x,.75).evalf()
print("Our guess is: \t%.15f" % guess)
print("True value is: \t%.15f" % math.cos( .75) )
print("Discrepency is: \t%.3e" % abs( guess - math.cos(.75) ))
```

    Our guess is: 	0.731688868548266
    True value is: 	0.731688868873821
    Discrepency is: 	3.256e-10


Our *absolute* error is about $3\times 10^{-10}$. Is this what we'd expect? Let's use math to be able to **guarantee** a small error (without having known the true answer first)

Let's look at the remainder term in the Taylor series


```
x0=sym.Symbol('x0')
sym.series( sym.cos(x), x, x0=x0, n=5)
```


```
xi = sym.Symbol('xi')
Remainder = -(x-x0)**5*sym.sin(xi)/120
Remainder
```

By Taylor's remainder theorem, we know $\xi \in [x,x_0]$ (or $\xi \in [x_0,x]$ if $x_0 < x$) such that the above remainder is the error in the Taylor series. We don't know exactly what $\xi$ is, but that's OK, because for any $\xi$, we can just bound $|\sin(\xi)| \le 1$.  So using this,...


```
RemainderBound = -(x-x0)**5/120
RemainderBound
```


```
# See https://docs.sympy.org/latest/tutorial/basic_operations.html
RemainderBound.subs( [ (x,.75), (x0,math.pi/4) ])
```

To summarize, our Taylor series approximation had error $3.25 \times 10^{-10}$.  We were able to use math to give an *a priori* bound that the error was no more than $4.63 \times 10^{-10}$ (though in that bound, we disregarded potentially cancellation errors when summing the series)

2303a5ea0a5bb1415fda43644cd015ba551ea014
ipynb
Jupyter Notebook
Demos/Ch1_SymbolicTaylorSeries.ipynb
skhadem/numerical-analysis-class
a022fc2e73254800a1e193f94280446223ef01ae
BSD-3-Clause
2020-08-25T19:11:46.000Z
2021-11-12T21:49:44.000Z
Demos/Ch1_SymbolicTaylorSeries.ipynb
sabrinazhengliu/numerical-analysis-class
1585b68d3f3c375259507bf18f03ba0332851edb
BSD-3-Clause
2020-09-01T21:44:12.000Z
2020-09-01T21:44:12.000Z
Demos/Ch1_SymbolicTaylorSeries.ipynb
sabrinazhengliu/numerical-analysis-class
1585b68d3f3c375259507bf18f03ba0332851edb
BSD-3-Clause
2020-08-25T21:25:17.000Z
2021-03-03T04:14:26.000Z
Qwen/Qwen-72B
1. YES
2. YES