2019年2月12日 星期二

習題1-3-a 已知下列座標值求牛頓差除表

習題1-3-a 已知下列座標值
x        f(x)
=============
0.1      0.003
0.3      0.067
0.5      0.148
0.7      0.248
1.1      0.518
=============
求差除表


Python程式
'''
/* pr1-3-a.py is for developing the divided-defference
 * table for Newton Interpolation polynomial.
 */
n=5
x        f(x)
=============
0.1      0.003
0.3      0.067
0.5      0.148
0.7      0.248
1.1      0.518
=============
'''

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.1 , 0.3 , 0.5 , 0.7 ,  1.1]
f= [[0.003 ,  0.067   , 0.148  ,0.248  , 0.518 ],
      [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-a.py ==========

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.1 0.003
0.3 0.067
0.5 0.148
0.7 0.248
1.1 0.518


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

{0} {0.10000} {0.00300} {0.32000} {0.21250} {0.04167} {0.02604}

{1} {0.30000} {0.06700} {0.40500} {0.23750} {0.06771}

{2} {0.50000} {0.14800} {0.50000} {0.29167}

{3} {0.70000} {0.24800} {0.67500}

{4} {1.10000} {0.51800}

>>> 

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...