2019年2月5日 星期二

例題1-8 計算差除表

例題1-8  計算差除表

x       f(x)
===========
0        0
1        -3
2        0
3        15
4        48
===========

'''
Differential Table Generator
Newton's Forward Interpolation Table and Newton's Backward Interpolation Table can be generated using c and c++ programming language.
Source Code For  Newton's Forward Interpolation Table and Newton's Backward Interpolation Table
'''
import math
def fx(x):
    return (math.pow(x,3) - 4*x)
#+++++++++++++++++++++++++
#// calculating factorial of given number n
def fact( n):
    f = 1;
    for i in range (2 , n+1):
        f *= i
    return f
#+++++++++++++++++++++++++
#// calculating u mentioned in the formula
def  u_cal( u , n):
    temp = u;
    for i in range  (1 , n):
        temp = temp * (u - i)
    return temp
#+++++++++++++++++++++++++
x= [0.0 for i in range(0,11)]     #x [n] 矩陣
y=  [ [0.0 for i in range(len(x))]  for j in range(len(x)) ]       #[n][n],
x= [0.0 , 1.0  ,2.0 , 3.0 , 4.0  , 0.0 ]
y=[ [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  ]]

n=5
#no. of items
print("Enter n : " ,n )
print("X[i]\t\tY[i]");
print("=====================")
for i  in range (0,n) :   
    print( round(x[i],4),"\t\t",round( y[0][i],4))
print()
   
#forward difference table
for i in range (1 ,n):
    for j in range (0 , n-i):
        y[i][j]=(y[i-1][j+1]-y[i-1][j]) / (x[i+j]-x[j])

 
print("\n***********Forward Difference Table ***********\n")
#display Forward Difference Table
for i in range (0 , n):
    print("\t(%.2f)" %(x[i]) , end='')
    for j in range (0 , (n-i)):
            print("\t(%.2f)" %(y[j][i]),end='')
    print("\n");


#//backward difference table
for i in range(1,n):
    for j in range (i , n):
        y[i][j]= (y[i-1][j]-y[i-1][j-1])  / (x[j]-x[j-i]);

print("\n***********Backward Difference Table ***********\n");
#//display Backward Difference Table
for i in range (0 , n):
    print("\t(%.2f)" %(x[i]),end='')
    for j in range(0 , i+1):
        print("\t(%.2f)" %(y[j][i]),end='')
    print("\n");



======= RESTART: F:\2018-09勤益科大數值分析\數值分析\PYTHON\Ex1-8-2.py ===========
Enter n :  5
X[i] Y[i]
=====================
0.0 0.0
1.0 -3.0
2.0 0.0
3.0 15.0
4.0 48.0


***********Forward Difference Table ***********

(0.00) (0.00) (-3.00) (3.00) (1.00) (0.00)

(1.00) (-3.00) (3.00) (6.00) (1.00)

(2.00) (0.00) (15.00) (9.00)

(3.00) (15.00) (33.00)

(4.00) (48.00)


***********Backward Difference Table ***********

(0.00) (0.00)

(1.00) (-3.00) (-3.00)

(2.00) (0.00) (3.00) (3.00)

(3.00) (15.00) (15.00) (6.00) (1.00)

(4.00) (48.00) (33.00) (9.00) (1.00) (0.00)

>>> 

沒有留言:

張貼留言

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

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