I want to calculate the convolution sum of the continuous unit pulse with itself, but I don't know how I can define this function $$ \delta(t) = \begin{cases} 1, 0 \le t \lt 1 \\ 0, otherwise \end{cases}$$ in Matlab.
2026-03-30 02:04:01.1774836241
On
On
How to define a piecewise function in matlab?
19.1k Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail At
3
There are 3 best solutions below
0
On
Create a MATLAB function with the following code
function d = delta(t)
if t >= 1
d = 0;
else if t < 0
d = 0;
else
d = 1;
end
end
And use delta for whatever reasons you want.
0
On
For symbolic math you can take advantage of MuPAD within Matlab. See the documentation for piecewise. You can use this function to concisely produce the example in your question:
pw = evalin(symengine,'piecewise([t >= 0 and t < 1, 1],[Otherwise, 0])')
And you can evaluate it for vector inputs using subs like this:
subs(pw,'t',[1/2 1 0])
which returns
ans =
[ 1, 0, 1]
See the documentation for piecewise for more examples of how to define piecewise functions. There are other ways to call MuPAD functions from Matlab – see here. Of course this method is not meant for performance, so you shouldn't rely on it being as fast as floating-point methods.
Use the rectangularPulse function.
Or you can define arbitrary piecewise functions in a number of ways, e.g.
ix=x<0
y(ix)=0
ix=x>=1
y(ix)=0
ix=0<=x&x<1
y(ix)=1