2018年12月24日 星期一

Lagrange Interpolation Algorithm 將C改寫成python EX1-4.py

'''
n=3
xa=1.5
1.0  0.0
2.0  0.693
3.0  1.099
4.0  1.386

/* ex1-4.c: 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])
 */
'''
xa=1.5
x= list()
x.extend([1.0,2.0,3.0,4.0])
f= list()
f.extend([0.0,0.693,1.099,1.386])
result=0.0
n=3

for k in range (0,n):
    print(x)
    print(f)

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]

s = 'The value of p' + repr(xa) + '= ' + repr(result) + '...'
print(s)


                                                                                                                      
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...                                                                                        
                                                                                                                      
                                                                                                                      
...Program finished with exit code 0                                                                                  
Press ENTER to exit console.  
                                

沒有留言:

張貼留言

2024產專班 作業2

 2024產專班 作業2   1. 系統圖       ESP32+MFRC522 組成RFID Reader 可以將RFID卡片的UID 透過 MQTT協定    上傳(發行 主題 (:topic) alex9ufo/2024/RFID/RFID_UID  ,, Payload...