Repetition¶
Firstly we will solve some examples with Lagrange interpolating polynomial
In [3]:
from IPython.lib.display import YouTubeVideo
vid = YouTubeVideo("7LhmLrJNVJg")
display(vid)
Newton interpolating polynomial¶
In [4]:
vid = YouTubeVideo("ziQTh7I6NfE")
display(vid)
Let us now look at how to implement an interpolation polynomial in Newton's form. The following code contains a function that evaluates a polynomial at given points, and calculates the coefficients of the interpolation polynomial in Newton's basis.
In [50]:
import numpy as np
from numpy import array,arange
from math import pi,cos
import matplotlib.pyplot as plt
def evalPoly(c,x,xu):
n = len(x) - 1 # Degree of polynomial
p = c[n]
for k in range(1,n+1):
p = c[n-k] + (xu -x[n-k])*p
return p
def polycoef(x,y):
n = len(x) # Number of data points
c = y.copy()
for j in range(1,n):
c[j:n] = (c[j:n] - c[j-1])/(x[j:n] - x[j-1])
return c
In the next part, we solve an example from the beginning of a lecture on interpolation
In [51]:
x=array([2 ,4 , 6, 8, 10 , 12, 14, 16, 18, 20, 22, 24])
y=array([0.0 ,0.35 , 0.46, 0.48, 0.50, 0.39,0.27,0.18,0.13,0.09, 0.07, 0.05])
c = polycoef(x,y)
xp = np.linspace(2, 24, 1000)
yu = evalPoly(c,x,xp)
fig, ax = plt.subplots(figsize=(6.5, 4))
plt.plot(x, y, 'bo', label="Data")
plt.plot(xp, yu,label="yInterp")
ax.legend(loc='upper right')
plt.savefig("interolacija.eps")
Problems related to Newton's interpolating polynomial¶
We will now solve a couple of problems related to Newton's interpolating polynomial and divided differences.
In [10]:
vid = YouTubeVideo("tvHZovLh3c4")
display(vid)
In [ ]: