필터 지우기
필터 지우기

Plotting just x = 1 over an x and y graph

조회 수: 8 (최근 30일)
Erin W
Erin W 2016년 10월 13일
댓글: Erin W 2016년 10월 13일
Howdy howdy howdy,
I have this following code which gives me two out of the three lines I need:
a = 2;
x = linspace(1,100,10);
for i=1:length(x)
y1(i) = 0*(i);
y2(i) = ((a-i).*(1+i))/i;
end
figure
plot(x,y1,x,y2)
I need to plot an additional line on the same plot. That line needs to be x = 1/(a-1) = 1.
Can someone help me?
Cheers! E

채택된 답변

Walter Roberson
Walter Roberson 2016년 10월 13일
yl = get(gca, 'YLim');
line( [1 1], yl )
This would draw a vertical line the height of the axes. (Note: if you later make the axes taller, the line will not automatically adjust itself.)
  댓글 수: 1
Erin W
Erin W 2016년 10월 13일
Beautiful, thank you so much!

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

추가 답변 (1개)

Guillaume
Guillaume 2016년 10월 13일
a = 2;
x = 1:linspace(1, 100, 10);
y = 1:numel(x);
plot(x, [zeros(size(x)); (a-y).*(1+y)./y; repmat(1/(a-1), 1, numel(x))]);
is probably the simplest. The loop is certainly not required.
I don't particularly see the point of the brackets in 0*(i), and even less the point of the multiplication, since y1(i) = 0 would convey exactly the same with better readability.
In your y2 expression, a and i are both scalar, so .* is the same as * and ./ is the same as /, but really you should be consistent.
  댓글 수: 1
Erin W
Erin W 2016년 10월 13일
It's odd because I was getting an error until I put that period in. But MATLAB goofs a lot for me.
I just was playing around with things. I'm not a great coder by any means. But thank you. You have been extremely helpful.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by