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

2 次查看(过去 30 天)
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 个评论
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 CenterFile Exchange 中查找有关 Construction 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by