Hello,
I am trying to create this piecewise function but there is an error: 'Incorrect number or types of inputs or outputs for function 'piecewise'.
Error in Q2 (line 7)
x1 = piecewise(ABSt<=1, 1-ABSt,0,0);'
this is the code:
t = linspace(-5,5,1001);
ABSt = abs(t)
x1 = piecewise(ABSt<=1, 1-ABSt,0,0);
I am trying to creat x1 = 1-abs(t) while abs(t) is smaller or equal to 1, otherwise x1=0
is anyone knows what is wrong and how to fix it?
thank you!

 채택된 답변

Torsten
Torsten 2022년 11월 16일
편집: Torsten 2022년 11월 16일

0 개 추천

"piecewise" works on symbolic arguments.
Define
t = linspace(-5,5,1001);
x1 = (1-abs(t)).*(abs(t)<=1);
plot(t,x1)
instead.

댓글 수: 3

Engi
Engi 2022년 11월 16일
Thank you, it worked.
Just to understand the logic behind it, (I am pretty new in Matlab) - why shall we not write 'picewise' before (1-abs(t)).*(abs(t)<=1);?
and why is the .* makes it work as we wanted?
As I wrote, you can define piecewise functions for symbolic inputs:
syms t
f = piecewise(abs(t)<=1,1-abs(t),0)
f = 
fplot(f)
but this doesn't work for numerical inputs for t.
The .* makes it work because you must multiply the numerical vector (1-abs(t)) with the logical vector (abs(t)<=1) elementwise.
The product as written means: multiply the element 1-abs(t(i)) with 1 (for true) if abs(t(i))<=1, else with 0 (for false). The result is a vector x1 that is 1-abs(t(i)) at position i if abs(t(i)) <=1 and 0 else.
Engi
Engi 2022년 11월 19일
Alright, thank you for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2022b

태그

질문:

2022년 11월 16일

댓글:

2022년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by