% Example 11.4b (Physics 330) clear; close all; x=-3:.4:3; y=x; % build the full 2-d grid for the crude x and y data % and make a surface plot [X,Y]=meshgrid(x,y); Z=cos((X.^2+Y.^2)/2); surf(X,Y,Z); title('Crude Data') %********************************************************* % now make a finer 2-d grid, interpolate linearly to % build a finer z(x,y) and surface plot it. % Note that because the interpolation is linear the mesh is finer % but the crude corners are still there %********************************************************* xf=-3:.1:3; yf=xf; [XF,YF]=meshgrid(xf,yf); ZF=interp2(X,Y,Z,XF,YF,'linear'); figure surf(XF,YF,ZF); title('Linear Interpolation') %********************************************************* % Now use cubic interpolation to round the corners. Note that there is % still trouble near the edge because these points only have data on one % side to work with, so interpolation doesn't work very well %********************************************************* ZF=interp2(X,Y,Z,XF,YF,'cubic'); figure surf(XF,YF,ZF); title('Cubic Interpolation') %********************************************************* % Now use spline interpolation to also round the corners and see how % it is different from cubic. You should notice that it looks better, % especially near the edges. Spline interpolation is often the % best. %********************************************************* ZF=interp2(X,Y,Z,XF,YF,'spline'); figure surf(XF,YF,ZF); title('Spline Interpolation')