< Return to Video

MATLAB Intro - Arrays, Figures, Plots, etc.

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

more » « less
Video Language:
English
Duration:
13:51

English subtitles

Revisions