3,226 views (last 30 days)

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?

Chad Greene
on 4 Feb 2017

Edited: Chad Greene
on 4 Feb 2017

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

Chad Greene
on 6 Feb 2017

Run this:

% Define some nominal unit step:

t = (-1:0.01:5)';

unitstep = zeros(size(t));

unitstep(t>=1) = 1;

% Plot the unitstep twice:

plot(t,unitstep,'k-','linewidth',2)

axis([-2 5 0 1.5])

hold on

h = plot(t,unitstep,'b');

% Change the location of the unitstep:

for k = 0:30:1000

% Create a new unitstep:

newunitstep = zeros(size(t));

newunitstep(t>=(1.5*sind(k)+1)) = 1;

% Update the blue line with the new unitstep:

set(h,'ydata',newunitstep)

% Pause 0.2 seconds so you can see it:

pause(0.2)

end

Chad Greene
on 7 Feb 2017

Perhaps you wanted to multiply the unitsteps together rather than add them?

Les Beckham
on 6 Feb 2017

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.

sandeep singh
on 27 Mar 2018

Edited: Chad Greene
on 19 Apr 2018

plot(t,unitstep)

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

Opportunities for recent engineering grads.

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/323383-plotting-a-unit-step-function-without-heaviside#comment_557736

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/323383-plotting-a-unit-step-function-without-heaviside#comment_557736

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/323383-plotting-a-unit-step-function-without-heaviside#comment_652290

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/323383-plotting-a-unit-step-function-without-heaviside#comment_652290

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/323383-plotting-a-unit-step-function-without-heaviside#comment_752270

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/323383-plotting-a-unit-step-function-without-heaviside#comment_752270

Sign in to comment.