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

2 visualizaciones (últimos 30 días)
현석 송
현석 송 el 6 de Oct. de 2021
Comentada: KSSV el 6 de Oct. de 2021
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?

Respuestas (1)

KSSV
KSSV el 6 de Oct. de 2021
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 comentarios
현석 송
현석 송 el 6 de Oct. de 2021
i need to use for if statment... :(.....
KSSV
KSSV el 6 de Oct. de 2021
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

Iniciar sesión para comentar.

Categorías

Más información sobre Construction en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by