Unit Step Funtion Without using heaviside function

14 visualizaciones (últimos 30 días)
Ashley Kim
Ashley Kim el 21 de Jun. de 2022
Comentada: Walter Roberson el 21 de Jun. de 2022
How can I plot the u(t) = u(t-1)+u(t-2)+u(t-3)+u(t-4)-u(t+1)-u(t+2)-u(t+3)-u(t+4) ?
I would like to have the plot going up on both sides without using the heaviside func.
Thank you!
  4 comentarios
Sam Chak
Sam Chak el 21 de Jun. de 2022
I see. Thanks for your clarification. The general idea is to create a function that produce the same output like the Heaviside function. For example, let's create a SAM function (square and minus) 😄
sam = @(x) x.^2 - x;
A = sam(5)
A = 20
Try creating one by referring to Heaviside step function. If you have trouble, come back again.
Walter Roberson
Walter Roberson el 21 de Jun. de 2022
hint: something >= 0

Iniciar sesión para comentar.

Respuestas (1)

Jonas
Jonas el 21 de Jun. de 2022
Editada: Jonas el 21 de Jun. de 2022
you could go over the integration of the delta function, which is the heaviside function:
t=-5:5;
ds=[0 1 1 1 1 0 -1 -1 -1 -1 0];
s=cumsum(ds);
stairs(t,s)
edit: ok, i think ds needs to be multiplied with -1:
ds=[0 -1 -1 -1 -1 0 1 1 1 1 0];
  2 comentarios
Sam Chak
Sam Chak el 21 de Jun. de 2022
Yup, can't find any difference between them.
% Code directly using Heaviside function
t = linspace(-5, 5, 10001);
u = heaviside(t - 1) + heaviside(t - 2) + heaviside(t - 3) + heaviside(t - 4) - heaviside(t + 1) - heaviside(t + 2) - heaviside(t + 3) - heaviside(t + 4);
plot(t, u, 'linewidth', 1.5)
grid on
xlabel('t')
% Code indirectly depending on Heaviside function
A = gradient(u);
k = 1;
for j = 1:1000:10001
dS(k) = 2*A(j);
k = k + 1;
end
S = cumsum(dS); % integration of the delta function
T = -5:5;
stairs(T, S, 'r-', 'linewidth', 1.5)
grid on
xlabel('t')
Jonas
Jonas el 21 de Jun. de 2022
there may be no visual difference but the difference lies in the detail. Heaviside is defined as 0.5 at 0 and the cumsum version is already 1 there.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by