# INF-510, v0.22, Claudio Torres, ctorres@inf.utfsm.cl. DI-UTFSM
## Textbook: Lloyd N. Trefethen, Spectral Methods in MATLAB, SIAM, Philadelphia, 2000

# Differentiation by Interpolation


```python
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import scipy.sparse.linalg as sp
import sympy as sym
from scipy.linalg import toeplitz
import ipywidgets as widgets
import matplotlib as mpl
mpl.rcParams['font.size'] = 14
mpl.rcParams['axes.labelsize'] = 20
mpl.rcParams['xtick.labelsize'] = 14
mpl.rcParams['ytick.labelsize'] = 14
sym.init_printing()
```

Defining symbolic variables


```python
xj, h, x = sym.symbols('xj h x', reals=True)
u1, u2, u3, u4, u5 = sym.symbols('u1 u2 u3 u4 u5', reals=True)
```


## Do you remember Lagrange interpolation? Please do


```python
# The little Lagrange polynomials
def l_Lagrange(X,x,i):
    l=1
    n=len(X)
    for k in np.arange(i):
        l*=(x-X[k])
    for k in np.arange(i+1,n):
        l*=(x-X[k])
    return l
# The Lagrange polynomials
def L_Lagrange(X,x,i):
    num = l_Lagrange(X,x,i)
    den = l_Lagrange(X,X[i],i)
    L=num/den
    return L
# The Lagrange interpolation
# X : x - data points
# x : the symbolic variable
# Y : y - data points
def P_Lagrange(X,x,Y):
    P=0
    n=len(X)
    for i in np.arange(n):
        P+=Y[i]*L_Lagrange(X,x,i)
    return P
```

## Just a small example of a interpolation of 2 points


```python
# x - data points
X=(0,1,2,3,4,5,6)
# y - data points
Y=(0,1,-2,1,0,2,7)
# The interpolation
P=P_Lagrange(X,x,Y)
# Just showing the polynomial. Do you see the Lagrange polynomials?
P
```


```python
# And a simplified version of the polinomial!
sym.simplify(P)
```

Plotting what we got


```python
# We need to plot in a finer grid
xx=np.linspace(min(X),max(X),1000)
# Here we create a lambda function for our interpolation
Pl=sym.lambdify(x,P)
# The next line vectorize the lambda function, it is not need here but it can be used
# Plv=np.vectorize(Pl) 
plt.figure()
# Plotting the interpolated data
plt.plot(xx,Pl(xx),label='$P(x)$')
# Plotting the data points
plt.plot(np.array(X),np.array(Y),'k.',markersize=20,label='Data points')
plt.grid(True)
plt.ylim([np.min(Pl(xx))-1,np.max(Pl(xx))+1])
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend(loc='best')
plt.show()
```

## Let's find the 4th order differentiation approximation


```python
# x - data points. Notice that they are all symbolic variables!
X=(xj-2*h,xj-h,xj,xj+h,xj+2*h)
# y - data points. Also symbolic variables!
Y=(u1,u2,u3,u4,u5)
## In-class
#X=(xj-h,xj,xj+h)
#Y=(u1,u2,u3)
# The Lagrange interpolation still works!
P=P_Lagrange(X,x,Y)
P
```


```python
# Here we compute the derivative of the polynomian P(x), i.e. we obtain P'(x) and we definy it as Pp (P-prime).
Pp=sym.diff(P,x)
Pp
```


```python
# Now we evaluate P'(x) at x=x_j.
PpE=Pp.subs({x:xj})
PpE
```

# Class work

## How to you find a 6th order approximation? Please do it


```python

```

## How do you find a second order approximation on a left boundary? i.e. say you have $u_1(x_1)$, $u_2(x_2)$ and $u_3(x_3)$, interpolate them and compute the derivative at $x_1$.


```python

```

aa99c4407caf624bedd04d6bb57fb23813a9758a
ipynb
Jupyter Notebook
SC5/01 Differentiation by Interpolation.ipynb
maxaubel/Scientific-Computing
57a04b5d3e3f7be2fe9b06127f7e569659698656
BSD-3-Clause
2017-06-05T21:01:15.000Z
2022-03-17T12:51:55.000Z
SC5/01 Differentiation by Interpolation.ipynb
maxaubel/Scientific-Computing
57a04b5d3e3f7be2fe9b06127f7e569659698656
BSD-3-Clause
SC5/01 Differentiation by Interpolation.ipynb
maxaubel/Scientific-Computing
57a04b5d3e3f7be2fe9b06127f7e569659698656
BSD-3-Clause
2017-10-02T21:21:30.000Z
2022-03-23T02:23:22.000Z
Qwen/Qwen-72B
1. YES
2. YES