2019年2月13日 星期三

習題1-5 已知下列諸點利用牛頓向前差除表 並求 P[0.50 ,0.60 ,0.65 , 0.75 , 0.90 , 1.10 ,1.30]之值

習題1-5
已知下列諸點
  x        f(x)
==========
0.50   0.6915
0.60   0.7257
0.65   0.7422
0.75   0.7734
0.90   0.8159
1.10   0.8643
1.30   0.9032

利用牛頓向前差除表
並求 P[0.50 ,0.60 ,0.65 , 0.75 , 0.90 , 1.10 ,1.30]之值



Python程式

import math
import array

n=7    #7 point n=6

xa=[0.55 , 0.7 , 0.8 , 1.0]
m=len(xa)

x=[0.50 ,0.60 ,0.65 , 0.75 , 0.90 , 1.10 ,1.30]
#y[][] is used for difference table with y[][0] used for input
f= [ [0.6915 , 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0] ,
     [0.7257 , 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0] ,
     [0.7422 , 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0] ,
     [0.7734 , 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0] ,
     [0.8159 , 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0] ,
     [0.8643 , 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0] ,
     [0.9032 , 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0] ]

for i in range (0,n):
    print("i=",i,",x=",round(x[i],3),'\t\t',end='')
    print("f[",i,"][0]=", round(f[i][0],5) )

print("\n Divided Difference Table:\n");
print(" =========================\n");
for j in range(1, n):
    for i in range(0 ,n-j):
        f[i][j]=(f[i+1][j-1]- f[i][j-1]) / (x[i+j]-x[i])
        #print(f[i][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{%8.5f}" %(i,x[i]),end='')
    for j in range(0 , n-i):
        print("\t{%8.5f} " %(f[i][j]),end='')
    print("\n");


print("\nTHE RESULTS OF INTERPOLATION:\n");
for j in range (len(xa)):
    dx=1.0;
    pn=f[0][0];
    for i in range (1 , n):
        dx=dx*(xa[j]-x[i-1]);
        pn=pn+f[0][i]*dx
    print("Pn(%6.3f)= %8.5f\n" %(xa[j],pn))


輸出畫面
======== RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/pr1-5-1.py ==========
i= 0 ,x= 0.5 f[ 0 ][0]= 0.6915
i= 1 ,x= 0.6 f[ 1 ][0]= 0.7257
i= 2 ,x= 0.65 f[ 2 ][0]= 0.7422
i= 3 ,x= 0.75 f[ 3 ][0]= 0.7734
i= 4 ,x= 0.9 f[ 4 ][0]= 0.8159
i= 5 ,x= 1.1 f[ 5 ][0]= 0.8643
i= 6 ,x= 1.3 f[ 6 ][0]= 0.9032

 Divided Difference Table:

 =========================

i x(i) f(i) f(i,i+1) f(i,i+1.i+2),  ......

{0} { 0.50000} { 0.69150} { 0.34200} {-0.08000} {-0.16000} { 0.44444} {-0.82540} { 1.14011} 

{1} { 0.60000} { 0.72570} { 0.33000} {-0.12000} { 0.01778} {-0.05079} { 0.08669} 

{2} { 0.65000} { 0.74220} { 0.31200} {-0.11467} {-0.00762} { 0.00989} 

{3} { 0.75000} { 0.77340} { 0.28333} {-0.11810} {-0.00119} 

{4} { 0.90000} { 0.81590} { 0.24200} {-0.11875} 

{5} { 1.10000} { 0.86430} { 0.19450} 

{6} { 1.30000} { 0.90320} 


THE RESULTS OF INTERPOLATION:

Pn( 0.550)=  0.70871

Pn( 0.700)=  0.75810

Pn( 0.800)=  0.78811

Pn( 1.000)=  0.84143

>>> 



沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...