已知下列諸點
x f(x)
=============
1.0 2.72
1.2 3.32
1.4 4.06
1.5 4.48
1.8 6.05
2.0 7.39
寫出牛頓差除表及i=2到i=5的一元三次方程式
'''
/* pr1-3-b.py is for developing the divided-defference
* table for Newton Interpolation polynomial.
*/
n=6
x f(x)
=============
1.0 2.72
1.2 3.32
1.4 4.06
1.5 4.48
1.8 6.05
2.0 7.39
=============
'''
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= [1.0 , 1.2 , 1.4 , 1.5 , 1.8 , 2.0]
f= [[2.72 , 3.32 , 4.06 ,4.48 , 6.05 , 7.39 ],
[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: H:/2018-09勤益科大數值分析/數值分析/PYTHON/pr1-3-b.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]
1.0 2.72
1.2 3.32
1.4 4.06
1.5 4.48
1.8 6.05
2.0 7.39
Divided Difference Table:
=================================================================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
{0} {1.00000} {2.72000} {3.00000} {1.75000} {-0.16667} {2.11806} {-3.29861}
{1} {1.20000} {3.32000} {3.70000} {1.66667} {1.52778} {-1.18056}
{2} {1.40000} {4.06000} {4.20000} {2.58333} {0.58333}
{3} {1.50000} {4.48000} {5.23333} {2.93333}
{4} {1.80000} {6.05000} {6.70000}
{5} {2.00000} {7.39000}
>>>
寫出 i=2到i=5 的一元三次方程式
P(x) = 4.06 + 4.2 (x-1.4) + 2.58333 (x-1.4)(x-1.5) + 0.58333 (x-1.4)(x-1.5)(x-1.8)
沒有留言:
張貼留言