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]
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 ?
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
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
See the following help pages for more details
Surface Properties
camlight
Coloring Mesh and Surface Plots
interp2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With