How do I draw a graph using a for if statement?

2 ビュー (過去 30 日間)
현석 송
현석 송 2021 年 10 月 6 日
コメント済み: KSSV 2021 年 10 月 6 日
question is this
(20>=x>=-10)
for t=1:0.01:30
x(t)=t-11;
if x(t)<0;
y(t)=5;
elseif 0<=x(t) & x(t)<10
y(x-11)=5*x+5;
else x(t)>10
y(x(t))=5*sqrt(10*(x(t))+5);
end
end
x=-10:0.01:20;
plot(t,y)
but i get this answer
Array indices must be positive integers or logical values.
so... how i solve this problem?

回答 (1 件)

KSSV
KSSV 2021 年 10 月 6 日
x = 0:0.01:30 ;
y = zeros(size(x)) ;
y(x<0) = 5 ;
idx = 0 >= x & x <10 ;
y(idx) = 5*x(idx)+5 ;
y(x >= 10) = 5*sqrt(10)*x(x >= 10)+5 ;
plot(x,y)
  2 件のコメント
현석 송
현석 송 2021 年 10 月 6 日
i need to use for if statment... :(.....
KSSV
KSSV 2021 年 10 月 6 日
x = 0:0.01:30 ;
y = zeros(size(x)) ;
for i =1:length(x)
y(i) = myfunc(x);
end
plot(x,y)
y = myfunc(x)
if x <0
y = 5 ;
elseif 0 >= x && x <10 ;
y = 5*x+5 ;
elseif x >= 10
y = 5*sqrt(10)*x+5 ;
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConstruction についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by