How to define and plot a piecewise time dependent function

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Star Strider
Star Strider el 21 de Feb. de 2019
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))

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by