牛頓內插法的差除表
將C改寫成Python EX1-9 P1-38#C:Python3.7 python.exe (32bit)
# -*- coding:utf-8 -*-
'''
/* ex1-9.c is for developing the divided-defference
* table for Newton Interpolation polynomial.
*/
n=5
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
'''
from array import *
xa=1.5
x=[0.0 ,0.1 ,0.3 ,0.6 ,1.0 ,1.1]
f=[[-6.0 ,0.0,0.0,0.0,0.0,0.0,0.0] , [-5.89483,0.0,0.0,0.0,0.0,0.0] , [-5.65014,0.0,0.0,0.0,0.0] , [-5.17788,0.0,0.0,0.0] , [-4.28172,0.0,0.0] ,[-3.99583,0.0]]
# 留意 f Array的設定
result=0.0
n=5 # 0...5=6
print('x=',x)
print('f=',f)
print("\n\t\tDivided Difference Table:\n")
print(" ============================================\n")
for j in range (1,n+1):
for i in range (0,n-j+1):
f[i][j] = ( (f[i+1][j-1]) - (f[i][j-1]) ) / ( x[i+j] - x[i] )
print("i\tx(i)\tf(i)\tf(i,i+1)\tf(i,i+1,i+2)\tf(i,i+1,i+2,i+3) ......\n")
for i in range (0,n+1):
print('%d ,\t%2.4f' %(i,x[i]),end='')
for j in range(0,n-i+1):
print('\t%8.5f' %f[i][j],end='')
print("\n")
x= [0.0, 0.1, 0.3, 0.6, 1.0, 1.1]
f= [[-6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [-5.89483, 0.0, 0.0, 0.0, 0.0, 0.0], [-5.65014, 0.0, 0.0, 0.0, 0.0], [-5.177
88, 0.0, 0.0, 0.0], [-4.28172, 0.0, 0.0], [-3.99583, 0.0]]
Divided Difference Table:
============================================
i x(i) f(i) f(i,i+1) f(i,i+1,i+2) f(i,i+1,i+2,i+3) ......
0 , 0.0000 -6.00000 1.05170 0.57250 0.21500
0.06302 0.01416
1 , 0.1000 -5.89483 1.22345 0.70150 0.27802
0.07859
2 , 0.3000 -5.65014 1.57420 0.95171 0.35661
3 , 0.6000 -5.17788 2.24040 1.23700
4 , 1.0000 -4.28172 2.85890
5 , 1.1000 -3.99583
...Program finished with exit code 0
Press ENTER to exit console.
沒有留言:
張貼留言