Study notes-Matlab algorithm articles-interpolation algorithm

Interpolation algorithm

01 Lagrangian polynomial interpolation

 

And then get the Lagrangian polynomial:

Matlab solving: There is no built-in solving function in matlab , you need to implement it yourself.

function f = Language(x,y,x0)
    syms t;
    if(length(x) == length(y))
        n = length(x);    
    else
        disp('x和y的维数不相等!');
        return;
    end                                      %检错
    f = 0.0;
    for i = 1:n
        l = y(i); 
        for j = 1:i-1
            l = l*(t-x(j))/(x(i)-x(j));      
        end
        for j = i+1:n
            l = l*(t-x(j))/(x(i)-x(j));      %计算拉格朗日基函数
        end
        f = f + l;                           %计算拉格朗日插值函数      
        simplify(f);                         %化简
        if(i==n)
            if(nargin == 3)
                f = subs(f,'t',x0);          %计算插值点的函数值
            else
                f = collect(f);          %将插值多项式展开
                f = vpa(f,6);                %将插值多项式的系数化成6位精度的小数
            end
        end
    end
end

Example (e01) : Use Lagrangian polynomial interpolation to calculate data

x=-5:0.2:5;
y=-3*x.^2+2*x+x.^3-2;
figure,plot(x,y,'-.b+','linewidth',2);hold on;

x0=2.3;
y0=3*x0+2*x0.^2-x0.^3+2;
tic
yl=Language(x,y,2.3);
toc
fprintf('理论值:%.4f,实际值%.4f\n',y0,yl);
plot(x0,y0,'r*','markersize',12);hold off;

result:

>> e01
时间已过 35.958879 秒。
理论值:7.3130,实际值-1.1030

02 Newton method interpolation

Then you can get:

function f = Newton(x,y,x0)
syms t;

if(length(x) == length(y))
    n = length(x);
    c(1:n) = 0.0;
else
    disp('x和y的维数不相等!');
    return;
end

f = y(1);
y1 = 0;
l  = 1;
 
for(i=1:n-1)   
    for(j=i+1:n)
        y1(j) = (y(j)-y(i))/(x(j)-x(i));
    end
    c(i) = y1(i+1);     
    l = l*(t-x(i));  
    f = f + c(i)*l;
    simplify(f);
    y = y1;
    
    if(i==n-1)
        if(nargin == 3)
            f = subs(f,'t',x0);
        else
            f = collect(f);                %将插值多项式展开
            f = vpa(f, 6);
        end
    end
end



x=-5:0.2:5;
y=3*x+2*x.^2-x.^3+2;
figure,plot(x,y,'-.b+','linewidth',2);hold on;

x0=2.3;
y0=3*x0+2*x0.^2-x0.^3+2;
tic
yl=Newton(x,y,2.3);
toc
fprintf('理论值:%.4f,实际值%.4f\n',y0,yl);
plot(x0,y0,'r*','markersize',12);hold off;

result:

>> e02
时间已过 16.260375 秒。
理论值:7.3130,实际值7.3130

03 piecewise linear interpolation

Matlab implementation: use the function y=interp1(x,y,x0,'method')

method specifies the interpolation method, the default is linear interpolation. Its value can be:

'nearest' nearest term interpolation

'linear' linear interpolation

'spline' piece by piece 3rd spline interpolation

'cubic' preserves the convexity and convexity of the 3rd interpolation.

All difference methods require x0 to be monotonic.

Example (e03) : Use piecewise linear interpolation to calculate data

x=-5:0.2:5;
y=3*x+2*x.^2-3*x.^3+2*x.^4+2;
figure,plot(x,y,'-.b+','linewidth',2);hold on;

x0=2.3;
y0=3*x0+2*x0.^2-x0.^3+2;
tic
yl=interp1(x,y,2.3);
toc
fprintf('理论值:%.4f,实际值%.4f\n',y0,yl);
plot(x0,y0,'r*','markersize',12);hold off;

result:

>> e03
时间已过 0.025925 秒。
理论值:7.3130,实际值39.3952

 

04 Emil Interpolation

Definition: If an interpolation function is not only required to have the same value as the function at the node, but also required to have the same first, second or even higher order derivative value with the function, this is the Hermite interpolation problem. which is:

The Emilt interpolation polynomial is:

 

Matlab does not have a ready-made Hermite function, you need to write it manually: Hermite

Example (e04) : Use Emilt interpolation to calculate data

function f = Hermite(x,y,y_1,x0)
syms t;
f = 0.0;

if(length(x) == length(y))
    if(length(y) == length(y_1))
        n = length(x);
    else
        disp('y和y的导数的维数不相等!');
        return;
    end
else
    disp('x和y的维数不相等!');
    return;
end

for i=1:n
    h = 1.0;
    a = 0.0;
    for j=1:n
        if( j ~= i)
            h = h*(t-x(j))^2/((x(i)-x(j))^2);
            a = a + 1/(x(i)-x(j));
        end
    end
    
    f = f + h*((x(i)-t)*(2*a*y(i)-y_1(i))+y(i));
    
    if(i==n)
        if(nargin == 4)
            f = subs(f,'t',x0);
        else
            f = vpa(f,6);
        end
    end
end
x=-5:0.2:5;
y=3*x+5*x.^2-4*x.^3+2*x.^5+2;
figure,plot(x,y,'-.b+','linewidth',2);hold on;

x0=2.3;
y0=3*x0+2*x0.^2-x0.^3+2;
y01=3+4*x0-9*x0.^2-8*x0.^3;
tic
y1=Hermite(x,y,y01,x0);
toc
fprintf('理论值:%.4f,实际值%.4f\n',y0,yl);
plot(x0,y0,'r*','markersize',12);hold off;

result: 

>> e04
y和y的导数的维数不相等!
时间已过 0.071891 秒。
理论值:7.3130,实际值39.3952

05 spline interpolation 

Definition: In mathematics, a piecewise polynomial with a certain smoothness is called a spline function.

Definition of second order spline function:

3 spline function is defined:

The 3rd order spline function in Matlab :

y=interp1(x0,y0,x,'spline')

y=spline(x0,y0,x)

pp=csape(x0,y0,conds)y=ppval(pp,x)

The csape function is recommended .

pp= csape (x0,y0) : Use the default boundary conditions, namely Lagrange boundary conditions. = PP csape (X0, yO, conds) in conds boundary conditions specified interpolation, which value may be:

The'complete' boundary is the first derivative, which is the default boundary condition

'not-a-knot' non -knot condition

'periodic' period condition

The'second' boundary is the second derivative, and the value of the second derivative is [0, 0] .

'variational' sets the second derivative value of the boundary to [0,0] .

Example (e05) : The shape of the part to be processed is given by a set of data ( x, y ) according to the process requirements (in the case of a plane). When processing with a program-controlled milling machine, each tool can only go very small in the x and y directions One step, which requires obtaining ( x, y ) coordinates with a small step required for processing from known data . The x and y data given in the table are located on the lower contour line of the wing section. It is assumed that the y coordinate when the x coordinate changes by 0.1 is required . Try to complete the processing data, draw the curve, and find the slope of the curve at x=0 and the minimum value of y in the range of 13≤x≤15 . Requirements for LAGRANGE , piecewise linear interpolation and cubic spline three kinds of calculation methods.

0

3

5

7

9

11

12

13

14

15

0

1.2

1.7

2.0

2.1

2.0

1.8

1.2

1.0

1.6

x0=[0 3 5 7 9 11 12 13 14 15];
y0=[0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.0 1.6];
x=0:0.1:15;
y1=Language(x0,y0,x); %调用前面编写的Lagrange插值函数
y2=interp1(x0,y0,x);
y3=interp1(x0,y0,x,'spline');
pp1=csape(x0,y0); y4=ppval(pp1,x);
pp2=csape(x0,y0,'second'); y5=ppval(pp2,x);
fprintf('比较一下不同插值方法和边界条件的结果:\n')
fprintf('x y1 y2 y3 y4 y5\n')
xianshi=[x',y1',y2',y3',y4',y5'];
fprintf('%f\t%f\t%f\t%f\t%f\t%f\n',xianshi')
subplot(2,2,1), plot(x0,y0,'+',x,y1), title('Lagrange')
subplot(2,2,2), plot(x0,y0,'+',x,y2), title('Piecewise linear')
subplot(2,2,3), plot(x0,y0,'+',x,y3), title('Spline1')
subplot(2,2,4), plot(x0,y0,'+',x,y4), title('Spline2')
dyx0=ppval(fnder(pp1),x0(1)) %求x=0处的导数
ytemp=y3(131:151);
index=find(ytemp==min(ytemp));
xymin=[x(130+index),ytemp(index)]

%% 结果分析
%可以看出,拉格朗日插值的结果根本不能应用,分段线性插值的光滑性较差(特别
%是在 14 = x 附近弯曲处),建议选用三次样条插值的结果。

result:

Comparison of several interpolation methods: https://blog.csdn.net/f2157120/article/details/80371214

 

 

Guess you like

Origin blog.csdn.net/seek97/article/details/108322553