Piecewise function with large time vector

9 ビュー (過去 30 日間)
Jayden Riley
Jayden Riley 2020 年 9 月 30 日
コメント済み: madhan ravi 2020 年 9 月 30 日
I am having issues creating a piecewise function. I have coded the piecewise function but MatLab says (t) is set by a nonscalar operator, and gives me "Operands to the || and && operators must be convertible to logical scalar values." when I try to run the code.
clear,clc
t=0:0.001:20; %time in seconds
g = piecewise((0<t)&&(t<2),t/2,(2.001<t)&&(t<4),4./(t.^2),(4.001<t)&&(t<20),0.25);
this is what I have coded. How do I fix this issue?
Thank you.

回答 (1 件)

madhan ravi
madhan ravi 2020 年 9 月 30 日
g = ((0<t)&(t<2)) .* t/2 ...
+((2<=t)&(t<4)) .* (4./(t.^2))...
+((4<=t)&(t<20)) *0.25;
  1 件のコメント
madhan ravi
madhan ravi 2020 年 9 月 30 日
Or if you have the Symbolic Math Toolbox:
syms t
g = piecewise((0<t)&(t<2),t/2,(2<=t)&(t<4),4./(t.^2),(4<=t)&(t<20),0.25);

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by