Reputation: 4966
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 ?
Upvotes: 9
Views: 22578
Reputation: 1102
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
Upvotes: 15
Reputation: 2201
I suggest try surf/surfl and try turn on/off lighting. Initial camera position is also important because I use "headlight" cam.
x=linspace(-5.,5.,51);
y=linspace(-5.,5.,51);
[x,y]=meshgrid(x,y);
z=sinc(x.*y);
surfl(x,y,z) %surf(x, y, z)
shading interp
colormap cool
%camlight headlight
%lighting gouraud
Upvotes: 3