例題1-7 牛頓的多項式內插法
已知3點座標
x f(x)
================
0 0
1 -3
2 0
求 x=0.5 f(x)= ??
#/****** Newton Divided Difference interpolation in Python ************/
import math
n=3
x= [0.0 for i in range(n+1)] #x [n] 矩陣
y= [0.0 for i in range(n+1)] #y [n] 矩陣
p =[0.0 for i in range(n+1)] #pf [n] 矩陣
x=[0.0 , 0.0 , 1.0 , 2.0 ]
y=[0.0 , 0.0 , -3.0 , 0.0 ]
k=0.5
print("\nEnter the number of observations:",n )
print("\nEnter the different values of x:\n");
for i in range (1,n+1) : #
print( round( x[i],4),"\t",end='')
print()
print("\nThe corresponding values of y are:\n");
for i in range (1,n+1) : #
print( round(y[i],4),"\t",end='')
print()
print("\nEnter the value of 'k' in f(k) you want to evaluate:" , k)
j=1
f1=1
f2=0;
f=y[1]
while(n !=1):
for i in range (1,n):
p[i] = ((y[i+1]-y[i])/(x[i+j]-x[i]))
y[i]=p[i]
f1=1;
for i in range (1 , j+1):
f1*=(k-x[i])
f2+=(y[1]*f1)
n=n-1
j=j+1
f+=f2
print("\nf {%.2f} = (%.6f) " %(k , f))
輸出畫面
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX1-7.py =============
Enter the number of observations: 3
Enter the different values of x:
0.0 1.0 2.0
The corresponding values of y are:
0.0 -3.0 0.0
Enter the value of 'k' in f(k) you want to evaluate: 0.5
f {0.50} = (-2.250000)
>>>
訂閱:
張貼留言 (Atom)
WOKWI DHT22 & LED , Node-Red + SQLite database
WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...
-
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 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言