MATLAB Intro - Arrays, Figures, Plots, etc.
-
0:00 - 0:06Hi guys, I'm gonna give you a quick
crash course tutorial on MathLab for -
0:06 - 0:08first timers or
approximately first timers. -
0:08 - 0:12When you first open MathLab you're
gonna see stuff like these, -
0:12 - 0:14get rid of these as soon as you can,
that's just getting in the way. -
0:14 - 0:18You can still use them, they're
still there, you just have to hover. -
0:20 - 0:22This is the command window.
-
0:22 - 0:24You can use it like a calculator.
-
0:24 - 0:27Do all of your code immediately,
and get response back. -
0:27 - 0:32I like to open up a script, and open
up the editor so I can write a script. -
0:32 - 0:33And it's really clean in here.
-
0:33 - 0:37And then you can evaluate,
over in the command line. -
0:37 - 0:39Notice, they're right next to each other.
-
0:39 - 0:40You can see.
-
0:40 - 0:43All the code heightwise.
-
0:43 - 0:46You can actually do this as well,
minimize the tool strip if you wanted to. -
0:46 - 0:54I've already got a file here
that we're gonna run through. -
0:56 - 0:58And then go through all of
this as quickly as possible. -
0:58 - 1:01So just pause, rewind if you have to.
-
1:03 - 1:06When you start evaluating code in
the editor, you can set these break points -
1:06 - 1:12here to evaluate up to that point,
and then you can continue stepping. -
1:12 - 1:19You can also set modify condition,
so this is a Boolean condition here. -
1:23 - 1:26So to run, I like to use hotkeys.
-
1:26 - 1:28This is function F5, to run.
-
1:29 - 1:33Or you can just use one of the arrows,
up here, when there's a run arrow. -
1:34 - 1:38Anyway, so
now that it's running in debug mode, -
1:38 - 1:42I can continue stepping by clicking
this or just use the hotkey. -
1:44 - 1:51So this first line, I stored a row vector,
-
1:51 - 1:55an array in a row into x1.
-
1:56 - 1:58And it goes from minus 2 pi to 2 pi.
-
1:58 - 2:01Or at least it's supposed to
go from minus 2 pi to 2 pi. -
2:01 - 2:04It defaults,
when it's just one colon there, -
2:04 - 2:07it defaults to integer value increment.
-
2:08 - 2:12So you see over here it went
from -2 pi but it didn't quite -
2:12 - 2:16hit positive 2 pi because
the incrementation didn't allow it. -
2:18 - 2:22So the next line here allows
it to do that because I -
2:22 - 2:27enforced the conditions that it
reaches a total range of 4 pi. -
2:29 - 2:34And it reaches that over 13 increments,
so there was a total of 14 points. -
2:36 - 2:39And if you look at here, X2,
it goes from minus 2 pi to 2 pi. -
2:40 - 2:45And he's our way to do this without
having to think about how to increment, -
2:45 - 2:47is just to use the linspace command.
-
2:49 - 2:51And you just tell it how
many points you want. -
2:51 - 2:56This one, in this case, it's 14 points
that it's identical to this, x2 and -
2:56 - 3:00x3 are identical, if you look over here,
they're identical. -
3:02 - 3:04Just easier to use lens space sometimes.
-
3:06 - 3:07There's also log space.
-
3:09 - 3:13I created lens space again,
and using these for later. -
3:13 - 3:15100 points for log spacing.
-
3:16 - 3:23This log space gives me ten to
the zero to ten to the six. -
3:23 - 3:28So it goes from one to a million
over a hundred points. -
3:31 - 3:35So if you see, you look over here,
-
3:35 - 3:39this is the very largest number,
-
3:39 - 3:45it's a 1 times 10 to the sixth,
it's a million. -
3:45 - 3:47Here's a matrix.
-
3:47 - 3:51It's a this is the first row,
second row, and they're separated, -
3:51 - 3:53rows are separated by a semi colon.
-
3:54 - 3:57This a row vector B,
this is a column vector C. -
3:59 - 4:02The same values just one is
a transpose of the other. -
4:04 - 4:10It was three by four matrix of zeros,
three rows, four columns. -
4:12 - 4:15Matrix of ones,
three by three identity matrix. -
4:17 - 4:24Now, when we started
doing matrix operations, -
4:24 - 4:30let's take a look at how to solve this,
A times Y is equal to B. -
4:31 - 4:35If we know A and we know B,
how do we solve for Y? -
4:37 - 4:43So we invert A, so
I'm gonna grab this and put it down here. -
4:44 - 4:48This operator says,
invert A and operate on B. -
4:49 - 4:53This is going to give me an error because
the matrix dimensions must agree, -
4:53 - 4:59which means MathLab doesn't know how
to operate a matrix on a row vector. -
5:01 - 5:06So this row vector for the least,
-
5:06 - 5:10the less obscure mathematical operation,
which is just matrix -
5:10 - 5:13multiplication that has to be a column
vector, it has to be a column vector. -
5:14 - 5:19There's also a way to do a row
vector here but it's a more -
5:19 - 5:23obscure operation,
out of product of some sort. -
5:23 - 5:27MathLab doesn't know how to
do it unless you massage it. -
5:29 - 5:34So you come over here and you put this
transpose operator, this tick mark, and -
5:34 - 5:36that'll allow it to do
the correct operation. -
5:37 - 5:39So that gives me a solution.
-
5:39 - 5:43My solution Y for this linear system.
-
5:44 - 5:49And then YY is gonna be the same thing
because C is already the transpose of B. -
5:49 - 5:53[COUGH]
This is to check to make sure that I get -
5:53 - 5:55B back, so there's B back.
-
5:57 - 5:59When I got my solution Y.
-
6:01 - 6:06Now here if I have an array of
input values into this function, -
6:06 - 6:10then this function automatically
loops through all those values, so -
6:10 - 6:12you don't have to write
your own four loop. -
6:14 - 6:19So that's the output here is the sign
of every one of those individually. -
6:19 - 6:24Now I'm gonna use these down below here.
-
6:24 - 6:29So this dot here
-
6:29 - 6:33doesn't mean dot product it means
element wise multiplication or -
6:33 - 6:38in this case element wise multiplication,
down here it's element wise division. -
6:40 - 6:46So it just takes the first element here
multiply it with the first element here. -
6:46 - 6:49Second element here, multiply it by
the second element here, and so forth. -
6:49 - 6:53And stores it in W.
-
6:53 - 6:57So if you look at the size of w, compare
it with the size of either Y1 or Y2, -
6:57 - 7:01it doesn't matter,
they're both the same size. -
7:01 - 7:03And size returns all dimensions.
-
7:05 - 7:08As compared to length which I
have suppressed the output so -
7:08 - 7:10I have to actually bring it over.
-
7:12 - 7:17Let's say for example, length of B,
B if you look up here, -
7:17 - 7:22B is 3 by 4 and C is 4 by 3.
-
7:22 - 7:26So the length of either of these
-
7:26 - 7:30is the same because length
finds the longest dimension. -
7:32 - 7:37So going into the plotting,
-
7:39 - 7:43you can just call up the plot function and
it brings up a figure. -
7:43 - 7:48Applies to that figure, you can come in
here and insert labels, title, legend and -
7:48 - 7:51all the stuff right here
if you wanted to but -
7:51 - 7:55I suggest since we're gonna be coding
a lot just get used to writing your code -
7:55 - 7:59to do everything for
you at least as much as possible. -
7:59 - 8:02So there we added a title to it,
we added a label. -
8:02 - 8:06Now if I wanna add another
plot to the same figure -
8:06 - 8:07I have to hold on to the figure.
-
8:07 - 8:13[COUGH] So then I can throw in another
plot under that same set of axis, -
8:13 - 8:15let go the figure.
-
8:16 - 8:21Just because I've let go of it,
you might assume that you can now -
8:21 - 8:25start another plot and it'll create a new
figure for that plot, but it doesn't. -
8:25 - 8:28It overwrites on top of this,
it writes to the same figure. -
8:31 - 8:36So that's an obvious problem,
the way to get over that is by explicitly -
8:36 - 8:41calling the figure,
this is just a quirkyness with MathLab, -
8:41 - 8:44there's probably a way around it,
I just, I'm not aware of it. -
8:44 - 8:48So create a whole new figure,
and then plot to that. -
8:50 - 8:52So it's the same thing with subplot.
-
8:52 - 8:56If I just start subplotting, it's gonna
write over the top of this figure. -
8:57 - 9:04Then it's gonna create a subplot axis or
set of axis and -
9:04 - 9:10then it's gonna plot to that set of axis
and it's gonna tie it all to that axis. -
9:11 - 9:16Now this X label, notice how I've
done this, it's a carrot for -
9:16 - 9:20a super script and
then an underscore for a subscript. -
9:22 - 9:27And the curly braces are if you have
multiple characters that you need to -
9:27 - 9:31include in the scripts,
in the subscripts, super script. -
9:31 - 9:37So then the subplot it says
make me a 2 by 1 region -
9:38 - 9:43and here is populate in
the first region and -
9:43 - 9:46now it's populating in the second region,
these plots. -
9:48 - 9:53So there's the second region of the plot,
title, label, okay? -
9:53 - 9:56So if I continue going now since
I have called figure again, -
9:56 - 10:01this is going to override this and that's
fine because I'm done looking at all this. -
10:03 - 10:07Well okay, so notice that it
didn't override the whole thing, -
10:07 - 10:10it just overwrites the current axis.
-
10:11 - 10:13So this is an exponential.
-
10:14 - 10:18We have a linear set of
points on the input and -
10:18 - 10:19then we have the exponential
set of the output. -
10:21 - 10:25I want to get rid of,
I want to look at this on, -
10:25 - 10:29let's call our new figure, whoops.
-
10:31 - 10:37So now I can look at both of these
if I choose to at the same time. -
10:38 - 10:39So notice how I have.
-
10:41 - 10:44Just gone from plot to send their log Y.
-
10:44 - 10:49So it suppress the exponential
growth in the Y direction by scaling -
10:49 - 10:50it logarithmically.
-
10:52 - 10:58Now, if I give inputs that
are growing exponential and -
10:58 - 11:05then the output is exponential
of the exponential input. -
11:06 - 11:09It wants to be a double
exponential growth. -
11:10 - 11:11So let's see what that looks like.
-
11:12 - 11:19Well, the problem here, you won't be
able to see it, unless we plot it. -
11:26 - 11:31That's a straight line,
notice the X-axis here -
11:31 - 11:37doesn't come any where near
the largest X values of X log. -
11:37 - 11:41Remember this is,
one million is the largest value. -
11:41 - 11:46So what's happened is it's gone to
the maximum that this can possibly go to, -
11:46 - 11:47on the Y value.
-
11:47 - 11:50That's the maximum it can go to, before it
thinks that all the numbers are infinity. -
11:52 - 11:56So this actually goes infinitely higher,
and this goes much further to the right. -
11:56 - 12:00So this is a totally garbage,
a total garbage plot here. -
12:00 - 12:01It doesn't, it's meaningless.
-
12:02 - 12:07So I'm gonna rescale, I'm gonna create
-
12:07 - 12:13X log small enough that it'll
actually plot those values. -
12:16 - 12:21So now you can see,
I should probably get rid of these dots. -
12:22 - 12:26Let's replot this new set of points.
-
12:26 - 12:28Just a default solid line.
-
12:29 - 12:34So this is,
this actually is the exponential growth -
12:35 - 12:39of the inputs and it goes over all
possible values of the inputs. -
12:41 - 12:46So that's still not very
appealing to look at so -
12:46 - 12:51let's create a new figure and
suppress the logarithmic or -
12:51 - 12:55the exponential growth in the X-axis now.
-
12:55 - 13:00So we we semi log X and
notice that I don't have -
13:00 - 13:06to repause a million times,
but let's do this. -
13:11 - 13:17So it is an exponential growth, it's
doubled the exponential because the input -
13:17 - 13:22is, the inputs are growing exponentially
and the output is growing exponentially. -
13:24 - 13:28It's tough to see it's exponential because
there, it's doubled the exponential. -
13:28 - 13:32So if you have a doubly
exponential growth, -
13:34 - 13:37the input and the output then do log log.
-
13:40 - 13:42And that shows you
the exponential characteristic. -
13:42 - 13:47All right, that's about it for this video.
-
13:47 - 13:49The next one will go into more depth.
- Title:
- MATLAB Intro - Arrays, Figures, Plots, etc.
- Description:
-
For the absolute beginner.
Contents:
1- Decluttering your workspace (0:08)
2- Command window vs. writing scripts using the editor (0:20)
2- Debugging with breakpoints and conditional statements (1:01)
3- Arrays (1:43)
4- Linear array incrementation using linspace (2:39)
5- Logarithmic array incrementation using logspace (3:15)
6- Multi-dimensional arrays (3:43)
7- Array of all zeros, all ones, and identity matrix (4:03)
8- Array operations, including common mistakes (4:15)
9- Counting elements in array using size and length (6:52)
10- Plotting data (7:35)
11- Interactive vs. programmatic plot modifications (7:45)
12- Add new data to same plot figure using hold on (8:00)
13- Create new figure without overwriting previous figure (8:18)
14- Create multiple subplots within same figure (8:49)
15- Superscripts and subscripts (9:12)
16- Semilog plots along x or y axis, and log-log plots (10:37 to end) - Video Language:
- English
- Duration:
- 13:51
![]() |
CDStunes edited English subtitles for MATLAB Intro - Arrays, Figures, Plots, etc. |