2018年12月27日 星期四

Python 語言 範例1-13 已知不明分行物體, 經光測儀器之量測,印出Hermite 差除表

Python 語言 範例1-13 已知不明分行物體, 經光測儀器之量測,印出Hermite 差除表

範例1-13 已知不明分行物體, 經光測儀器之量測其結果如下
=========================================
時間                位置               飛行速度
 x                       f(x)                  f ' (x)
 0                       50.00               50.00
 2                       216.67             94.44
 4                       410.00             98.00
=========================================
印出Hermite 差除表

'''
/* ex1-13.c To generate the coeffficients for
 * Hermite Interpolating Polynomial H on the distinct numbers
 * x0,x1,x2,...with f0,f1,f2,... and f'0,f'1,f'2...
 * based on the Newton backward divided-difference Algorithm
 * and output the divided_difference table for Hn(x).
 */

n=2

0.0  50.0    50.0
2.0  216.67  94.44
4.0  410.00  98.00

i z(i)   f(i)   f(i-1,i)   f(i-2,i-1,i)...
0  0.0 50.0000
1  0.0 50.0000  50.0000
2  2.0 216.6700 83.3350 16.6675
3  2.0 216.6700 94.4400  5.5525 -5.5575
4  4.0 410.0000 96.6650  1.1125 -1.1100  1.1119
5  4.0 410.0000 98.0000  0.6675 -0.2225  0.2219 -0.2225

'''
# Number of values given
from array import *
n = 2
x= [0.0 , 2.0 , 4.0]
f= [50.0, 216.67 , 410.00]
ff=[50.0, 94.44 , 98.00]
z=[0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0 ]

q=[[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.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("i z(i)\t    f(i)\t  f(i-1,i)\t  f(i-2,i-1,i)...\n")
for i in range (0,n+1):
    z[2*i]=x[i]
    z[2*i+1]=x[i]
 
    q[2*i][0]=f[i]
    q[2*i+1][0]=f[i]
    q[2*i+1][1]=ff[i]

    if(i!=0):
        q[2*i][1]=(q[2*i][0]-q[2*i-1][0])/(z[2*i]-z[2*i-1])

    if(i==0):
        print('{:d} {:f} {:f} '.format(i,z[i],q[i][0]),"\n")
    elif (i==1):
        print("{:d} {:f} {:f} {:f}".format(i,z[i],q[i][0],q[i][1]),end='')
    elif (i==2):
        print("\n")
#print(q)

for i in range (2,2*n+2):
    print("{:d} {:f} {:f} {:f}".format(i,z[i],q[i][0],q[i][1]),'\t',end='');
    for j in range (2,i+1):
        # print(i,'->',j,end='') debug
        q[i][j]=(q[i][j-1]-q[i-1][j-1])/(z[i]-z[i-j])
        print("{:f}".format(q[i][j]),'\t',end='')
    print("\n");


======= RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/Ex1-13-0.py ============
i z(i)     f(i)   f(i-1,i)   f(i-2,i-1,i)...

0 0.000000 50.000000  

1 0.000000 50.000000 50.000000

2 2.000000 216.670000 83.335000 16.667500

3 2.000000 216.670000 94.440000 5.552500 -5.557500

4 4.000000 410.000000 96.665000 1.112500 -1.110000 1.111875

5 4.000000 410.000000 98.000000 0.667500 -0.222500 0.221875 -0.222500

>>> 

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

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