You are on page 1of 2

clc clear close all t = linspace(-2, 3, 8)'; xi= linspace(-2,3,1000); y1 = exp(-t.

^2); y2 = [-1 -1 -1 -1 1 1 1 1]'; % nstructs in almost the same way pchip constructs . However, spline chooses the slopes at the differently, namely to make even continuous. This has the following effects: % % spline produces a smoother result, i.e. is continuous. % % spline produces a more accurate result if the data consists of values of a smooth function. % % pchip has no overshoots and less oscillation if the data are not smooth. % % pchip is less expensive to set up. % % The two are equally expensive to evaluate %---------------ilinear=interp1(t,y1,xi,'linear'); ispline=interp1(t,y1,xi,'spline'); ipchip=interp1(t,y1,xi,'pchip'); figure1 = figure; axes1 = axes('Parent',figure1,'Color',[0.8 0.8 0.8]); box(axes1,'on'); hold(axes1,'all'); plot(xi,ilinear,'color','m','linewidth',2) hold on plot(xi,ispline,'color','yellow','linewidth',2) plot(xi,ipchip,'color','red','linewidth',2) plot(xi,exp(-xi.^2),'color','blue','linewidth',2) plot(t,y1,'linestyle','none','marker','o')

legend('linear int','spline int','shape-preserving kubisk Hermiteinterpolation','-exp(-x^2)','data y1') hold off

%-----------fel il_er=exp(-xi.^2)-ilinear; is_er=exp(-xi.^2)-ispline; ip_er=exp(-xi.^2)-ipchip; figure2 = figure; axes1 = axes('Parent',figure2,'Color',[0.8 0.8 0.8]); box(axes1,'on'); hold(axes1,'all'); plot(xi,il_er,'color','m','linewidth',2) hold on plot(xi,is_er,'color','yellow','linewidth',2) plot(xi,ip_er,'color','red','linewidth',2) [il il_x]=max(abs(il_er)); [is is_x]=max(abs(is_er)); [ip ip_x]=max(abs(ip_er)); plot(xi(il_x),il_er(il_x),'linestyle','none','marker','o','ma rkersize',10) plot(xi(is_x),is_er(is_x),'linestyle','none','marker','o','ma rkersize',10) plot(xi(ip_x),ip_er(ip_x),'linestyle','none','marker','o','ma rkersize',10) legend(['linear interpolation max error: ' num2str(il)], ['spline interpolation max error: ' num2str(is)],['shapepreserving kubisk Hermiteinterpol. max error: ' num2str(ip)]) grid on hold off %------------------ilinear=interp1(t,y2,xi,'linear'); ispline=interp1(t,y2,xi,'spline'); ipchip=interp1(t,y2,xi,'pchip'); figure3 = figure; axes1 = axes('Parent',figure3,'Color',[0.8 0.8 0.8]); box(axes1,'on'); hold(axes1,'all'); plot(t,y2,'linestyle','none','marker','o') hold on plot(xi,ilinear,'color','m','linewidth',2) plot(xi,ispline,'color','yellow','linewidth',2) plot(xi,ipchip,'color','red','linewidth',2) legend('data y2','linear int','spline int','shape-preserving kubisk Hermiteinterpolation') hold off

You might also like