2019年2月5日 星期二

範例1-3 通過下面4點的Lagrange內插法求P(1.5)之值?

範例1-3
通過下面4點的Lagrange內插法求P(1.5)之值?

(1 , 0.0)  (2 , 0.693)  (3 , 1.099)  (4 , 1.386)

x               f(x)
==================
1.0              0.0
2.0              0.693
3.0              1.099
4.0              1.386
===================



# 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=4
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  , 3.0  ,4.0]
fx=[0.0 , 0.693 , 1.099 , 1.386]
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)) )




========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX1-3.py ============
Please enter function's point count: 4
1.0 0.0
2.0 0.693
3.0 1.099
4.0 1.386

Please enter point to calculate: 1.5


L(0) = (0.312)

L(1) = (0.938)

L(2) = (-0.312)

L(3) = (0.062)



Result =====> f(1.50) = (0.3929)

>>>




源自於
https://mat.iitm.ac.in/home/sryedida/public_html/caimna/interpolation/lagrange.html
LAGRANGE'S INTERPOLATION FORMULA
This is again an Nth degree polynomial approximation formula to the function f(x), which is known at discrete points xii = 0, 1, 2 . . . Nth. The formula can be derived from the Vandermonds determinant but a much simpler way of deriving this is from Newton's divided difference formula. If f(x) is approximated with an Nth degree polynomial then the Nthdivided difference of f(x) constant and (N+1)th divided difference is zero. That is
f [x0, x1, . . . xn, x] = 0
From the second property of divided difference we can write
f0
 + 
fn
fx
  =  0


+ . . . + 

(x- x1) . . . (x- xn)(x0- x)
(x- x0) . . . (xn - xn-1)(xn- x)
 
(x - x0) . . . (x - xn)
or
 
(x - x1) . . . (x - xn)
 
(x - x0) .  .  . (x - xn-1)
 
f(x)  = 

f0  + .   .   . +

 fn
 
(x- x1) . . . (x- xn)
 
(x- x0) .  .  . (xn - xn-1)
 

 
n
(
   n 
  
)fi
 
S
|  |
 
 x - xj
 
j = 0
 
(xi - xj)
i = 0
j ¹ 1
  
Since Lagrange's interpolation is also an Nth degree polynomial approximation to f(x) and the Nth degree polynomial passing through (N+1) points is unique hence the Lagrange's and Newton's divided difference approximations are one and the same. However, Lagrange's formula is more convinent to use in computer programming and Newton's divided difference formula is more suited for hand calculations.
Example : Compute f(0.3) for the data
x
0
1
3
4
7
f
1
3
49
129
813
using Lagrange's interpolation formula (Analytic value is 1.831)

 
 
(x - x1) (x - x2)(x- x3)(x - x4)
 
(x - x0)(x - x1) (x - x2)(x - x3)
 
f(x)  = 

f0+ . . . + 

 f4
 
(x- x1) (x- x2)(x- x3)(x- x4)
 
(x- x0)(x- x1)(x- x2)(x- x3
 

 
 
(0.3 - 1)(0.3 - 3)(0.3 - 4)(0.3 - 7)
 
(0.3 - 0)(0.3 - 3)(0.3 - 4)(0.3 - 7)
 
       = 

 1+ 

 3 + 
 
(-1) (-3)(-4)(-7)
 
x (-2)(-3)(-6) 
 

  
(0.3 - 0)(0.3 - 1)(0.3 - 4)(0.3 - 7)
 
(0.3 - 0)(0.3 - 1)(0.3 - 3)(0.3 - 7)
 

 49 + 

129 + 
x 2 x (-1)(-4)
 
x 3 x 1 (-3)
 

  
(0.3 - 0)(0.3 - 1)(0.3 - 3)(0.3 - 4)
 

813
x 6 x 4 x 3
 
         = 1.831 

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...