Unit Step Funtion Without using heaviside function

11 ビュー (過去 30 日間)
Ashley Kim
Ashley Kim 2022 年 6 月 21 日
コメント済み: Walter Roberson 2022 年 6 月 21 日
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
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)
A = 20
Try creating one by referring to Heaviside step function. If you have trouble, come back again.
Walter Roberson
Walter Roberson 2022 年 6 月 21 日
hint: something >= 0

サインインしてコメントする。

回答 (1 件)

Jonas
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
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
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by