2019年3月7日 星期四

Python語言 例題1-1 若已知下面二點 請問 x=1.5時 則f(1.5)= ??

Python語言  例題1-1 若已知下面二點
   x              y=f(x)
=================
  1.0            0.000
  2.0            0.693   
請問 x=1.5時 則f(1.5)= ??


# Lagrange Interpolation  Code
import math

def Lagrange( n, k,val):
    result = 1.0
    for i in range  (0 , n):
        if (i!=k):
            result = result * ( (val - x[i])  /  (x[k] - x[i]))
    return result

def Calculate(n, x):
    intPolasyon = 0.0
    L=  [  0.0 for i in range(size) ]
    for i in range (0 ,  n):
        L[i] = Lagrange(n, i, x)
        intPolasyon += fx[i] * L[i]
        #print("L(%d) = (%5.3f)\n" %( i, L[i]))

    print("\n")
    return intPolasyon


size=2
print("Please enter function's point count:",size)
fx= [  0.0 for i in range(size) ]
x=  [  0.0 for i in range(size) ]
x=[1.0  , 2.0  ]
fx=[0.0 , 0.693]
for i  in range (0,len(x)) : 
    print(  '\t',round(x[i],4),"\t\t",round( fx[i],4))
print()

question=1.5
print("Please enter point to calculate:", question)
print("\n");
print("Result =====> f(%4.2f) = (%5.4f)\n" %(question, Calculate(size, question)) )


輸入畫面


輸出畫面
Please enter function's point count: 2
1.0 0.0
2.0 0.693

Please enter point to calculate: 1.5




Result =====> f(1.50) = (0.3465)

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...