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

2 vues (au cours des 30 derniers jours)
현석 송
현석 송 le 6 Oct 2021
Commenté : KSSV le 6 Oct 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?

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Construction dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by