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