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 CenterFile Exchange에서 Construction에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by