## Basic sympy setting


```python
%reset -f             
from sympy import *    # import everything from sympy module
init_printing()        # for nice math output 

## forcing plots inside browser
%matplotlib inline
```

###  Declare symbolic variables


```python
x = symbols('x')
```

### Example 1

Find 4-th degree Taylor polynamial of $f(x) = \sin(x)$ at $a = \frac{\pi}{2}$. Plot $f(x)$ and Taylor polynomial on the same window



```python
## Finding Taylor polynomial

a = pi/2
f = sin(x)

T = f.subs(x,a) \
    + diff(f,x,1).subs(x,a)*(x-a)  \
    + diff(f,x,2).subs(x,a)*(x-a)**2/2 \
    + diff(f,x,3).subs(x,a)*(x-a)**3/factorial(3) \
    + diff(f,x,4).subs(x,a)*(x-a)**4/factorial(4)
  
T
  
```


```python
## plotting f(x) and T(x) in same window


p = plot(f,T, (x,a - 4, a + 4),show=False, legend=True)

p[0].line_color = 'blue'
p[1].line_color = 'green'

p[0].label      = 'f(x)'
p[1].label      = 'T(x)'

p.show()
```

### Example 2

Find 20-th degree Taylor polynamial of $f(x) = \log(x)$ at $a = 1$. Plot $f(x)$ and Taylor polynomial on the same window



```python
## evaluation of T

a = 1
n = 20
f = log(x)

T = f.subs(x,a)

for k in range(1,n+1):
    df = diff(f,x,k)
    T = T + df.subs(x,a)*(x-a)**k/factorial(k)
    
T
```


```python
## plotting f(x) and T(x) in same window


p = plot(f,T, (x, 0.5, 2),show=False, legend=True)

p[0].line_color = 'blue'
p[1].line_color = 'green'

p[0].label      = 'f(x)'
p[1].label      = 'T(x)'

p.show()
```


```python

```

ab140b59006d559ef67f9f3ad0a42fe244a25cc8
ipynb
Jupyter Notebook
sympy/TaylorPolynomial.ipynb
krajit/krajit.github.io
221c8bcdf0612b3ae28c827809aa309ea6a7b0c2
MIT
2018-09-29T07:40:07.000Z
2022-02-28T22:17:04.000Z
sympy/TaylorPolynomial.ipynb
krajit/krajit.github.io
221c8bcdf0612b3ae28c827809aa309ea6a7b0c2
MIT
2019-08-26T08:42:47.000Z
2019-08-26T09:48:21.000Z
sympy/TaylorPolynomial.ipynb
krajit/krajit.github.io
221c8bcdf0612b3ae28c827809aa309ea6a7b0c2
MIT
2017-09-09T23:32:09.000Z
2020-01-28T21:11:39.000Z
Qwen/Qwen-72B
1. YES
2. YES