習題1-3 利用Lagrange 寫出方程式
'''
n=3
xa=[-0.5 ,0.5 ,1.2 , 1.5 ,1.8, 2.5 ]
-1.0 0.3679
0.0 1.00
1.0 2.7183
2.0 7.3891
/* pr1-1-b.py : Lagrange Interpolation Algorithm
* Read in data file of ex1-4.dat which has n point values
* and the value of interpolating point xa. Based on Lagrange
* Interpolation algorithm to compute p(xa) and output its value.
* (x[i],f[i]):given points and n+1 are number of points
* Ln,k(x)=l=summation of (x-x[i])/(x[k]-x[i]).
* p(x)=ff=L(x)*f(x[k])
*/
'''
import math
def fx(x):
return (math.exp(x))
print('\nLagrange Interpolation Algorithm\n')
xa=[-0.5 , 0.5 ,1.2, 1.5 ,1.8, 2.5 ]
x=[-1.0 , 0.0 , 1.0 , 2.0 ]
f=[0.3679 , 1.0 , 2.7183 , 7.3891]
n=3 # 4 point n=3
print('x=',x)
print('f=',f)
print('xa=',xa)
print('\n')
for m in range (len(xa)):
indata=xa[m]
result=0.0
print("xa=",repr(indata))
for k in range (0,n+1): #n -->n+1
temp=1.0;
for i in range (0,n+1): #n -->n+1
if(i !=k):
temp=temp * ( indata - x[i]) / ( x[k] - x[i])
result = result + temp *f[k]
s1 = 'The value of p' + repr(indata) + '= ' + repr(result) + ' ...'
print(s1)
resultfx=fx(indata)
s1 = 'The value of f(' + repr(indata) + ')= ' + repr(resultfx) + ' ...'
print(s1)
s1 = 'The value of | f(' + repr(indata) + ') - p' + repr(indata) + ' = ' + repr( abs(resultfx - result) ) + ' ...'
print(s1)
print("\n\n")
======== RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/pr1-1-c.py ==========
Lagrange Interpolation Algorithm
x= [-1.0, 0.0, 1.0, 2.0]
f= [0.3679, 1.0, 2.7183, 7.3891]
xa= [-0.5, 0.5, 1.2, 1.5, 1.8, 2.5]
xa= -0.5
The value of p-0.5= 0.66481875 ...
The value of f(-0.5)= 0.6065306597126334 ...
The value of | f(-0.5) - p-0.5 = 0.05828809028736659 ...
xa= 0.5
The value of p0.5= 1.6067312500000002 ...
The value of f(0.5)= 1.6487212707001282 ...
The value of | f(0.5) - p0.5 = 0.04199002070012803 ...
xa= 1.2
The value of p1.2= 3.3565384 ...
The value of f(1.2)= 3.3201169227365472 ...
The value of | f(1.2) - p1.2 = 0.03642147726345257 ...
xa= 1.5
The value of p1.5= 4.56799375 ...
The value of f(1.5)= 4.4816890703380645 ...
The value of | f(1.5) - p1.5 = 0.08630467966193578 ...
xa= 1.8
The value of p1.8= 6.1291576 ...
The value of f(1.8)= 6.0496474644129465 ...
The value of | f(1.8) - p1.8 = 0.07951013558705355 ...
xa= 2.5
The value of p2.5= 11.414906249999998 ...
The value of f(2.5)= 12.182493960703473 ...
The value of | f(2.5) - p2.5 = 0.7675877107034754 ...
>>>
訂閱:
張貼留言 (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 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言