Chapter 6: Graphing in MATLAB
Creating Graphs
MATLAB has tools that enable the user to display data within visual forms such as tables, 2D, or 3D graphs to increase readability for the user. General graphs can be created by the user with the plot command, which can be modified to incorporate colors, symbols, labels, and other aspects of the graph to ensure that the data is able to be read and interpreted by the user.
The plot function operates by plotting data assigned to a variable onto a graph. A simple way to graph the first-order line onto a plane is by listing the range of values for both the x and y coordinate which need to be graphed. The following example shows how the user could assign the range for the x- and y-axis, respectively, using vectors. This notation will generate a graph with a line running from the point (5,11) to (10,16). This is an easy way to generate a linear graph but most applications within scripts will be more involved than this.
>> x = 5:10;
>> y = 11:16;
>> plot (x,y)
The above technique can be modified to make one variable dependent on the other. For example, if you have a set range of values (let’s say x = 1,2,3,4,5 for this example) and another variable that is a multiple of x (say y = 3*x for this example) the following code could be used to plot the above example.
>> x = 1:5;
>> y = 3*x;
>> plot(x,y)
Most applications of the plot function within MATLAB will incorporate an equation for one of the variables, thus creating the need for a plot to visualize the data. As a result, equations such as the y-value in the above example are much more prevalent than predetermined vector operations.
Another way to generate an evenly spaced vector to use when plotting a function is the linspace
command. The linspace
command selects the initial value, the final value, and how many values should be generated in between this range. An example is shown below, which represents a vector containing 100 values ranging between 0 and 10. The linspace
command is used below in the example plotting two functions on the same graph.
>> linspace(0,10,100)
Several functions can be graphed within a single MATLAB, enabling the user to display several data sets more efficiently. This can be done by listing each set of variables in a series. For example, for the following variables, x, y, and x, the user would graph an x vs y plot and a x vs z plot in the same axis by using the following notation. This notation is one way the user can plot as many sets of variables as needed, though it is limited to 2D.
>> x = 1:6;
>> y = x.^2;
>> z = x.^3;
>> plot(x,y,x,z)
In order to place labels and titles in MATLAB plots, the following commands can be used to generate labels. Listing each of these commands after a plot command will add the labels to the graph. The following example is a general example of how to label the graph. Note that the font size can be modified by adding ‘FontSize’ followed by the desired font size after the title in the command.
>> xlabel(‘Name of Label’)
>> ylabel(‘Name of Label’)
>> title(‘Graph Title’,’FontSize’, 12)
>> legend(‘Name of First Plot’,’Name of Second Plot’)
The following image contains concepts addressed above within an actual MATLAB code to create graphs like the one represented in the above examples of commands.
Another easy method to plot several lines on the same graph is easy within MATLAB. When writing code, use the hold-on command in between commands plotting each line. This will result in several lines being graphed on the same plot, which can be valuable when comparing data or wanting to save space when displaying information. An example is included below which also uses the linspace
function to generate vectors for the x-axis of the plot.
There are many methods available within MATLAB that can assist in producing graphs including scatter plots, line plots, or other non-linear display methods. To create a function that plots a scatter graph instead of a linear graph, use the function scatter in the place of the plot, which will only place points. This type of function is when the vector used to create the x-axis is important, as the number of points in the vector will dictate the number of points in the scatter plot. Additional replacements that can be used in place of the plot function include the bar function for bar graphs, the stairstep function for stairstep graphs, and the stem plot for stem graphs. An example of the scatter function is included in example 1 below.
Specifier | Line Style |
‘-‘ | Solid Line, which is the default |
‘–‘ | Dashed Line |
‘:’ | Dotted Line |
‘-.’ | Dash-Dot Line |
Specifier | Marker Type |
‘+’ | Plus |
‘o’ | Circle |
‘*’ | Asterisk |
‘.’ | Point |
‘square’ or ‘s’ | Square |
‘diamond’ or ‘d’ | Diamond |
‘^’ | Upward-pointing triangle |
‘v’ | Downward-pointing Triangle |
‘>’ | Right-Pointing Triangle |
‘<’ | Left-pointing Triangle |
‘pentagram’ or ‘p’ | Five-pointed Star (Pentagram) |
‘hexagram’ or ‘h’ | Six-pointed Star (Hexagram) |
MATLAB plots enable the user to modify the physical appearance of the code to incorporate colors and shapes which will help depict data more effectively than a simple line. Being able to differentiate the physical appearance of graphs will be especially important when plotting several data sets in the same plan, which will be addressed shortly. Color or shape variations are listed at the end of a plot function within quotations. The following chart depicts a list of symbols and letters that can be used to create corresponding symbols or colors within a MATLAB plot.
The following examples utilize colors and specifiers to create unique and more effective graphs. Notice that when using a vector, the specific values are visible when using a point notation as opposed to a continuous line. An additional detail that is necessary to make this code run is the period placed after the variable x in the equation. In order to multiply a matrix by an exponent, the period must be used to tell MATLAB to take each value in the matrix by the power. In this situation, each number from zero through 10 is squared and plotted in the graph.
Symbol | Color |
r | Red |
g | Green |
b | Blue |
c | Cyan |
m | Magenta |
y | Yellow |
k | Black |
w | White |
The Plot Tab
The plot tab in the MATLAB user interface is a handy tool to create involved plots for variables within a MATLAB code. By using the plot tab user can select a variable in the workspace and generate a graph that best represents the data assigned to the variable. In the following example, the variable y is selected, which enables the user to select one of the included graphs from MATLAB’s library. This feature can save time and enable the user to generate a quality image that effectively displays the user’s data.
Examples
1) A professor wants a graph that depicts 20 points of a sine wave undergoing one period of motion. Create the graph using a cyan sine wave and 20 red stars plotted over the cyan wave. Label the x-axis “X”, the y-axis “Y”, and title the graph “Sine Wave”.
2) The following functions represent the motion of two projectiles. Plot the functions on the same graph. Use proper labels and ensure the graph is sufficiently legible for users. Use x-axis values from 0 to 20. Create a key and some title for the plot.
Equation 1: [latex]f\left(x\right)=-4x^2+24x+100[/latex]
Equation 2: [latex]f\left(x\right)=-6x^2+36x+120[/latex]
Problems
1) The following data points were collected in an experiment. Write a MATLAB script that plots the data points.
Time (s) | 1 | 2 | 3 | 4 | 5 |
Location (y) | 1 | 4 | 7 | 12 | 17 |
2) The location of a particle is represented by the function. Write a MATLAB script that enables the user to plot the data set over a range of seconds inputted by the user.
3) The following equation can be used to model the velocity of water as it flows out of a hole located in the bottom of a tank. Given that (velocity coefficient) is .97 for water, gravitational acceleration, is 9.81 on Earth, and is the height of the water, determine the velocity of the water like a tank initially filled with 5m of water drains until empty. Create labels for the graph’s axes and a title.
[latex]v=\ {c_v(2gh)}^\frac{1}{2}[/latex]