Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to produce a interpolated smooth 3d plot in Matlab

Tags:

matlab

This plot is created by Mathematica:

ls = Table[Sinc[x*y], {x, -5, 5, 0.2}, {y, -5, 5, 0.2}];
ListPlot3D[ls, InterpolationOrder -> 2, PlotRange -> All, 
 Mesh -> None]

enter image description here

How to create a plot like this in MatLab?

Here is my try so far:

>> x=linspace(-5.,5.,51);
>> y=linspace(-5.,5.,51);
>> [x,y]=meshgrid(x,y);
>> z=sinc(x.*y);
>> surf(x,y,z)
>> shading interp

It looks like very different, especially the details of the ripples. Is it possible to make a plot like the Mathematica one, especially the smoothness, shadows ?

enter image description here

like image 958
xslittlegrass Avatar asked May 30 '13 16:05

xslittlegrass


1 Answers

In order to create nice lighting and shadows, you need to add a light to your plot, and add some sort of face lighting. If the resolution is too low, then you will end up with a somewhat ragged plot, since the 'interp' style shading uses linear interpolation. For example

n = 51;
x=linspace(-5., 5., n);
y=linspace(-5., 5., n);
[x, y]=meshgrid(x, y);
sinc = @(x) sin(x)./x;
z=sinc(x.*y);
z(isnan(z)) = 1;
surf(x, y, z, 'LineStyle', 'none', 'FaceColor', 'interp')
colormap(cool)
camlight right
set(gca, 'CameraPosition', [45 35 9.8])

which produces the following

enter image description here

Note that how smooth the surface appears is related to n. Larger values of n will increase the smoothness of the surface.

If the data you produce is expensive to create, you can increase the resolution by using a more advanced form of interpolation than linear, as follows

n = 51;
x=linspace(-5., 5., n);
y=linspace(-5., 5., n);
[x, y]=meshgrid(x, y);
sinc = @(x) sin(x)./x;
z=sinc(x.*y);
z(isnan(z)) = 1;

nn = 401;
xi = linspace(-5.0, 5.0, nn);
yi = xi;
[xi, yi] = meshgrid(xi, yi);
zi = interp2(x, y, z, xi, yi, 'spline');
surf(xi, yi, zi, 'LineStyle', 'none', 'FaceColor', 'interp')
colormap(cool)
camlight right
set(gca, 'CameraPosition', [45 35 9.8])

which produces the following image

enter image description here

See the following help pages for more details

  • Surface Properties

  • camlight

  • Coloring Mesh and Surface Plots

  • interp2

like image 113
hoogamaphone Avatar answered Oct 02 '22 09:10

hoogamaphone