Lagrange interpolation method + python implementation


This article will introduce the mathematical principles of Lagrange interpolation and implement it in Python.

1. Mathematical principles

First of all, we have to know a theorem: given n+1 different nodes, its n-degree interpolation polynomial is unique.

1.1 Linear interpolation

Linear interpolation is an interpolation
Known:
insert image description here

Solve: L 1 ( x ) = a 1 x + a 0 L_1(x)=a_1x+a_0L1(x)=a1x+a0
According to the point slope formula:
L 1 ( x ) = y 0 + ( y 1 − y 0 ) ( x 1 − x 0 ) ( x − x 0 ) L_1(x)=y_0+\frac{(y_1-y_0)} {(x_1-x_0)}(x-x_0)L1(x)=y0+(x1x0)(y1y0)(xx0)
即:
L 1 ( x ) = ( x − x 1 ) ( x 0 − x 1 ) y 0 + ( x − x 0 ) ( x 1 − x 0 ) y 1 L_1(x)=\frac{(x) -x1)}{(x_0-x_1)}y_0+\frac{(x-x0)}{(x_1-x_0)}y_1L1(x)=(x0x1)(xx 1 ).y0+(x1x0)(xx 0 ).y1
This is a Lagrangian polynomial
Let
l 0 ( x ) = ( x − x 1 ) ( x 0 − x 1 ) l_0(x)=\frac{(x-x1)}{(x_0-x_1)}l0(x)=(x0x1)(xx 1 ).
l 1 ( x ) = ( x − x 0 ) ( x 1 − x 0 ) l_1(x)=\frac{(x-x0)}{(x_1-x_0)} l1(x)=(x1x0)(xx 0 ).
Then the first-order Lagrange polynomial can be expressed as
l 1 ( x ) = y 0 l 0 ( x ) + y 1 l 1 ( x ) l_1(x)=y_0l_0(x)+y_1l_1(x)l1(x)=y0l0(x)+y1l1(x)

1.2 Secondary interpolation

Knowledge:
insert image description here
Solving:
L 2 ( x ) = a 2 x 2 + a 1 x + a 0 L_2(x)=a_2x^2+a_1x+a_0L2(x)=a2x2+a1x+a0
Constructed in the following way, let
L 2 ( x ) = A ( x − x 1 ) ( x − x 2 ) + B ( x − x 0 ) ( x − x 2 ) + C ( x − x 0 ) ( x − x 1 ) L_2(x)=A(x-x_1)(x-x_2)+B(x-x_0)(x-x_2)+C(x-x_0)(x-x_1)L2(x)=A(xx1)(xx2)+B(xx0)(xx2)+C(xx0)(xx1)
can be solved:
A = y 0 ( x 0 − x 1 ) ( x 0 − x 2 ) A=\frac{y_0}{(x_0-x_1)(x_0-x_2)}A=(x0x1)(x0x2)y0
B = y 1 ( x 1 − x 0 ) ( x 1 − x 2 ) B=\frac{y_1}{(x_1-x_0)(x_1-x_2)} B=(x1x0)(x1x2)y1
C = y 2 ( x 2 − x 0 ) ( x 2 − x 1 ) C=\frac{y_2}{(x_2-x_0)(x_2-x_1)} C=(x2x0)(x2x1)y2
于是可以得到:
L 2 ( x ) = ∑ i = 1 2 ( ∏ i = 0 , i ≠ j 2 x − x i x j − x i ) y j L_2(x)=\sum\limits_{i=1}^{2}(\prod\limits_{i=0,i\neq{j}}^{2}\frac{x-x_i}{x_j-x_i})y_j L2(x)=i=12(i=0,i=j2xjxixxi)yj
This formula is called a quadratic Lagrange polynomial
If let:
l 0 ( x ) = ( x − x 1 ) ( x − x 2 ) ( x 0 − x 1 ) ( x 0 − x 2 ) l_0(x) =\frac{(x-x1)(x-x_2)}{(x_0-x_1)(x_0-x_2)}l0(x)=(x0x1)(x0x2)(xx 1 ) ( xx2
l 1 ( x ) = ( x − x 0 ) ( x − x 2 ) ( x 1 − x 0 ) ( x 1 − x 2 ) l_1(x)=\frac{(x-x0)(x-x_2)}{(x_1-x_0)(x_1-x_2)} l1(x)=(x1x0)(x1x2)(xx0)(xx2)
l 2 ( x ) = ( x − x 0 ) ( x − x 1 ) ( x 2 − x 0 ) ( x 2 − x 1 ) l_2(x)=\frac{(x-x0)(x-x_1)}{(x_2-x_0)(x_2-x_1)} l2(x)=(x2x0)(x2x1)(xx0)(xx1)

L 2 ( x ) = y 0 l 0 ( x ) + y 1 l 1 ( x ) + y 2 l 2 ( x ) L_2(x)=y_0l_0(x)+y_1l_1(x)+y_2l_2(x) L2(x)=y0l0(x)+y1l1(x)+y2l2(x)

1.3 Lagrangian interpolation polynomial of degree n

According to the above law, we can get the n-degree Lagrangian interpolation polynomial:
L 2 ( x ) = ∑ i = 1 n ( ∏ i = 0 , i ≠ jnx − xixj − xi ) yj L_2(x)=\sum\limits_ {i=1}^{n }(\prod\limits_{i=0,i\neq{j}}^{n}\frac{x-x_i}{x_j-x_i})y_jL2(x)=i=1n(i=0,i=jnxjxixxi)yj

2. Python implementation

import numpy as np

def LagrangeInterpolation(x):
    grid_x = np.array([4, 5, 6])
    k = len(grid_x)
    value = np.array([10, 5.25, 1])
    result = 0
    for j in range(k):
        result_l = 1
        for i in range(k):
            if i != j:
                result_l = result_l * (x - grid_x[i]) / (grid_x[j] - grid_x[i])

        result = result + value[j] * result_l

    return result


if __name__ == '__main__':
    x=18
    result=LagrangeInterpolation(x)
    print(result)

The result is as follows:

-11.0
参考:https://blog.csdn.net/u011699626/article/details/120394802

Guess you like

Origin blog.csdn.net/qq_45160840/article/details/127738458