範例1-4 利用Lagrange 求 P(1.5) 與 f(x)=log(x) = liog(1.5) 的誤差值
(1) x f(x) n=3+1 xa=1.5
==========================
1.0 0.0
2.0 0.693
3.0 1.099
4.0 1.386
=================
(2) x f(x) n=3+1 xa=1.5
==========================
1.0 0.0
1.2 0.182
1.4 0.336
2.0 0.693
=================
(3) x f(x) n=9 +1 xa=1.5
==========================
1.0 0.000
1.2 0.182
1.7 0.531
2.0 0.693
2.2 0.788
2.7 0.993
3.0 1.099
3.2 1.163
3.7 1.308
4.0 1.386
'''
n=3
xa=1.5
1.0 0.0
2.0 0.693
3.0 1.099
4.0 1.386
/* ex1-4.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
print('\nLagrange Interpolation Algorithm\n')
xa=1.5
result=0.0
'''
x= list()
x.extend([1.0,2.0,3.0,4.0])
f= list()
f.extend([0.0,0.693,1.099,1.386])
n=3
'''
'''
x=[1.0 , 1.2 , 1.4 , 2.0]
f=[0.0 , 0.182 , 0.336 , 0.693]
n=3
'''
x=[1.0 , 1.2 , 1.7 , 2.0 , 2.2 , 2.7 , 3.0 , 3.2 , 3.7 , 4.0]
f=[0.0 , 0.182 , 0.531 , 0.693 , 0.788 , 0.993 , 1.099 , 1.163 , 1.308 , 1.386]
n=9
print(x)
print(f)
print('\n')
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 * ( xa - x[i]) / ( x[k] - x[i])
result=result+temp*f[k]
s1 = 'The value of p' + repr(xa) + '= ' + repr(result) + '...'
print(s1)
s1 = 'The value of f(' + repr(xa) + ')= ' + repr(math.log(xa)) + '...'
print(s1)
s1 = 'The value of | f(' + repr(xa) + ') - p' + repr(xa) + ' = ' + repr( abs(math.log(xa) - result) ) + '...'
print(s1)
輸出畫面
========= RESTART: F:\2018-09勤益科大數值分析\數值分析\PYTHON\EX1-4.py ============
Lagrange Interpolation Algorithm
[1.0, 2.0, 3.0, 4.0]
[0.0, 0.693, 1.099, 1.386]
The value of p1.5= 0.392875...
The value of f(1.5)= 0.4054651081081644...
The value of | f(1.5) - p1.5 = 0.01259010810816441...
=========================================================
Lagrange Interpolation Algorithm
[1.0, 1.2, 1.4, 2.0]
[0.0, 0.182, 0.336, 0.693]
The value of p1.5= 0.40446875...
The value of f(1.5)= 0.4054651081081644...
The value of | f(1.5) - p1.5 = 0.0009963581081643968...
>>>
Lagrange Interpolation Algorithm
[1.0, 1.2, 1.7, 2.0, 2.2, 2.7, 3.0, 3.2, 3.7, 4.0]
[0.0, 0.182, 0.531, 0.693, 0.788, 0.993, 1.099, 1.163, 1.308, 1.386]
The value of p1.5= 0.40614193840579654...
The value of f(1.5)= 0.4054651081081644...
The value of | f(1.5) - p1.5 = 0.0006768302976321583...
>>>
訂閱:
張貼留言 (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 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言