matlab interpolation: Lagrangian interpolation

Lagrangian interpolation is to perform Lagrange polynomial fitting on the function to be interpolated

This is the second issue of the matlab interpolation series, the first issue: [Numerical Analysis and Fitting] Matlab Cubic Spline Interpolation Fitting Data

(I will update more interpolation methods when I have time in the future)

I have referred to some other articles in the derivation process of this article. The code is written by myself. If there is something wrong or the formula is wrong, please criticize and correct

First, for the independent variable x to be interpolated and the data point y to be interpolated:

First, let's first understand its interpolation principle:

        For interpolation at any point in a set of numerical scatter points, find an n-degree polynomial that satisfies the corresponding conditions. We hope to represent it with the function values ​​of all points, and the function value of each point is the same as the original function value. conform to.

        Therefore, assuming that the function value of each point of the original data is y_i, in order to form the interpolation obtained y(x), the coefficient of the previous matching is l_i, then there are:

        Remember to deduce from 0, then
        y(x) = l_0y_0+l_1y_1+l_2y_2+...+l_ny_n = \sum_{i=1}^n l_iy_i

        Then starting with the interpolation of two points, the result should be a linear interpolation

then there is  \begin{Bmatrix} l_0(x_0)y_0 +l_1(x_0)y_1=y0\\ l_0(x_1)y_0+l_1(x_1)y_1=y1 \end{Bmatrix} obviously\begin{Bmatrix} l_0(x_0)=l_1(x_1)=1\\ l_0(x_1)=l_1(x_0)=0 \end{Bmatrix}

For each point in the middle, there should be

y(x) = y_0+\frac{y_1-y_0}{x_1-x_0}(x-x_0) =\frac{x_1-x}{x_1-x_0}y_0 +\frac{x-x_0}{x_1-x_0}y_1

sorted out

y(x) =\frac{x-x_1}{x_0-x_1}y_0 +\frac{x-x_0}{x_1-x_0}y_1

Then starting from 0, the interpolation coefficient is

l_1 = \frac{x-x_1}{x_0-x_1},l_2 = \frac{x-x_0}{x_1-x_0}, havel_i(x_j) = \delta_{ij}=\begin{Bmatrix} 1 & i=j\\ 0&i\neq j \end{Bmatrix}

At this time, the law is not obvious enough, so we use more points for related derivation

When the number of points is n+1, in order to ensure that l_i(x_j) = \delta_{ij}=\begin{Bmatrix} 1 & i=j\\ 0&i\neq j \end{Bmatrix}it still holds

First, in order to ensure the following conditions, the numerator is

 (picture from reference article

Basis function construction method of Lagrange interpolation polynomial (detailed derivation)

At this time, according to \delta_{ij} = 1, there can be n+1 equations, then since there are n+1 c(c0,c1,c2....cn), the corresponding c0 can be uniquely determined by solving the equation, after solving c0, can be determined

l_0(x)= \prod_{i=0,i\neq j}^n \frac{x-x_j}{x_i-x_j},y(x) = \sum_{i=0}^{n}l_iy_i

Through the derivation of the above formula, the following uses MATLAB to realize the above calculation content

First add the len.m ​​I called inside

%返回一维数组长度(行数或列数其中大的一个,必须是一位数组)
function [length]=len(A)

    s=size(A);
    if s(2)==1
        length=s(1); %取行
    end
    if s(1)==1
        length=s(2);
    end
    if s(1)~=1 && s(2)~=1
        disp('必须是单行向量或单列向量')
        return
    end
            
end

Write the function Lagrange.m to calculate the Lagrange interpolation:

I wrote many of the notes in English, so don’t think it’s too much trouble, I’m also getting scalp numb every day in order to learn English)   

% *** Lagrange  interpolation ***
function L = Lagrange(x,y,x_2)
% x --origional x vector
% y --origional y vector need to be interpolated
% x_2 --the vector x that you want to interpolate to 
% author:FriedParrot   --2022-2-4
if len(x) ~= len(y)
    error('The length of x and y should be correspond');
end

xi = x_2;  %  generate the vector that need to be interpolated
% define the initial vectors
L = zeros(1,len(xi));

for i = 1:1:len(xi)  % This is the index of the xi(to be interpolated)
    l = ones(1,len(x));  % it is used every time
    for k = 1:1:len(x)
        for j= 1:1:len(x)
            if j ~= k  % similar to prod but the denominator shouldn't be zero
                l(k) = l(k) * ( xi(i)-x(j)) / (x(k)-x(j));
                % the index should be k --the first index of the iteration
            end
        end % cacultte the l_j
        L(i) = L(i) + l(k)*y(k);  % just for every single loop of s
    end
end

if nargout == 0
    figure('name','Lagrange Interpolation');
    plot(xi,L);
end

end

Add the test code and effect below:

(the code uses extrapolation, i.e. the range in the formula is wider than the range in the data)

x = [1,2,4,6,8,9];
y =  cos(x);
x_2 = 0:0.05:10;
Lagrange(x,y,x_2);

Interpolation effect: 

 

Reference article:

Basis function construction method of Lagrange interpolation polynomial (detailed derivation)

Introduction to Lagrange interpolation in mathworld (you can enter it with over-the-wall software)

"Special Topic 1: Interpolation (1) Lagrangian Interpolation"

Guess you like

Origin blog.csdn.net/sbsbsb666666/article/details/122785623