例題1-9 已知六點的座標值 使用牛頓內插法 求差除表?
n=6
x f(x)
=============
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
=============
'''
/* ex1-9-7.py is for developing the divided-defference
* table for Newton Interpolation polynomial.
*/
n=6
x f(x)
=============
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
=============
'''
n=6
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 , 0.1 , 0.3 ,0.6 , 1.0 , 1.1]
f= [[-6.0 , -5.89483 ,-5.65014 ,-5.17788 ,-4.28172 ,-3.99583 ],
[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)\t\t\tf(i,i+1)\tf(i,i+1.i+2), ......\n");
for i in range (0 ,n):
print ("{%d}\t{%2.5f}" %(i,x[i]), end='')
for j in range (0 , n-i):
print("\t{%2.5f}" %(f[j][i]) ,end='')
print("\n")
======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/Ex1-9-7.py ===========
Newton's Divided Interpolation :
Enter the value of n : 6
Enter the value of x[i] and f[i][0] :
x[i] f[i][0]
0.0 -6.0
0.1 -5.8948
0.3 -5.6501
0.6 -5.1779
1.0 -4.2817
1.1 -3.9958
Divided Difference Table:
==================================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
{0} {0.00000}{-6.00000}{1.05170}{0.57250}{0.21500}{0.06302} {0.01416}
{1} {0.10000}{-5.89483}{1.22345}{0.70150}{0.27802}{0.07859}
{2} {0.30000}{-5.65014}{1.57420}{0.95171}{0.35661}
{3} {0.60000}{-5.17788}{2.24040}{1.23700}
{4} {1.00000}{-4.28172}{2.85890}
{5} {1.10000}{-3.99583}
>>>
P(x)= {-6.00000} + {1.05170} (x-0.0) + {0.57250} (x-0.0) (x-0.1) +
{0.21500} (x-0.0) (x-0.1) (x-0.3) + {0.06302}(x-0.0) (x-0.1) (x-0.3) (x-0.6) +
{0.01416} (x-0.0) (x-0.1) (x-0.3) (x-0.6) (x-1.0)
沒有留言:
張貼留言