Creating a line with different steps
조회 수: 1 (최근 30일)
이전 댓글 표시
The idea is to plot some kind of staircase with sloped steps. I made this code, it runs but it doesn't plot a graph, any ideas what went wrong?
h=5
for x = [0.2:0.2:100]
if x == [0.2:1:100]
y = h*x
elseif x == [0.4:1:100]
y= h*x
elseif x == [0.6:1:100]
y = h*x
elseif x == [0.8:1:100]
y= h*x - 0.2*h
else x == [1:1:100]
y= h*x-0.4*h
end
end
plot(x,y)
댓글 수: 1
madhan ravi
2019년 3월 13일
편집: madhan ravi
2019년 3월 13일
expected graph picture ?, you seem to be plotting a point, are you aware of stairs() ?
답변 (1개)
KALYAN ACHARJYA
2019년 3월 13일
편집: KALYAN ACHARJYA
2019년 3월 13일
it runs but it doesn't plot a graph, any ideas what went wrong?
You can check the x and y value-
x =
100.000000000000e+000
>> y
y =
498.000000000000e+000
It's having single point, it just plot one point, that why not visible as line or graph.
From the statement
for x =[0.2:0.2:100] get last iteration value x=100 and from any one if else statement single y valie is assigned.
See the point
plot(x,y,'*','linewidth',30);

Hope your doubt is cleared.
댓글 수: 2
KALYAN ACHARJYA
2019년 3월 13일
편집: madhan ravi
2019년 3월 13일
@Joyce You can do that in multiple ways, I have tried the way what you have tried so far. Please do the needful change as your required output.
x=[0:0.1:10];
h=2;
for i=1:length(x);
if x(i)<1
y(i)=h*x(i);
elseif x(i)>=1 & x(i)<2
y(i)=h*x(i)+3*h;
elseif x(i)>=2 & x(i)<3
y(i)=h*x(i)+4*h;
elseif x(i)>=3 & x(i)<4
y(i)=h*x(i)+5*h;
else
y(i)=h*x(i)+6*h;
end
end
plot(x,y)

You can change the shape of the curve (whether staircase or ramp) by changing the step and multiplication factor in 2nd part of expression 3*h. You can take the h as common and add constant value with x(i).. same thing.
Hope you get the idea and expecting it helps to way out from the issue.
Thanks and regards
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!