# Taylor Series for Approximations

Taylor series are commonly used in physics to approximate functions making them easier to handle specially when solving equations. In this notebook we give a visual example on how it works and the biases that it introduces.

## Theoretical Formula

Consider a function $f$ that is $n$ times differentiable in a point $a$. Then by Taylor's theorem, for any point $x$ in the domain of f, we have the Taylor expansion about the point $a$ is defined as:
\begin{equation}
f(x) = f(a) + \sum_{k=1}^n \frac{f^{k}(a)}{k!}(x-a)^k + o\left((x-a)^n\right) \quad,
\end{equation}
where $f^{(k)}$ is the derivative of order $k$ of $f$. Usually, we consider $a=0$ which gives:
\begin{equation}
f(x) = f(0) + \sum_{k=1}^n \frac{f^{k}(0)}{k!}(x)^k + o\left((x)^n\right) \quad.
\end{equation}

For example, the exponential, $e$ is infinitely differentiable with $e^{(k)}=e$ and $e^0=1$. This gives us the following Taylor expansion:
\begin{equation}
e(x) = 1 + \sum_{k=1}^\infty \frac{x^k}{k!} \quad.
\end{equation}

## Visualising Taylor Expansion Approximation and its Bias

Let us see visually how the Taylor expansion approximatees a given function. We start by defining our function below, for example we will consider the exponential function, $e$ again up to order 3.


```python
#### FOLDED CELL
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.display import Markdown as md
from sympy import Symbol, series, lambdify, latex
from sympy.functions import *
from ipywidgets import interactive_output
import ipywidgets as widgets
from sympy.parsing.sympy_parser import parse_expr
import numpy as np

x = Symbol('x')
```


```python
order = 3
func = exp(x)
```


```python
#### FOLDED CELL
taylor_exp = series(func,x,n=order+1)
approx = lambdify(x, sum(taylor_exp.args[:-1]), "numpy")
func_np = lambdify(x, func, "numpy")
latex_func = '$'+latex(func)+'$'
latex_taylor = '\\begin{equation} '+latex(taylor_exp)+' \end{equation}'
```

The Taylor expansion of  {{ latex_func }} is :
{{latex_taylor}}

Now let's plot the function and its expansion while considering a point, noted $p$, to study the biais that we introduce when we approximate the function by its expansion:


```python
#### FOLDED CELL
order = widgets.IntSlider(min=0,max=20,step=1,value=3,description='Order')
x_min = -4
x_max = 4
x1 = widgets.FloatSlider(min=x_min,max=x_max,value=3,step=0.2,description='Point Absciss')
func = widgets.Text('exp(x)',description='Function')
text_offset = np.array([-0.15,2.])


ui = widgets.HBox([x1, order, func])

def f(order=widgets.IntSlider(min=1,max=10,step=1,value=3)
      ,x1=1.5
      ,func='exp(x)'):
    func_sp = parse_expr(func)
    taylor_exp = series(func_sp,x,n=order+1)
    approx = lambdify(x, sum(taylor_exp.args[:-1]), "numpy")
    func_np = lambdify(x, func_sp, "numpy")
    n_points = 1000
    x_array = np.linspace(x_min,x_max,n_points)
    approx_array = np.array([approx(z) for z in x_array])
    func_array = np.array([func_np(z) for z in x_array])
    func_x1 = func_np(x1)
    approx_x1 = approx(x1)
    plt.figure(42,figsize=(10,10))
    plt.plot(x_array,approx_array,color='blue',label='Taylor Expansion')
    plt.plot(x_array,func_array,color='green',label=func)
    plt.plot(0,approx(0),color='black',marker='o')
    plt.annotate(r'(0,0)',[0,approx(0)],xytext=text_offset)
    plt.plot([x1,x1]
             ,[-np.max(np.abs([np.min(func_array),np.max(func_array)])),min(approx_x1, func_x1)]
             ,'--',color='black',marker='x')
    plt.plot([x1,x1],[approx_x1, func_x1],'r--',marker='x')
    plt.annotate(r'$p_{approx}$',[x1,approx(x1)],xytext=[x1,approx(x1)]-text_offset)
    plt.annotate(r'$p$',[x1,func_np(x1)],xytext=[x1,func_np(x1)]-text_offset)
    plt.xlim([x_min,x_max])
    plt.ylim(-np.max(np.abs([np.min(func_array),np.max(func_array)]))
             ,np.max(np.abs([np.min(func_array),np.max(func_array)])))
    plt.legend()
    plt.show()
    print('Approximation  bias : {}'.format(func_x1-approx_x1))
    return None
interactive_plot = widgets.interactive_output(f, {'order': order, 'x1': x1, 'func': func})
interactive_plot.layout.height = '650px'
display(interactive_plot, ui)
```


    Output(layout=Layout(height='650px'))



    HBox(children=(FloatSlider(value=3.0, description='Point Absciss', max=4.0, min=-4.0, step=0.2), IntSlider(val…


Notice that the further $p$ gets away from the point of the expansion (in that case $0$), the higher the approximation bias gets. Samely, the lower the order of approximation is, the higher the approximation bias gets.

d1e7f77a0398c1454062aa878fc5723994dd3e99
ipynb
Jupyter Notebook
taylor_series.ipynb
fadinammour/taylor_series
4deb11d51dcf23432c035486d997cfebd3ea7418
MIT
taylor_series.ipynb
fadinammour/taylor_series
4deb11d51dcf23432c035486d997cfebd3ea7418
MIT
taylor_series.ipynb
fadinammour/taylor_series
4deb11d51dcf23432c035486d997cfebd3ea7418
MIT
Qwen/Qwen-72B
1. YES
2. YES