Plotting a unit step function without heaviside.

조회 수: 213 (최근 30일)
Hannah Chamberlain
Hannah Chamberlain 2017년 2월 4일
답변: Sam Chak 2024년 7월 17일
So for my class I need to be able to plot
Xg(t)= u(t+1)-2u(t-1)+u(t-3)
Xh(t)=(t+1_u(t-1)-tu(t)-u(t-2)
and a whole other host of things but for these ones I'm confused on how to do it without the heaviside function. I got an answer for just u(t) was:
t = (-1:0.01:5)';
unitstep = t>=0;
plot(t,unitstep)
This worked.
When I tried to get it to shift instead the line became more of a ramp function.
t = (-1:0.01:5)';
unitstep = t>=0;
u1 = unitstep.*(t+1)
plot(t,u1)
What am I doing wrong?
  댓글 수: 2
Ulfah Nizam
Ulfah Nizam 2018년 12월 19일
what does 0.01 means in
t =(-1:0.01:5)';
Adam Turton
Adam Turton 2019년 10월 3일
Matlab has an issue with jump discontinuities, so 0.01 makes it so that it "updates" the function to the correct placement, allowing what would otherwise plot as a ramp function to show as a near vertical line.

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

채택된 답변

Chad Greene
Chad Greene 2017년 2월 4일
편집: Chad Greene 2017년 2월 4일
Hi Hannah,
Your unitstep contains only zeros and ones. So when you plot
plot(t,unitstep)
it's a Heaviside function, just as you expect. But when you multiply unitstep by t, you end up plotting zeros wherever unitstep is zero, and the values of t (not ones!) wherever unitstep is one. If you're trying to move a simple Heaviside function left or right, try this:
t = (-1:0.01:5)';
% Start with all zeros:
unitstep = zeros(size(t));
% But make everything corresponding to t>=1 one:
unitstep(t>=1) = 1;
plot(t,unitstep,'b','linewidth',3)
% Repeat, with everything shifted to the right by 1 unit:
unitstep2 = zeros(size(t));
unitstep2(t>=2) = 1;
hold on
plot(t,unitstep2,'r:','linewidth',2)
box off
  댓글 수: 5
Hannah Chamberlain
Hannah Chamberlain 2017년 2월 6일
Thanks so much for the help. I think it wasn't working earlier because the value of t was too small. The only thing I still need to do is be able to add the functions together. So I need to do Xg(t)= u(t+1)-2u(t-1)+u(t-3). Here is my attempt. I don't think this is what it's suppose to look like.
t = (-2:0.01:5)';
% Start with all zeros:
unitstep = zeros(size(t));
% But make everything corresponding to t>=1 one:
unitstep(t>=-1) = 1;
%plot(t,unitstep,'b','linewidth',3)
% Repeat, with everything shifted to the right by 1 unit:
unitstep2 = zeros(size(t));
unitstep2(t>=1) = 1;
%hold on
unitstep3 = zeros(size(t));
unitstep3(t>=3) = 1;
Ut= unitstep+unitstep2+unitstep3;
plot(t,Ut)
axis([-2 5 0 4])
Chad Greene
Chad Greene 2017년 2월 7일
Perhaps you wanted to multiply the unitsteps together rather than add them?

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

추가 답변 (3개)

Les Beckham
Les Beckham 2017년 2월 6일
You are very close to the first half of your goal.
Your line of code
Ut= unitstep+unitstep2+unitstep3;
should, I believe, reflect what you are calling Xg in your original problem. You might want to rename it to help you remember the connection between the code and the original equation. Also, you need to look at what the multipliers/coefficients are in your original Xg definition. u(t-1), which you are calling unitstep2 in your code, is multiplied by -2 when assembling the total Xg. This is not reflected in your code.
I think you can figure it out from here.
  댓글 수: 2
Hannah Chamberlain
Hannah Chamberlain 2017년 2월 7일
Thank you so much for you help!! It was enough that I was able to finish the rest of the assignment on my own.
Les Beckham
Les Beckham 2017년 2월 8일
You are quite welcome.

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


abdelkader omr
abdelkader omr 2024년 7월 17일
I want to plot a unit step function
x axis represenets time and its value [0 1 2 3 4 5 6 7]
y axis represents load and its value are[2 2 3 3 3 3 2 2]

Sam Chak
Sam Chak 2024년 7월 17일
Your function looks somewhat like a Boxcar function. However, your description of the continuous-time function is slightly unclear because they are discrete values. There are simpler methods for continuous-time, but since yours are discrete values, perhaps you may consider flexibly creating the function as follows:
%% Boxcar function
function y = boxcar(x, params)
for i = 1:length(x)
if x(i) <= params(1)
y(i) = 2;
elseif x(i) <= params(2)
y(i) = 2;
elseif x(i) <= params(3)
y(i) = 2;
elseif x(i) <= params(4)
y(i) = 3;
elseif x(i) <= params(5)
y(i) = 3;
elseif x(i) <= params(6)
y(i) = 3;
elseif x(i) <= params(7)
y(i) = 2;
elseif x(i) <= params(8)
y(i) = 2;
else
y(i) = 2;
end
end
end
%% Plot the function
x = linspace(0, 10, 1001);
y = boxcar(x, [0 1 2 3 4 5 6 7]);
plot(x, y, 'linewidth', 2), grid on, ylim([1 4])
xlabel('Time'), ylabel('Load'), title('Boxcar function')

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by