what is the purpose of u1 and u2 in this code?

6 views (last 30 days)
t=-1:0.01:6;
u1=[zeros(1,100),ones(1,601)];
u2=[zeros(1,600),ones(1,101)];
u=u1-u2;
x=10*u;
plot(t,x)

Accepted Answer

Star Strider
Star Strider on 5 Dec 2020
They create (1x701) vectors with different numbers of zeros and ones.
The overall effect (that is easily seen if plotted) is to create a square pulse that is equal to 1 from 0 to 5, and 0 elsewhere.
(There are easier and more efficient ways to do this.)
  3 Comments
Star Strider
Star Strider on 5 Dec 2020
Sure!
The easiest way I can code:
t=-1:0.01:6;
sqwv = (t >= 1) & (t <= 5);
figure
plot(t, sqwv)
grid
ylim(1.1*ylim)
producing the same result as the original code in your Question.
The logical vector created in ‘sqwv’ is converted to numeric when used in a calculation (such as in an argument to the plot function).

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 5 Dec 2020
They are unit step functions with different initial times.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!