Creating a line with different steps

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 个评论

expected graph picture ?, you seem to be plotting a point, are you aware of stairs() ?

请先登录,再进行评论。

回答(1 个)

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 个评论

Yes but what is the remedy?
@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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Graphics Performance 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by