Meshgrid lighning and how to use contour around the plot

2 次查看(过去 30 天)
I've been trying to make a meshgrid with a certain lighting and two contours around the plot. But I'm stuck and help would be appreciated.
To make it easier to understand I'm trying to do this:
But my result is:
And below is the code I'm using for said result:
x=linspace(-2,2,1000);
y=x';
[X,Y]=meshgrid(x,y);
z=exp(-(X.^2+Y.^2));
surf(x,y,z);
shading interp
colormap summer
lighting gouraud
contour3(X,Y,Z,[0.8 0.8], 'g', 'LineWidth',1)
contour3(X,Y,Z,[0.2 0.2], 'r', 'LineWidth',1)
So the problem is how do I get the lighting right and how do I get the lines around.

采纳的回答

Cris LaPierre
Cris LaPierre 2022-10-4
编辑:Cris LaPierre 2022-10-4
You might find the settings in this example helpful. Not a perfect match, but here's what I came up with
x=linspace(-2,2,1000);
y=x';
[X,Y]=meshgrid(x,y);
z=exp(-(X.^2+Y.^2));
h=surf(x,y,z);
shading interp
colormap summer
lightangle(0,88)
h.FaceLighting= 'gouraud';
h.AmbientStrength = 0.3;
h.DiffuseStrength = 0.5;
h.SpecularStrength = 0.8;
h.SpecularExponent = 10;
h.BackFaceLighting = 'unlit';
hold on
contour3(X,Y,z,[0.8 0.8], 'g', 'LineWidth',1)
contour3(X,Y,z,[0.2 0.2], 'r', 'LineWidth',1)
hold off

更多回答(1 个)

Simon Chan
Simon Chan 2022-10-4
For the accurate solution, you need to find those x and y where z=0.2 and 0.8.
If an estimated solution is accepted, the positions on the grid close to this value can be selected by selecting a proper threshold.
However, if the threshold is too low, you will see a broken circle. On the other hand, if the threshold is too large, multiple circles will appear. So you need to choose the threshold carefully.
x=linspace(-2,2,1000);
y=x';
[X,Y]=meshgrid(x,y);
z=exp(-(X.^2+Y.^2));
s=surf(x,y,z);
shading interp
colormap summer
lighting gouraud
Th = 0.0015; % Threshold to find those positions close to the specific value
idx1 = abs(z-0.8)<Th;
xp = X(:);
yp = Y(:);
idxp = idx1(:);
hold on
c1 = scatter3(xp(idxp),yp(idxp),repelem(0.8,sum(idxp),1), 'g','SizeData',1);
idx2 = abs(z-0.2)<Th;
idxq = idx2(:);
c2 = scatter3(xp(idxq),yp(idxq),repelem(0.2,sum(idxq),1), 'r','SizeData',1);
hold off

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by