2019年1月31日 星期四

例題1-1 Lagrange內插法 已知 平面上二點 求x=1.5 , f(x)= ?

例題1-1 Lagrange內插法
已知 平面上二點

   x                  y=f(x)
===================
 1.0                 0.0
 2.0                 0.693

求x=1.5 ,  f(x)= ?


#/********** Lagrange's interpolation ***************/
n=2
x= [0.0 for i in range(n+1)]     #x [n] 矩陣
y= [0.0 for i in range(n+1)]     #f [n] 矩陣
x=[1.0 , 2.0]
y=[0.0 , 0.693]
a=1.5   #x=1.5 求 f(1.5)= ?

k=0.0
d=1

print("\nEnter the number of the terms of the table: ",n)
print("\nEnter the respective values of the variables x and y: \n")
print("The values of x =")
for i  in range (0,n) :     #
    print( round( x[i],4),"\t",end='')
print()
print("The values of y =")
for i  in range (0,n) :     #
    print( round( y[i],4),"\t",end='')

print("\n Enter the value of the x to find the respective value of y ",a)

while(d==1):
    for i in range(0, n):
        s=1.0
        t=1.0
        for j in range(0,n):
            if(j!=i):
                s=s*(a-x[j]);
                t=t*(x[i]-x[j]);
       
        k=k+((s/t)*y[i]);

    print("\nThe respective value of the variable y is: {%f}" %(k))
    d=0


輸出畫面
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX1-1.py ===========

Enter the number of the terms of the table:  2

Enter the respective values of the variables x and y: 

The values of x =
1.0 2.0
The values of y =
0.0 0.693
 Enter the value of the x to find the respective value of y  1.5

The respective value of the variable y is: {0.346500}
>>> 

沒有留言:

張貼留言

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

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