How to define and plot a piecewise time dependent function

4 ビュー (過去 30 日間)
Robert Fenton
Robert Fenton 2019 年 2 月 21 日
回答済み: Star Strider 2019 年 2 月 21 日
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 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by