Plotting perpendicular lines but they are not perpendicular.

9 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 15 Jan 2024
Edited: Stephen23 on 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
  1 Comment
Yannick Ongena
Yannick Ongena on 15 Jan 2024
Ok, that makes sense. I thought it was something like that but wasn't able to put my finger on it...
Thanks; The axis equal is a great help for this!

Sign in to comment.

More Answers (2)

Voss
Voss on 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 Comments
Yannick Ongena
Yannick Ongena on 15 Jan 2024
Thanks that did work.
Is there also a way to make the graph more square by setting the interval for y to for example [-10 10]. I know some of the values will be outside the graph, but that's not a big deal.

Sign in to comment.


John D'Errico
John D'Errico on 15 Jan 2024
Edited: John D'Errico on 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.

Tags

Community Treasure Hunt

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

Start Hunting!