Hello!
I am trying to plot a function that has a certain y value when it is between two variables, but I keep getting an error that reads:
Operands to the || and && operators must be convertible to logical scalar values.
for this line:
if (0<=t) && (t<=e)
Please help!
e = 1;
t=linspace(0,10);
l=numel(t);
for i=1:l
if (0<=t) && (t<=e)
y(i) = (pi/(2*e))*sin((pi/e)*t);
else
y(i) = 0;
end
plot(t,y)
end

 채택된 답변

Star Strider
Star Strider 2019년 2월 21일
The double logic operators (&&, ||) are short-circuit operators and operate only on logical values. They will not work in your situaiton.
Try ‘logical indexing’ instead:
y = @(t,e) (pi/(2*e))*sin((pi/e)*t) .* (0<=t) & (t<=e);
e = 1;
t=linspace(0,10);
figure
plot(t,y(t,e))

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기

질문:

2019년 2월 21일

답변:

2019년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by