Interpolation example of MATLAB data

In the known data, using a relatively simple interpolation function f(x) to pass through all sample points and performing estimation calculations on adjacent data is called interpolation.
The interpolation function f(x) must pass through all sample points. However, in some cases, the acquisition of sample points itself contains measurement errors in the experiment. This requirement undoubtedly retains the influence of these measurement errors. Although meeting this requirement will make the "error" at the sample points zero, it will The error at non-sample points becomes too large, which is very unreasonable. For this reason, another function approximation method - data fitting is proposed, which does not require that the constructed approximate function f(x) all passes through the sample points, but "approximates" them very well.
1. One-dimensional interpolation
The interp1 function is provided in MATLAB to implement one-dimensional interpolation of functions. The calling format is as follows:
Y1=interp(x,y,X1,'method')
%According to the known data (x,y), use the method method to interpolate, and then calculate the function value corresponding
to y is a known data vector, where x should be arranged in ascending or descending order; X1 is the independent variable coordinate vector of the interpolation point: "method" is used to select the interpolation algorithm, which can be: "linear" (linear interpolation) , "cubic" (cubic polynomial interpolation), "nearest" (nearest neighbor interpolation), "Spline" (cubic spline interpolation), "previous" (early interpolation), "next" (delayed interpolation), "cubic" ( Cubic interpolation), "pchip" (piecewise third-order interpolation method), "makima" (makima interpolation), "v5cubic" (v5 cubic interpolation).

clear all;
x1=-6:0.2:6;
y1=1./(2+x1.^2);
x2=-6:10/(11-1):6;
y2=1./(2+x2.^2);
x=-6:0.3:6;
y3=interp1(x2,y2,x,'linear');
y4=interp1(x2,y2,x,'spline');
y5=interp1(x2,y2,x,'nearest');
y6=interp1(x2,y2,x,'makima');
y7=interp1(x2,y2,x,'next');
y8=interp1(x2,y2,x,'previous');
y9=interp1(x2,y2,x,'pchip');
y10=interp1(x2,y2,x,'cubic');
y11=interp1(x2,y2,x,'v5cubic');
subplot(221);
plot(x1,y1,'m:',x,y3);
title('线性');
subplot(222);
plot(x1,y1,'m:',x,y4);
title('三次样条插值')
subplot(223);
plot(x1,y1,'m:',x,y5);
title('最邻近插值');
subplot(224);
plot(x1,y1,'m:',x,y6);
title('玛奇玛');

Insert image description here

subplot(221);
plot(x1,y1,'m:',x,y7);
title('next');
subplot(222);
plot(x1,y1,'m:',x,y8);
title('previous')
subplot(223);
plot(x1,y1,'m:',x,y9);
title('pchip');
subplot(224);
plot(x1,y1,'m:',x,y10);
title('cubic');

Insert image description here

subplot(221);
plot(x1,y1,'m:',x2,y2,'o');
title('y=1/(2+x^2)');
subplot(222);
plot(x1,y1,'m:',x,y8);
title('previous');
subplot(223);
plot(x1,y1,'m:',x,y9);
title('pchip');
subplot(224);
plot(x1,y1,'m:',x,y11);
title('v5cubic');

Insert image description here
Development tools: MATLAB 2022b and WeChat Alt+A screenshot tool

References
[1] Zhang Defeng. MATLAB/Simulink modeling and simulation examples [M] Beijing: Machinery Industry Press, page 39

Guess you like

Origin blog.csdn.net/m0_38127487/article/details/133193547