'''
n=9
xa=1.5
1.0 0.000
1.2 0.182
1.7 0.531
2.0 0.693
2.2 0.788
2.7 0.993
3.0 1.099
3.2 1.163
3.7 1.308
4.0 1.386
/* Lagrange Interpolation Algorithm
* Read in data file of ex1-4.dat which has n point values
* and the value of interpolating point xa. Based on Lagrange
* Interpolation algorithm to compute p(xa) and output its value.
* (x[i],f[i]):given points and n+1 are number of points
* Ln,k(x)=l=summation of (x-x[i])/(x[k]-x[i]).
* p(x)=ff=L(x)*f(x[k])
*/
'''
print('\nLagrange Interpolation Algorithm\n')
xa=1.5
x= list()
x.extend([1.0, 1.2, 1.7, 2.0, 2.2, 2.7, 3.0, 3.2, 3.7, 4.0])
f= list()
f.extend([0.0, 0.182, 0.531 , 0.693, 0.788 , 0.993 , 1.099 , 1.163 , 1.308 ,1.386])
result=0.0
n=3
print(x)
print(f)
print('\n')
for k in range (0,n+1): #n -->n+1
temp=1.0;
for i in range (0,n+1): #n -->n+1
if(i !=k):
temp=temp * ( xa - x[i]) / ( x[k] - x[i])
result=result+temp*f[k]
s = 'The value of p' + repr(xa) + '= ' + repr(result) + '...'
print(s)
輸出畫面
Lagrange Interpolation Algorithm
[1.0, 1.2, 1.7, 2.0, 2.2, 2.7, 3.0, 3.2, 3.7, 4.0]
[0.0, 0.182, 0.531, 0.693, 0.788, 0.993, 1.099, 1.163, 1.308, 1.386]
The value of p1.5= 0.4064107142857144...
>>>
沒有留言:
張貼留言