This screencast is going to show you the basics to plotting in MATLAB.
To start out I've got a vector for time, and for y, so y is a function of time, and I am
just going to simply plot this, so the results, I am going to plot t and y, so that's in the
form (x,y).
I can also add things like xlabel('time') and ylabel('pressure difference').
When I do that it pulls up a plot with the axis labels intact.
By default this is generated in a new figure, figure 1.
If I am interested in plotting multiple things I can actually declare figure 2, and when
I do that it's just empty, and I still have my figure 1 here.
That's how if you're working on two different things you can have two figures going.
So let's go back to figure 1, and I am actually going to create something called a subplot.
A subplot is basically breaking that figure into multiple subplots.
Here I have subplot (2,2,1), and that will actually make it a 2x2, and when I have the
1 here that refers to the first of those subplots, and it actually goes by rows.
So I am going to show you subplot(2,2,3), and if I bring up my figure it has changed
to a subplot, and now I am working with a subplot 3 of that 2x2.
I can then redo the plot, and if I go back to my figure 1 we see that it has plotted
that in the subplot 3.
Now if I wanted to I can use commands to actually scale this, and add a grid if I wanted to.
Some other basic information for plotting, there are these color codes, line styles,
marker styles.
For example plot(1,2,'gs') in single quotations would plot a green square at coordinates x=1,
y=2.
You can also plot straight lines by putting in a vector for x coordinates, a vector for
y coordinates, and then for example we have a black dotted line here.
Some other useful supporting functions, we have axis, so you can actually change the
x-axis to be a minimum, a maximum, the min and max of the y-axis by putting this vector
of those four different ranges.
We have something called linspace, and that is creating a vector of fifty equally spaced
values.
By default this is 100, it's not quite the same as if you actually make a vector, and
we can also do logspace, which creates a vector of 100 values from 0.01 to 100, according
to the log values, so this is how you can make a log or a semi-log plot.
What if we wanted to plot both of those, so cosine(x)^2, and sine(x)^2 on the same plot,
we can easily do that.
I redefined my y and z, plot(x,y) will actually plot y as a function of x.
This is very important, because you want to plot two different lines on the same plot
you have to do this hold on command, so hold on will actually retain the previous plot,
and now we can add something to it, and here by the way this g makes that line green, so
if I press enter and go to the figure we can see that both of them are plotted.
I can do grid, or grid on in this case, and we see that both of them are plotted.
So let's work through another example here, I've set x values going from 0.1 to 1 in increments
of 0.1, and now I've got data here, and now I am going to plot y as a function of x, and
I've got ks.
k corresponds to black, s corresponds to square.
When I do this I get the following plot.
Another thing I am going to do now is fit the data on that plot to a third order polynomial
so I can write coefficient equals polyfit(x,y,3), this 3 corresponds to a third order polynomial.
When the coefficient is spit out that vector, the first one is the coefficient for the third
power, second power, first.
Now we want to add that fit, our data.
I can make a new vector here that's going to be polyval, so that's actually going to
take each of those x values using the coefficients that we just determined from our third order
fit using polyfit, and we're going to create new values, and we're going to plot this as
a line, and I am going to put a hold on so I retain the markers.
Plotting x and I am plotting my fit, and I am making a black dotted line.
We look at our plot, we've got our data markers as black squares, and we got our fit as a
line, and we that the fit goes through most of these black markers.