Plotting perpendicular lines but they are not perpendicular.
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Yannick Ongena
le 15 Jan 2024
Modifié(e) : John D'Errico
le 15 Jan 2024
I have this simple snippet:
syms x;
y = 3*x-1;
z = (-1/3)*x-7;
fplot(y)
title("Perpendicular Lines")
grid on;
hold on;
fplot(z)
hold off;
But as you can see these do not show up as perpendicular. Why is this?
Their slope multiply to -1 so they are perpendicular but still they do not appear to be graphing as perpendicular...
I tried with other perpendicular lines and it never works... Only with slope 1 and -1 does it seem to work but with any other slope, it doesn't.
0 commentaires
Réponse acceptée
Stephen23
le 15 Jan 2024
Modifié(e) : Stephen23
le 15 Jan 2024
"But as you can see these do not show up as perpendicular."
They are perpendicular: check the axes scales!
If you want the X and Y axes to have equal displayed unit lengths then you need to specify that:
syms x;
y = 3*x-1;
z = (-1/3)*x-7;
fplot(y)
title("Perpendicular Lines")
grid on;
hold on;
fplot(z)
axis equal
Plus de réponses (2)
Voss
le 15 Jan 2024
The lines don't appear perpendicular because the data aspect ratio of the axes (i.e., the number of pixels on the screen one unit in the y-direction takes vs one unit in the x-direction) is not 1:1. You can make it 1:1 using axis equal and then the lines appear perpendicular:
syms x;
y = 3*x-1;
z = (-1/3)*x-7;
fplot(y)
title("Perpendicular Lines")
grid on;
hold on;
fplot(z)
hold off;
axis equal % set axes DataAspectRatio to [1 1 1]
2 commentaires
Stephen23
le 15 Jan 2024
"Is there also a way to make the graph more square by setting the interval for y to for example [-10 10]."
John D'Errico
le 15 Jan 2024
Modifié(e) : John D'Errico
le 15 Jan 2024
Both @Stephen23 and @Voss have explained the issue perfectly, so I am just adding some additional information to clear things up. (Accept one of their answers is my suggestion.) It is a common problem I see, often when someone plots what they think should be a circle, and sees it looks like an ellipse. It is just axis scaling though, and MATLAB does not choose an equal scaling on the axes, not by default.
syms x;
y = 3*x-1;
z = (-1/3)*x-7;
fplot(y)
title("Perpendicular Lines")
grid on;
hold on;
fplot(z)
hold off;
Now look closely at the x axis. It goes from -5 to 5, but y goes from -16 to +14, or so. And even at that, look at the shape of the figure window itself. It is not square. So what happens is the lines that you thought were perpendicular do not appear as if they are. Now, I'll replot the figure, but add one more command.
fplot(y)
title("Perpendicular Lines")
grid on;
hold on;
fplot(z)
hold off;
axis equal
And indeed, having forced MATLAB to use the same spacing in x as there is in y, the lines now appear perfectly perpendicular. They always were so of course. But appearances can be deceiving.
So why did that happen? By default, fplot uses limits on x of [-5,5] You can see that in the plot. But when you do that, the blue line forces the vertical axis to be much longer. It spans a range of 3 times as much as the x axis. And you can deduce that just from the slope of that line.
Why did this not happen when the slopes were -1 and 1? There the two axes will span the same ranges. So there is no issue.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




