例題1-8 已知5點的座標值 使用牛頓內插法求 差除表
已知5點的座標值
n=5
x f(x)
=============
0.0 0.0
1.1 -3.0
2.0 0.0
3.0 15.0
4.0 48.0
=============
'''
/* ex1-8.py is for developing the divided-defference
* table for Newton Interpolation polynomial.
*/
'''
n=5
x= [0.0 for i in range(0,n)] #x [n] 矩陣
f= [ [0.0 for i in range(n)] for j in range(n) ] #[n] * [n] 矩陣
x= [0.0 , 1.0 , 2.0 ,3.0 ,4.0]
f= [[0.0 , -3.0 ,0.0 ,15.0 ,48.0 ,0.0 ],
[0.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[0.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[0.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[0.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ],
[0.0 , 0.0 ,0.0 ,0.0 ,0.0 ,0.0 ]]
print("\nNewton's Divided Interpolation : ")
print("\nEnter the value of n : ",n)
print("\nEnter the value of x[i] and f[i][0] : \n");
print("\tx[i]\t\tf[i][0]");
for i in range (len(x)) :
print( '\t',round(x[i],4),"\t\t",round( f[0][i],4))
print()
print("\n Divided Difference Table:")
print(" ==================================" )
for i in range (1 ,n):
for j in range (0 , n-i):
f[i][j]=(f[i-1][j+1]-f[i-1][j]) / (x[i+j]-x[j])
print("i\tx(i)\tf(i)\tf(i,i+1)\tf(i,i+1.i+2), ......\n");
for i in range (0 ,n):
print ("{%d}\t{%2.1f}" %(i,x[i]), end='')
for j in range (0 , n-i):
print("\t{%2.1f}" %(f[j][i]) ,end='')
print("\n")
Newton's Divided Interpolation :
Enter the value of n : 5
Enter the value of x[i] and f[i][0] :
x[i] f[i][0]
0.0 0.0
1.0 -3.0
2.0 0.0
3.0 15.0
4.0 48.0
Divided Difference Table:
==================================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
{0} {0.0} {0.0} {-3.0} {3.0} {1.0} {0.0}
{1} {1.0} {-3.0} {3.0} {6.0} {1.0}
{2} {2.0} {0.0} {15.0} {9.0}
{3} {3.0} {15.0} {33.0}
{4} {4.0} {48.0}
>>>
P(x) = 0.0 + -3.0 (x-0.0) + 3 (x-0.0) (x-1.0) + 1.0 (x-0.0) (x-1.0)(x-2.)
+ 0.0 (x-0.0) (x-1.0)(x-2.0) (x-3.0)
P(x)= x^3 + 4x
P(1.5) = -2.625
P(2.5) = 5.625
沒有留言:
張貼留言