Creating a line with different steps

1 view (last 30 days)
Joyce de Heer
Joyce de Heer on 13 Mar 2019
Edited: madhan ravi on 13 Mar 2019
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 Comment
madhan ravi
madhan ravi on 13 Mar 2019
Edited: madhan ravi on 13 Mar 2019
expected graph picture ?, you seem to be plotting a point, are you aware of stairs() ?

Sign in to comment.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 13 Mar 2019
Edited: KALYAN ACHARJYA on 13 Mar 2019
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 Comments
madhan ravi
madhan ravi on 13 Mar 2019
Yes but what is the remedy?
KALYAN ACHARJYA
KALYAN ACHARJYA on 13 Mar 2019
Edited: madhan ravi on 13 Mar 2019
@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

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!