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!!

Answers (1)

Bruno Luong
Bruno Luong on 5 Sep 2023
Edited: Bruno Luong on 5 Sep 2023
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.

Asked:

on 5 Sep 2023

Commented:

on 5 Sep 2023

Community Treasure Hunt

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

Start Hunting!