Plotting perpendicular lines but they are not perpendicular.

조회 수: 3 (최근 30일)
Yannick Ongena
Yannick Ongena 2024년 1월 15일
편집: John D'Errico 2024년 1월 15일
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.

채택된 답변

Stephen23
Stephen23 2024년 1월 15일
편집: Stephen23 2024년 1월 15일
"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
Yannick Ongena
Yannick Ongena 2024년 1월 15일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Voss
Voss 2024년 1월 15일
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
Yannick Ongena
Yannick Ongena 2024년 1월 15일
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.
Stephen23
Stephen23 2024년 1월 15일
"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
John D'Errico 2024년 1월 15일
편집: John D'Errico 2024년 1월 15일
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.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by