2019年2月13日 星期三

習題1-4 已知下列座標求牛頓向前差除表

習題1-4 已知下列座標
x= [1.2 , 1.25 , 1.30 , 1.40 ,  1.45 , 1.50]
f= [[0.1823 ,  0.2231   , 0.2624  ,0.3365  , 0.3716 , 0.4055 ],
求牛頓向前差除表

Python 程式

'''
/* pr1-4-a.py is for developing the divided-defference
 * table for Newton Interpolation polynomial.
 */
#test Pattern
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 ]]


        Divided Difference Table:
=========================================================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2),  ......

{0} {0.00000} {0.00000} {-3.00000} {3.00000} {1.00000} {0.00000}

{1} {1.00000} {-3.00000} {3.00000} {6.00000} {1.00000}

{2} {2.00000} {0.00000} {15.00000} {9.00000}

{3} {3.00000} {15.00000} {33.00000}

{4} {4.00000} {48.00000}

>>>
     
'''

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.2 , 1.25 , 1.30 , 1.40 ,  1.45 , 1.50]
f= [[0.1823 ,  0.2231   , 0.2624  ,0.3365  , 0.3716 , 0.4055 ],
      [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)\t\tf(i)\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-4.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.2 0.1823
1.25 0.2231
1.3 0.2624
1.4 0.3365
1.45 0.3716
1.5 0.4055


        Divided Difference Table:
================================================================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2),  ......

{0} {1.20000} {0.18230} {0.81600} {-0.30000} {0.00000} {0.80000} {-4.00000}

{1} {1.25000} {0.22310} {0.78600} {-0.30000} {0.20000} {-0.40000}

{2} {1.30000} {0.26240} {0.74100} {-0.26000} {0.10000}

{3} {1.40000} {0.33650} {0.70200} {-0.24000}

{4} {1.45000} {0.37160} {0.67800}

{5} {1.50000} {0.40550}

>>> 


沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...