已知 平面上3點
x y=f(x)
===================
1.0 0.0
2.0 0.693
3.0 1.099
求x=1.5 , f(x)= ?
#/********** Lagrange's interpolation ***************/
n=3
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 ,3.0]
y=[0.0 , 0.693 , 1.099 ]
a=1.5 #x=1.5 求 f(1.5)= ?
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\nEnter the value of the x to find the respective value of y ",a)
k=0.0
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))
輸出畫面
======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX1-2.py ============
Enter the number of the terms of the table: 3
Enter the respective values of the variables x and y:
The values of x =
1.0 2.0 3.0
The values of y =
0.0 0.693 1.099
Enter the value of the x to find the respective value of y 1.5
The respective value of the variable y is: {0.382375}
>>>
沒有留言:
張貼留言