< Return to Video

MATLAB Command Line Plotting: Simple example

  • 0:00 - 0:02
    Hello, and welcome to the screen cast,
  • 0:02 - 0:05
    where we're gonna do a real simple example
    of plotting a single variable function in
  • 0:05 - 0:10
    MATLAB using only an m file, using a step
    we would use only from the command line.
  • 0:10 - 0:13
    I'm picking this function just basically
    by propping open my calculus book and
  • 0:13 - 0:16
    picking a random number
    from the exercise sets, and
  • 0:16 - 0:21
    happened to see the function f of x
    equals x divided by x squared plus 4.
  • 0:21 - 0:24
    I have no idea what this looks like,
    and this is a.
  • 0:24 - 0:27
    Function from the book and the section
    on first and second derivative tests, so
  • 0:27 - 0:30
    the shape of this graph is
    actually pretty important.
  • 0:30 - 0:32
    This is something I would do
    if I were doing homework.
  • 0:32 - 0:34
    So let's begin with a blank M file and
  • 0:34 - 0:40
    we're gonna first define our variable x
    equals, let's just go with linspace -5,5.
  • 0:40 - 0:45
    This turns out to be a bad choice or
    need different values of x,
  • 0:45 - 0:50
    I can come back and redefine x or
    change the axis limits later on.
  • 0:50 - 0:57
    So x put a colon there and on my function
    I said was x divided by x squared +4.
  • 0:57 - 1:01
    This is gonna give me all kinds of error
    messages right now because remember,
  • 1:01 - 1:06
    x is a vector and so any time I'm
    dividing or exponentiating by a vector,
  • 1:06 - 1:08
    I need to make element-wise operations.
  • 1:08 - 1:10
    So.
    divided by.
  • 1:10 - 1:13
    squared, that should do it.
  • 1:13 - 1:16
    Now the basic plot I would get
    is plot x,y, and we're going
  • 1:16 - 1:21
    to change a lot of things from here,
    but let's just get a basic plot going.
  • 1:21 - 1:27
    I'm going to save this to my
    MATLAB folder as a simple example.
  • 1:30 - 1:33
    And so I'll save it and when I plot it,
    I get something that looks like this.
  • 1:33 - 1:34
    Actually that's not too bad.
  • 1:34 - 1:38
    I'm kinda wondering what happens here at
    the ends as x increasing without bound and
  • 1:38 - 1:40
    decreases without bound.
  • 1:40 - 1:43
    Maybe there's an asyms out there, so
    I think I may make a few changes here.
  • 1:43 - 1:48
    And again, not from the plot tools
    window but straight from the m file.
  • 1:48 - 1:50
    Let's change some of the style elements.
  • 1:50 - 1:55
    First of all let's make this a dotted
    red line as a little thicker than usual.
  • 1:55 - 1:57
    That would be single quote.
  • 1:59 - 2:03
    I'm sorry a dotted red line
    would be a single dot redline.
  • 2:03 - 2:06
    And let's just do that much and
    that makes a bunch of dots,
  • 2:06 - 2:11
    now that I see it I'm not so keen on that,
    I think I'd rather have a dash line.
  • 2:11 - 2:13
    So I'm gonna go back and
    change this to a dash,
  • 2:13 - 2:17
    you get a dash line we have to 2
    dashes in here in this plot option.
  • 2:17 - 2:19
    Now when I click this,
    now it looks pretty good.
  • 2:19 - 2:23
    I would like to make
    this a little thicker, so
  • 2:23 - 2:28
    I'm gonna change the line width like so
    to let's say to 3 Yeah,
  • 2:28 - 2:33
    that's pretty nice, and I think I will
    leave the markers alone for right now.
  • 2:33 - 2:34
    So I'm done with changing the style and
  • 2:34 - 2:38
    color of my graph right now,
    I'm not gonna add any markers.
  • 2:38 - 2:41
    Now one thing I said was looking
    at this graph on the right here,
  • 2:41 - 2:45
    it makes me wonder if there's a horizontal
    isotope somewhere further down the x axis.
  • 2:45 - 2:50
    One thing I can do here is if I want to
    look further than negative five to five,
  • 2:50 - 2:53
    I'm going to have to change my x value,
    so let me go back and
  • 2:53 - 2:57
    change this to something
    relatively double that, -10,10.
  • 2:57 - 3:01
    And reclick and
    it will automatically change that and
  • 3:01 - 3:06
    maybe I even want to go further than that,
    lets' go -20,20.
  • 3:06 - 3:08
    I think that should be good enough.
  • 3:08 - 3:10
    We plot and that looks very nice there.
  • 3:12 - 3:15
    Now one thing I might want to
    do is look at the graph here.
  • 3:15 - 3:17
    I think a graph grid would be useful for
    you.
  • 3:17 - 3:21
    So, I'm going to type grid on and
    evaluate that and it puts a grid on here.
  • 3:23 - 3:26
    I'm pretty happy with the placement of
    my tick marks and I'm pretty happy with
  • 3:26 - 3:30
    placement of my tick marks over here so
    I think I'm gonna leave those alone.
  • 3:30 - 3:33
    Although if I wanted to change them
    I would use the set command and
  • 3:33 - 3:36
    then feed a GCA for the argument.
  • 3:36 - 3:38
    And then set the x-ticks
    to whatever I want.
  • 3:39 - 3:44
    Now one thing I will do is, I'm gonna
    make the vertical limits on the axis
  • 3:44 - 3:49
    a little bit taller cuz I see the peak
    happen right here but it's almost like
  • 3:49 - 3:53
    I would like a little more daylight
    up above this so I can see it.
  • 3:53 - 3:57
    So, I'm gonna keep the x minimum and
    x maximum at -20 and 20 but
  • 3:57 - 4:00
    I'm gonna change the y minimum and
    y maximum.
  • 4:01 - 4:06
    Let's say to -0.35 instead of -0.25 and
  • 4:06 - 4:10
    +0.35 to plus from +0.25.
  • 4:10 - 4:12
    I am gonna do that by typing axis and
  • 4:12 - 4:16
    then just feed it a vector
    that's got my limits in it.
  • 4:16 - 4:19
    So keep (-20,20) and
    -0.35 to 0.35 for the y.
  • 4:19 - 4:24
    And again, that's just gonna give
    me a little bit of daylight so
  • 4:24 - 4:28
    I can see those peaks
    happen a little bit better.
  • 4:28 - 4:30
    You've got a nice looking
    square grid on here.
  • 4:30 - 4:33
    Let's move on now to titles.
  • 4:33 - 4:38
    And we don't have any context here,
    but I will give the xlabel,
  • 4:38 - 4:43
    and just out the letter x on the x-axis,
    and ylabel.
  • 4:43 - 4:47
    How to split the f(x),
    because that's what I'm plotting, is f(x).
  • 4:47 - 4:48
    Let me run those, there are labels.
  • 4:50 - 4:52
    Finally, let's end off
    by giving this a title.
  • 4:54 - 5:00
    And let's put as title just
    the function definition,
  • 5:00 - 5:04
    f(x) = X divided by x squared plus 4.
  • 5:04 - 5:09
    And when I do that,
    there's my function up here.
  • 5:09 - 5:12
    So that's a very simple example of
    starting with a mathematical function,
  • 5:12 - 5:16
    plotting it, and making a few changes to
    make it nice and attractive, thanks for
  • 5:16 - 5:17
    watching.
Title:
MATLAB Command Line Plotting: Simple example
Description:

A quick example of plotting and formatting a single-variable function from an M-file in MATLAB.

more » « less
Video Language:
English
Duration:
05:23

English subtitles

Revisions