第一,演示邻近点插值Nearest,线性插值Linear,三次样条插值Spline和立方插值Pchip,启动MATLAB,新建脚本(Ctrl+N),输入如下代码:
close all; clear all; clc
x=0:0.5:2*pi; y=sin(x);
%-1—Nearest interpolation—%
figure(‘Position’,[50,50,500,400],’Name’,’Nearest’)
x1=0:0.1:2*pi;y1=interp1(x,y,x1,’Nearest’);
plot(x,y,’.’,’color’,’r’,’MarkerSize’,20);hold on;
plot(x1,y1,’.’,’color’,’b’,’MarkerSize’,10);hold off;
xlabel(‘x’,’fontsize’,20);ylabel(‘y’,’fontsize’,20);
%-2—Linear interpolation—%
figure(‘Position’,[100,100,500,400],’Name’,’Linear’)
x2=0:0.1:2*pi;y2=interp1(x,y,x1,’Linear’);
plot(x,y,’.’,’color’,’r’,’MarkerSize’,20);hold on;
plot(x2,y2,’.’,’color’,’b’,’MarkerSize’,10);hold off;
xlabel(‘x’,’fontsize’,20);ylabel(‘y’,’fontsize’,20);
%-3—Spline interpolation—%
figure(‘Position’,[150,150,500,400],’Name’,’Spline’)