Unit Step Funtion Without using heaviside function
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
Sam Chak
2022-6-21
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)
回答(1 个)
Jonas
2022-6-21
编辑:Jonas
2022-6-21
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 个评论
Sam Chak
2022-6-21
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
2022-6-21
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!