6: Graphing in MATLAB (2024)

  1. Last updated
  2. Save as PDF
  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vectorC}[1]{\textbf{#1}}\)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}}\)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}\)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    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)

    6: Graphing in MATLAB (1)

    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)

    6: Graphing in MATLAB (2)

    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.

    6: Graphing in MATLAB (3)

    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.6: Graphing in MATLAB (4)

    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

    6: Graphing in MATLAB (5)

    6: Graphing in MATLAB (6)

    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.

    6: Graphing in MATLAB (7)


    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”.

    6: Graphing in MATLAB (8)

    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: 6: Graphing in MATLAB (9)

    Equation 2: 6: Graphing in MATLAB (10)

    6: Graphing in MATLAB (11)


    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.

    \[v=\ {c_v(2gh)}^\frac{1}{2}\]

    6: Graphing in MATLAB (2024)
    Top Articles
    Latest Posts
    Article information

    Author: Msgr. Refugio Daniel

    Last Updated:

    Views: 5693

    Rating: 4.3 / 5 (54 voted)

    Reviews: 93% of readers found this page helpful

    Author information

    Name: Msgr. Refugio Daniel

    Birthday: 1999-09-15

    Address: 8416 Beatty Center, Derekfort, VA 72092-0500

    Phone: +6838967160603

    Job: Mining Executive

    Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

    Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.