In today’s lesson, we will be covering the following topics:
1. X-Y Plotting
2. Fplot and Ezplot
3. Adding Titles and Axis Labels
4. Controlling the Axes
VIDEO COMING SOON
X-Y Plotting
To make a plot (only in two dimensions for now), we need a matching number of x- and y- values, just like we would need if we plotted a graph by hand, where each x-value had a corresponding y-value. Say you have the (x,y) coordinates you’d like to plot.
How do we put these into MATLAB? They need to be typed in as vectors (or matrices – if you try to type in scalars, you will get a blank graph because you need a set of data, not just one value). We learned about creating vectors in a previous lesson, either manually typing the numbers within square brackets, or using other commands such as the colon operator or linspace. Once we have created these vectors in MATLAB, we use the plot(x,y) command to create a graph. Replace x in the brackets with the variable you are storing the x-variables in, and same with y.
Take a look at this example, where I have created vectors A and B, then plotted them using plot(A,B). This means that the numbers for A are on the x-axis and those for B are on the y-axis, because I used plot(A,B) rather than plot(B,A). The resulting graph is also shown in the image below:
Here’s another example where I’ve created vector A using the colon operator, and then plotted it for the function sin(A).
We can also use the plot command with just plot(y) as well.
If you type in the plot command with just one variable instead of two, it will plot the numbers from that one vector directly on the graph along the y-axis.
What would it plot on the x-axis if there’s no (x,y) point to plot? Take a look at the numbers in the vector you created – for that’s vector A below. How many numbers are in that vector? Mine has 6 numbers in it – 1, 4, 6, 7, 9, 10. The x-axis will reflect the positions of each number in the set. This means that 1 is in position 1, 4 is in position 2, 6 is in position 3, and so on. This is called indexing, as we discuss in a previous lesson. That’s why you only see 6 numbers along my x-axis, but the numbers on the y-axis go up to 10 which is the highest value in vector A.
Note: We can add a grid to the graph to make it easier to see what points the line is falling between. To do this, use the command grid on.
Running this will give the following graph, where you can see grid lines:
Fplot and Ezplot
If you don’t want to define both x- and y-vectors to plot, you can use the commands fplot or ezplot by just defining a function. fplot will plot that function over the default x-values of -5 to 5. ezplot plots the function over default x-values of -2*pi to 2*pi. You can use this with implicit or explicit functions!
The syntax for these is either fplot(function name) or ezplot(function name). If you want to set the limits on these graphs, you can add this directly into the syntax as fplot(function name, [xmin xmax]) or ezplot(function name, [xmin xmax]). We cover this in the examples below. Note that Mathworks recommends to use fplot rather than ezplot, because ezplot can behave differently under certain conditions.
Example – Using ezplot to plot an explicit function, with and without limits
Recall from math that an explicit function is one in which you have two different variables on their own sides of the equals sign. For example, y = x^3, where y is on the left and x is on the right. Let’s plot this using ezplot. We can do it one of two ways – either using syms to define the unknown variable x, or creating an inline function (with the @ symbol) before using ezplot. I do it both ways here, to get the same plot.
The first method uses syms with the unknown variable x, then calls the function to plot using ezplot:
The second method creates an inline function, then calls that variable using ezplot:
This is the same graph that is output from using either method:
Notice how the axis goes from about -6 to 6? This is because the limits of ezplot are by default,-2*pi to 2*pi, which is about -6.28 to 6.28. Now if we wanted to change these limits (let’s say from -10 to 10), we could replace the code with the following instead, by defining the limits right within ezplot.
Note that we do not use xlim to change the limits here as we would using the plot command, because that only plots the graph between the default values -2*pi to 2*pi, and just zooms the graph out while not actually changing the values it plots over.
This gives the following graph, and you can see how the x-axis now goes from -10 to 10:
Example – Using fplot to plot an explicit function, with and without limits
Just as we did with ezplot, we can define the variables for fplot with either an inline function or with syms. I won’t go through both methods here, because we’ve done them with ezplot and you will define your equations in the same manner. I will use syms here, and I will plot y = x^3.
This is the graph that comes up:
Example – Plotting an implicit function using ezplot
We can plot implicit functions as well, specifically using ezplot. There is also a command called fimplicit, but my version of MATLAB does not have this functionality (you can check it out on Mathworks if you’d like to specifically use fimplicit instead). Recall from math that implicit functions are those that have some of the same variables on both sides of the equation – the equation is not defined in terms of one variable. For example, y+x*y^2 = y^3 + 2*x would be considered an implicit function, because it is not defined solely in terms of y on one side of the equation and x on the other. The ezplot command is used in almost the same way as it is with one variable.
I am going to use the syms method to define my unknown variables (recall I mentioned above that we can either use syms or create an inline function before using ezplot). The two unknowns are now x and y, because y is not defined by x anymore. Because of the change from an explicit to an implicit function, using syms will now require us to define both x and y. Then to use ezplot (the same way as we did in the example above), we will define the equation within the ezplot brackets. For implicit functions, you will need to write the entire equation with both sides of the equals sign. This requires a double equals sign, otherwise MATLAB will give error.
This is the graph that results:
Example – Plotting multiple equations with fplot or ezplot
What if we want to plot multiple equations? This can be done with both fplot and ezplot. They are used in the same manner, so I will just do an example using ezplot. Here, you can use two inline functions to define the equations, or use syms as I have done. I still only need syms x, because the only unknown variable in both equations is x. Then I use ezplot with both equations within the brackets:
This is the output:
Adding Titles and Axis Labels
These graphs above look like they’re missing something – what about a title, or a label on the x- or y-axis? To add these in, use the following syntax:
(a) Title: title(‘name of title’), where you replace the words name of title with the title of the graph
(b) X-Axis Label: xlabel(‘name of x label’), where you replace the words name of x label with the x-label of the graph
(c) Y-Axis Label: ylabel(‘name of y label’), where you replace the words name of y label with the y-label of the graph
Note that the title/labels code must be typed after you type the plot command. In the example below, you can see that I have created the vectors A and B, then typed the plot command in the next line, then the title and labels in the lines below that. The output graph has the title and labels circled in red:
Controlling the Axes
Controlling the axes means setting a limit on the x- and/or y-axis to specify what values you want to show.
Let’s take a look at this example. I have created vectors A and B, and I want to plot them – but only between the values of 0 and 5. Use the syntax xlim([min max]) and ylim([min max]) to control the x- and y-axis, respectively. Replace min and max with the minimum and maximum numbers you are setting your bounds with, which for me was 0 and 5. That’s why I have the code below for xlim([0 5]) and ylim([0 5]). This code should come after the plot command, and you can see that the resulting graph only shows the x- and y-axis both stopping at 5.