```python
import sympy as sp
import numpy as np
```


```python
def factorial(n):
    if n < 2:
        return 1
    else:
        return n*factorial(n-1)
```


```python
def n_degree_taylor(formula, x, x_0, n):
    '''
    Given the analytic expression, x and x_0, calculate
    its nth degree Taylor polynomial
    Note: Variable is defaulted to x
    '''
    var_x = sp.symbols('x')
    acc = 0
    # Be careful, n+1 because it's up to n
    for i in range(n+1):
        acc += sp.diff(formula, var_x, i).subs(var_x, x_0)/factorial(i)*(x-x_0)**i
    return float(acc)
```


```python
# Generic Test for n_degree_taylor
expected = -2
actual = n_degree_taylor('-sin(x)', 2, 0, 2)
tol = 10**-7
assert abs(expected-actual) < tol
```


```python
def empirical_taylor(derivatives, x, x_0):
    '''
    Given empirical derivatives f(x_0), f'(x_0), f''(x_0)...
    calculate it's taylor series with this empirical data
    ASSUME FROM 0th derivative (which is the original function)
    '''
    result = 0
    for i in range(len(derivatives)):
        result += derivatives[i]*(x-x_0)**i/factorial(i)
    return result
```


```python
# Generic Test Case for empirical_taylor
expected = -4/3
actual = empirical_taylor([8, -8, 4, -2, 2], 2, 0)
tol = 10**-7
assert abs(expected-actual) < tol
```


```python
# Workspace
n_degree_taylor('-sin(x)', 5, 0, 2)
```




    -5.0




```python

```

e50132dcb2884d28095c8045de416ac773fcfaf7
ipynb
Jupyter Notebook
taylor.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
2021-11-02T05:56:10.000Z
2022-03-03T19:25:19.000Z
taylor.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
taylor.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
2021-10-30T15:18:01.000Z
2021-12-10T11:26:43.000Z
Qwen/Qwen-72B
1. YES
2. YES