% GriddataExample0305.m L Braile % Modified from example in Matlab Help, Griddata rand('seed',0) % Find 100 random x,y locations between -2 to 2 x = rand(100,1)*4-2; y = rand(100,1)*4-2; % Calculate function z(x,y) at x,y locations z = x.*exp(-x.^2-y.^2); % function definition ti = -2:.25:2; % set grid parameters [XI,YI] = meshgrid(ti,ti); % make x,y grid ZI = griddata(x,y,z,XI,YI,'cubic'); % grid (2-D interpolate) figure mesh(XI,YI,ZI), hold on % Make a mesh of gridded surface plot3(x,y,z,'o'), hold off % Show original x,y,z data values % % Plot color image and overlay contours figure pcolor(XI,YI,ZI) shading interp colormap jet colorbar hold on % plot the x-y locations of the original data plot(x,y,'+k') % Contour the gridded data v = [-0.5:0.1:0.5]; [c,h] = contour(XI,YI,ZI,v,'w-'); clabel(c,h,'color','w','fontweight','bold','fontsize',15) axis square hold off % % use interp2 to regrid for increased resolution and smoothness figure [X2,Y2] = meshgrid(-2:0.05:2); % set finer grid interval Z2 = interp2(XI,YI,ZI,X2,Y2,'cubic'); % replot color display and contour map for interpolated data pcolor(X2,Y2,Z2) shading interp colorbar hold on % Contour the gridded data v = [-0.5:0.1:0.5]; [c,h] = contour(X2,Y2,Z2,v,'w-'); clabel(c,h,'color','w','fontweight','bold','fontsize',15) axis square hold off % % Calculate and plot exact 2-D function for comparison t3 = -2:.05:2; % set grid parameters [X3,Y3] = meshgrid(t3,t3); % make x,y grid Z3 = X3.*exp(-X3.^2-Y3.^2); % function definition figure pcolor(X3,Y3,Z3) shading interp colormap jet colorbar hold on % Contour the gridded data v = [-0.5:0.1:0.5]; [c,h] = contour(X3,Y3,Z3,v,'w-'); clabel(c,h,'color','w','fontweight','bold','fontsize',15) axis square hold off