例題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)
>>>
2019年2月5日 星期二
訂閱:
張貼留言 (Atom)
2024_09 作業3 以Node-Red 為主
2024_09 作業3 (以Node-Red 為主 Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker 2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言