Surf plot and the slices do not match
显示 更早的评论
I am trying to do a surface plot of a 3D function
at slice
(which corresponds to time = 1). To do so I create a grid with
and
. Here's the code:
[W, Theta] = meshgrid(grid(1, :), grid(2, :));
surf(W, Theta,reshape( u(2, :, :), N, N), 'EdgeColor','none');
xlabel("$x$", 'Interpreter','latex');
ylabel("$p$", 'Interpreter','latex');
I obtain:

Notice this discontinuity at x = 1.5 for example along the p-axis. This is a close up view from above, and one from the side.

However, if I plot just a slice using the normal plot function in matlab there is no discontinuity. Here's the code and the plot:
plot(grid(2, :), reshape(u(2, 40, :), 50, 1)); xlabel('p'); ylabel('u(x = 1.59, p)')

here index 40 corresponds to x = 1.59.
Does anybody know what is going on in here? Any help/hint would be greatly appreciated!!
回答(1 个)
Bruno Luong
2023-9-5
编辑:Bruno Luong
2023-9-5
1 个投票
You mix grids, with meshgrid and surf:
- grid(;,1) => 3rd dimension of u, W, you label it x
- grid(;,2) => 2nd dimension of u, THETA, you label it p, and describe it as y !!!
But then you plot using plot you considered then
- the 3rd dimension of u for p
which is incoherent with the surface plot. The plot wit plot is for p constant with x varies, assuming the surface is correct.
Also your variable naming is not coherent and all over the places : W, x, THETA, p, y, grid(:,1), grid(:,2) etc... There is some basic discipline in naming in science that you need to learn and apply if you don't want to waste your time for such problem.
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!