This is a short video about MATLAB trying to demonstrate its capabilities and how
it can be used in this course, but also I think you'll find it useful beyond just
the confines of this course, and actual practice as an engineer or in future
courses. You can see I am running MATLAB because
of what it says at the top of this title bar over here, on the right side.
That's the command window for MATLAB. There is a public domain version of
MATLAB, which is known as Octave. And, you can see the Octave can be
obtained at the GNU website. You can download your own copy of Octave
for free. It's, supports lots of different systems.
I'm, I happen to be running on a Macintosh, and that's the one I would
download if I were using it. The MATLAB command window is here over on
the right side, the really interesting thing about MATLAB is I don't have to
write a standalone program and then run it.
I can do a lot just typing commands to MATLAB at the keyboard, and that's what
I'm primarily showing today. Over on, here on the left side, is a
figure window, a plot window, and that's where all my plots are going to show up.
And I think you'll finally see why we're so enthusiastic about using MATLAB,
especially for this course. So every variable In MATLAB, is a vector,
or more generally a matrix. And this is extremely useful, as I want
to show you. So let me define a, a vector named x, and
I'm going to say that's equal to zero colon n.
And so this says, it says, this command says, set the variable x equal to a row
vector whose elements start at zero, increment by one, going from element to
element in the vector and wind up with the value of ten.
And if you just hit Return, you'll see that x has been defined to be that
variable, and sure enough it's a vector of the 11 elements, consistent of the
integer 0010. Well I can plot this against itself.
It' snot a very interesting plot, but I can do it, and the plot command in that,
in that tab is plot. This is what I would plot on the x-axis.
You can see the little help window that pops up in Matlab.
That won't pop up in Optic, but you really don't need it, eh, to do your
work. And I'm going to plot that against
itself. And you plot on the left side.
It won't change much because that was what I was setting up for, and you can
see that it's plotted a straight line as you would expect.
Well, I can plot x to and instead I'll make the vertical scale 5 times x.
So the usual syntax for doing multiplication, division, addition, and
everything still apply to variables in MATLAB.
So now I'm going to do something a bit fancier I'm going to plot x 5 times x
plus 2. The reason that's fancy is what is going
to happen when I add a, this is a vector, 5 times x is a vector, but 2 is a scalar.
And what does MATLAB do? Well it does the thing you want it to do.
It's going to add 2 to every element of 5 times x, and so when I finally, plot
this. You see the plot changes, and there's an
offset by two at the origin. So the vector plus a scalar is a vector
with the scalar added to every element of the vector.
Same for subtraction, etc. Now suppose I wanted to plot, Y = X^2.
Well first of all, what is this mean? Well, you cannot multiply a vector times
a vector. It doesn't work, and it's going to
complain because it, basically it's assuming everything is in matrix
multiplication. All the matrix multiplication rules
apply. But MATLAB has a very special syntax for
term by term multiplication. So, what this, see the dot in front of
the star? What that means instead of thinking about
x as a vector, and just doing vector times vector multiplication, this means
to term by term multiplication. So, since x consists of the integers zero
to ten, the y vector here should consist of the squares of every element, and
that's exactly what they are. And so this dot star is what you need, if
you want to apply the same, mathematical operation to every element of the vector.
And now I can plot, y versus x, and I get a parabola, as you would expect.
So let me label the axes. I always like to do that.
You label the x axis with the command x label and you enclose strings in MATLAB
with single quotes, not double quotes, it turns out, single quotes.
And there will pop up a label over here in the plot window, and I'll do one for
the y-axis. I'm not going to do anything fancy, just
calling it y. And you can see it actually rotates it,
counter-clockwise, so it looks pretty. Well, you've certainly, if you do
something like what is sine of two time pi times x over 5.
So pi is defined. It's already predefined for you and I
think this makes perfect sense. But what does sign of a vector mean?
All of the special functions in MATLAB are set up, so if you type this what this
means is term by term application of this function to the factor x.
And so what we get back is, you can see now the values are equal to sine of 2 pi
times the integers here of the 10 divided by 5.
It's not a very exciting result, but there it is.
Well you can see that what MATLAB does is that after a calculation, like the sine,
it gives you an answer. And it's going to tell you that, well you
may not want to see every detail of every calculation.
So what you can do is you type it in. If you end the last line with a
semicolon, nothing gets typed actually, but the calculations are still performed.
Notice it just performs the calculation, but it did not type them out for you.
So, I'm going to, from now on, use the semicolon, because I don't want to see
all the intermediate, calculations. Now, there's another very cool thing that
saves typing. Remember, use MATLAB a lot at the command
line, not necessarily to, to write a program for.
I'm going to type s i n, and I you can't see it but I'm going to type an up arrow
on my key board and it's going to bring up the previous command that began with s
i b. I'm going to go over here and say y
equals and hit return, and now the variable y if I just type it I can see
it's values. You can see it has stored all the values
of this previous calculation that I did before, and I can plot it.
So I'm going to bring up the plot command, I type pl up-arrow and there it
is, and it doesn't look much like a sine wave, but I can fix that.
So, I'm going to redefine x, I may say X and I am going, just going to go up
arrow. There is X label, hit up arrow again, and
there is my definition for X. I am going to add an argument here.
0.01 colon, so if you have an intermediate value in the colon notation
here, now the increment, instead of being 1 between the elements of the vector, now
it's going to be 0.01. And I don't really want to show you all
of those, so I'm going to type a semicolon out there and there's x.
I'm going to recalculate the sine function.
[SOUND] So, I'm going to be over the same interval, but now much more finely
represented in the plot command, gives me something that looks very much like a
sine. So, now you can see that you can really
do some very simple calculations or complicated calculations, at the
keyboard, getting plots and seeing what they look like.
Now I want to show you something else that's very, going to be very important.
suppose I want to plot 1 over x. So let's define y equals to 1 slash x.
Do you think this is going to work? And one is a scalar, don't forget that x
is a vector, and a scalar divided by a vector doesn't work very well, and you
get that same command again, guess what you type to fix this?
If I just hit an up arrow now, it's just going to bring up the previous line.
And I'm going to go over, type my dot, like I should have.
And I'm going to put a semi-colon out here because I certainly don't want to
see all these values of y here. Let's see, there's .01 times 10, so
there's 1000 elements there. And I don't really want to see them all.
So I'll do that, and now I'll bring up my plot command, and there it is.
It looks like a hyperbola. Well, those of you who know what I've
follow what I just did, know that y of one is 1 over 0, and guess what math lab
says it is, it is indeed infinity. So the way to access any element of a
vector is you give the name of the vector, and in parenthesis you put the
element you want to extract. If I wanted to extract the first 10
elements, guess what I use. I use the colon notation again, this time
always, since it's trying to index y, it always starts, the increments should be
1. So 1 colon 10 over 5 and in that lab, all
indexes for arrays start at 1, not 0 like they do in C.
So this gives me the first 10 elements and it should be one over X, going on a
very, fine way, a detailed way, and see it has explicitly knows infinity, and 1
over 0.01 is 100, etc. So the plot command is smart enough, and
if it sees infinity it doesn't plot it at all.
So that's why this plot, the vertical scale here, starts at 100.
And by the way all plots are auto-scaled, it's going to find the scale that makes
things fit. and you can change those if you look at
the details of how the Plot command works.
So, the getting help is easy for a given command like plot.
How do I change the axes, things like that.
You type like plot. And you will get something, that's, that
you can see. It's got lots and lots of detail here.
which you should go through when you're. First getting started with MATLAB.
Now, suppose I want to try something I want to calculate something that looks
like oh, let's say, x squared divided by sin of x.
So y equals x dot up arrow two. I think you now know what the dot means,
that means I'm going to take the vector x, and, turn by turn, compute the square.
I'm going to divide that by sine of x, which is about the same thing, but I'm
going to do it anyway. And notice I have to do the dot again, so
it's dot slash, and I don't want to have it all typed out at me.
And so let's see what y is in first ten elements, and you can see that x equals
0, we get 0 divided by 0, and MATLAB has said that's not a number.
NAN means not a number. And so Matlab can take care of these
calculations, it doesn't care if the things goes to infinity, if you divide 0
by 0, we're still going to return an answer.
And that's one of the very interesting things about MATLAB.
So plot, I can plot these two things, and it looks kind of screwy at the places
where the sine function is going to zero. You can see it's going to start to blow
up, but the plot is still there. Okay.
So this is how you divide a vector by another vector.
You use the dot slash notation. Now the other interesting thing about
MATLAB is that it loves complex numbers. It doesn't care.
So if you type square root of minus 2, it's going to tell you that that is 0
plus 1.4142 times i. They use i for the square root of minus
1. I can't change it to j.
However, what's also very interesting about MATLAB is that the variable i is
predefined, in this case to be 28 because I've written a program before.
If I say i equals the square root of minus 1, that turns out to be the
default. When you first run MATLAB, that's
going to be the default that I use. There's a special symbol that they use,
but you see what happened to me. I ran a program before where I used i as
an index and I, being the square root of minus 1, got redefined by my program.
There's a special symbol, 1i, Which is an illegal variable name that is reserved in
Mat lab to be the square root of -1. And also we want j as defined, which is
also the square root of -1. So I would tend to use 1j.
So I'm going to define y to be equal to the exponent of 1j times 2 times pi times
a vector, f, which I'm going to say runs from 0 to point, in terms of steps, of
101 to, 2. Let's see, I notice I didn't have to
define a immediate variable. I can just type this vector inside, and
everything's going to work fine, so I do that and if I did plot y.
Y is complex and what its going to do is plot the imaginary part versus the real
part. And that's a circle.
So I can also plot, the real part of y, which is a cosine.
And notice I did not give it an x argument.
What it's doing, it's just plotting it against the index into the vector.
And this is a way of getting plots going very quickly, not worrying about the
details of a plot when you're trying to debug or get something just to work.
And of course you can do the imaginary part.
And there's the sine wave that's coming out of the complex exponential.
But notice, it, the thing I want to emphasize is it did calculations with
complex numbers just like they were real numbers.
It really doesn't care. So, this is very handy for us, because
here's our complex exponential signal, we use that a lot in this course, and it's
very easy to type it in and use it just like It was a, a real variable that you
would, in most other programming languages.
So I think you can see why MATLAB is so useful.
Learn, play with it, learn its commands, and one other thing before I leave, is I
want to show you how to write, a loop. So the For command exists in MATLAB, and
I'm just going to say i equals one colon ten.
And I think, I forgot the equals, equals one colon ten.
And I think you know what it's going to do.
It's a loop. It's going to start with i equal to one,
and wind up at a value of ten, incrementing by one every time.
And I'm going to say that Z and I is equal to I up arrow T.
So I'm going to put in the squares, just kind of a silly program, and I've been,
the way that the for loop is terminated is with the end statement.
And it has set up a vector z, which is going to have 10 elements equal in it,
equal to the squares. I could have done this much more simply.
Let's say, for x equals 1 colon 10. Does exactly the same thing, except I
didn't square it, and now if I do x.up there 2, now I get the same thing as z,
and I, could've either written a loop or I could've used, used the special
notation in Matlab. And I think it's pretty clear that
writing the loop is not that great a deal.
Now, suppose I wanted to find this, the average of the x's.
Okay. There's a special command that will do
that called, sum, and that will sum it all up.
And you can divide by the length of x, and I think it's pretty clear what the
length is going to do. And that's the average of the squares of
the first 10 integers. You can also do some other very important
things like this that are matrix-like things.
There's a mean function which will be exactly the same thing as what I just
typed, and of course it gives exactly the same answer.
There are many, many built in functions ready for you to use, and you just have
to go in there and explore MATLAB and find out, to find out what they are.
There's many many tutorials on lab, online, for helping you on getting into
MATLAB and get started, but I think you can see why we use it a lot.
And you are, will be asked to use it to work some of the homework problems.
because. I need some numeric answers
occassionally. Hope you enjoy using matlab in this
course and later on.