How to write piecewise function in symbolic form?

I have to make a piecewise function in symbolic form using the following:
-2 <= t <=-1, t+2,
-1 < t <=1, 1,
1<t <=2, -t+2
otherwise 0
i tried the code below but it seems to be giving me errors. Any idea what I'm doing wrong?
syms x(t)
x(t) = piecewise(-2 <=t && t <=-1, t+2,-1 < t && t <=1,1,1<t && t<=2,-t+2 ,0);
fplot (x(t))

回答 (1 件)

Paul
Paul 2021 年 9 月 23 日

0 投票

Use &, not &&
syms x(t)
x(t) = piecewise(-2 < t & t <= -1 , t+2, -1 < t & t <= 1, 1, 1 < t & t <= 2, -t+2, 0)
x(t) = 
fplot(x(t),'-o')

質問済み:

2021 年 9 月 22 日

回答済み:

2021 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by