-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterp.py
128 lines (76 loc) · 2.54 KB
/
interp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
def driver():
f = lambda x: np.sinc(5*x)
N = 17
''' interval'''
a = -1
b = 1
''' create equispaced interpolation nodes'''
xint = np.array(chebyshev_nodes(N+1,a,b))
''' create interpolation data'''
yint = f(xint)
''' create points for evaluating the Lagrange interpolating polynomial'''
Neval = 1000
xeval = np.array(chebyshev_nodes(Neval+1,a,b))
yeval_l= np.zeros(Neval+1)
yeval_dd = np.zeros(Neval+1)
'''Initialize and populate the first columns of the
divided difference matrix. We will pass the x vector'''
y = np.zeros( (N+1, N+1) )
for j in range(N+1):
y[j][0] = yint[j]
y = dividedDiffTable(xint, y, N+1)
''' evaluate lagrange poly '''
for kk in range(Neval+1):
yeval_l[kk] = eval_lagrange(xeval[kk],xint,yint,N)
yeval_dd[kk] = evalDDpoly(xeval[kk],xint,y,N)
''' create vector with exact values'''
fex = f(xeval)
plt.figure()
plt.plot(xeval,fex,'ro-')
plt.plot(xeval,yeval_l,'bs--')
plt.plot(xeval,yeval_dd,'c.--')
plt.legend()
plt.figure()
err_l = abs(yeval_l-fex)
err_dd = abs(yeval_dd-fex)
plt.semilogy(xeval,err_l,'ro--',label='lagrange')
plt.semilogy(xeval,err_dd,'bs--',label='Newton DD')
plt.legend()
plt.show()
def chebyshev_nodes(n, a=-1, b=1):
i = np.arange(n)
x = np.cos((2*i + 1) / (2*n) * np.pi)
# Map from [-1, 1] to [a, b]
return 0.5 * (a + b) + 0.5 * (b - a) * x
def eval_lagrange(xeval,xint,yint,N):
lj = np.ones(N+1)
for count in range(N+1):
for jj in range(N+1):
if (jj != count):
lj[count] = lj[count]*(xeval - xint[jj])/(xint[count]-xint[jj])
yeval = 0.
for jj in range(N+1):
yeval = yeval + yint[jj]*lj[jj]
return(yeval)
''' create divided difference matrix'''
def dividedDiffTable(x, y, n):
for i in range(1, n):
for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;
def evalDDpoly(xval, xint,y,N):
''' evaluate the polynomial terms'''
ptmp = np.zeros(N+1)
ptmp[0] = 1.
for j in range(N):
ptmp[j+1] = ptmp[j]*(xval-xint[j])
'''evaluate the divided difference polynomial'''
yeval = 0.
for j in range(N+1):
yeval = yeval + y[0][j]*ptmp[j]
return yeval
driver()