Creating a line with different steps

1 回表示 (過去 30 日間)
Joyce de Heer
Joyce de Heer 2019 年 3 月 13 日
編集済み: madhan ravi 2019 年 3 月 13 日
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
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
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 件のコメント
madhan ravi
madhan ravi 2019 年 3 月 13 日
Yes but what is the remedy?
KALYAN ACHARJYA
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 ExchangeGraphics Performance についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by