Plotting's issue that cannot explain

Hello everyone,
I got a trouble with plot3 and surf.
I plot a surface and 2 lines on the surface. Everything is okay until this weird happening. In my code, if I change the option of line's style is solid for red one, the line will disappear. But if I change it into 'x' or '+', then it works. The problem just affects on the red one but not the pink one. I cannot explain why.
I am looking forward to hearing from you.
Thank you in advance.
close all; clc; clear
alpha = 5.5;
[y,x] = meshgrid(-50:0.1:50);
r=(y.^3 - 3*y.^2 + y + 3);
s=-(x.^3 - 3*x.^2 + x + 3);
Pnpo = 3*x.*x.*y - x.*y + 2*y + x - 7;
Pnpor = 3*r.*r.*y - r.*y + 2*y + r - 7;
Pnpos = 3*x.*x.*s - x.*s + 2*s + x - 7;
m = surf(y,x,Pnpo,'FaceAlpha',0.7);
m.EdgeColor = 'none';
xlim([-50 50])
ylim([-50 50])
zlim([-6000 6000])
xlabel('y')
ylabel('x')
zlabel('z')
hold on
plot3(s,x,Pnpos,'-m','LineWidth',2) % pink line
hold on
%plot3(y,r,Pnpor,'-r','LineWidth',2); % red line ---> the line disappears
plot3(y,r,Pnpor,'+r','LineWidth',2); % red line ---> the line appears

2 Comments

Stephen23
Stephen23 on 7 Mar 2020
Edited: Stephen23 on 7 Mar 2020
Note to future readers: the accepted answer does not explain the underlying issue, and does not resolve it correctly.
See Walter Roberson's answer instead.
Nuec Tah
Nuec Tah on 7 Mar 2020
Edited: Nuec Tah on 7 Mar 2020
@Stephen Cobeldick: I really know that the answer of Walter Roberson is correct. Both of you and @Ameer Hamza also agree with that. But the problem is, the method of Walter makes a lot of loops, it makes the time of execution is much longer than Ameer's. That's why I decide to use method of Ameer instead of Walter. I know it's not really suitable, but it's still fine for my case. I changed the accepted answer.

Sign in to comment.

 Accepted Answer

[y,x] = meshgrid(-50:0.1:50);
r=(y.^3 - 3*y.^2 + y + 3);
Notice that r depends only on y. r is a mapping of y.
Pnpor = 3*r.*r.*y - r.*y + 2*y + r - 7;
Notice that Pnpor depends upon y, and upon r, and as per above, r depends only on y. Pnpor will be the same for either all rows or all columns, depending on the orientation of y.
plot3(y,r,Pnpor,'+r','LineWidth',2); % red line ---> the line appears
You are passing in y as the first parameter, and y is 2D. y will be used as the first independent variable, along the x axis. When 2D arrays of values are given for x, then the lines are connected along the columns -- one line will be generated for each column of x. On the display, you will only be able to see lines drawn in which at least two adjacent x values (adjacent rows) in the same column are in xlim range, and the corresponding rows and columns in the second independent variable (r in this case) are in ylim range, and in the corresponding rows and column of the dependent variable (Pnpor in this case) are in zlim range. Furthermore, in order to produce a mark on the screen, something in the coordinates must be different from the other corodinates
We now have to ask whether there are two adjacent rows in the same column of the first independent variable that are in xlim range, and simultaneously the same rows/one-column combinations in the second independent variable are within ylim range, and simultaneously the same rows/one-column combination of the independent variable are within zlim range.
The answer to that question turns out to be "Yes", such things occur. So why no marks on the screen?
It is because of that extra condition: that something in the coordinates must be different . With the layout you have for y, and the fact that r depends only on y, and that Pnpor depends only on y and r and so depends only on y, then it turns out that for any given column in y, and the corresponding column in r, and the corresponding column in Pnpor, all of the values are the same . So nothing is drawn on the screen.
Try
plot3(y.',r.',Pnpor.','-r','LineWidth',2); % red line ---> the line appears
Note that this will end up drawing the same line 1001 times...

1 Comment

@Walter Roberson : Thank you so much. This technique I really never knew and thought of.

Sign in to comment.

More Answers (1)

In your code y, r, and pnpor are 2D matrices with dimensions 1001x1001. line3 function is creating 1001 lines, and each individual line is so small that it is invisible. Following will fix this issue
plot3(s(:),x(:),Pnpos(:),'-m','LineWidth',2) % pink line
% hold on
p = plot3(y(:),r(:),Pnpor(:),'-r','LineWidth',2); % red line ---> the line disappears

3 Comments

Using (:) will join the parts incorrectly.
The lines are not drawn too small to be invisible: the LineWidth would ensure that they were drawn at constant number of screen pixels no matter what the zoom level was. The lines really are not drawn at all. See my Answer for the reason.
@Ameer Hamza : Thank you. It worked.
The reasoning in Walter's answer is correct. The corresponding columns of y, r, and Pnpos are mapping to a single point in the 3D graph, and there is no line at all. With this context, the most efficient solution will be
plot3(y(1,:),r(1,:),Pnpor(1,:),'-r','LineWidth',2);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!