smooth surface plot in matlab using data points in matlab

To smooth a surface plot made from data points in MATLAB, you can use the TriScatteredInterp() function to interpolate the data onto a finer grid, and then use the surf() function to display the interpolated surface. Here is an example code snippet:

main.m
% Generate some sample data points
x = randn(100,1);
y = randn(100,1);
z = 10*x.*exp(-x.^2-y.^2);

% Set up the interpolation grid
xi = linspace(min(x),max(x),100);
yi = linspace(min(y),max(y),100);
[xi,yi] = meshgrid(xi,yi);

% Interpolate the data onto the grid
zi = TriScatteredInterp(x,y,z,'natural');
zi = zi(xi,yi);

% Smooth the interpolated surface
szi = smoothn(zi);

% Plot the smoothed surface
surf(xi,yi,szi);
422 chars
20 lines

In this example, we first generate some random data points in 3D space (x, y, and z). We then set up an interpolation grid using the linspace() and meshgrid() functions. Then we use the TriScatteredInterp() function to interpolate the data onto the grid. The 'natural' option tells MATLAB to use natural neighbor interpolation for the surface.

Next, we use the smoothn() function to smooth the interpolated surface. This function uses a smoothing algorithm based on a Gaussian kernel to reduce noise and produce a smoother surface.

Finally, we use the surf() function to display the smoothed surface.

gistlibby LogSnag