Skip Navigation

4.1: Introduction to Graphing in M-File Environments

Difficulty Level: At Grade Created by: CK-12
Turn In

One of the reasons that m-file environments are extensively used by engineers is their capability to provide graphical representations of data and computed values. In this section, we introduced the basics of graphing data in m-file environments through a series of examples. This section uses some fundamental operations on vectors that are explained in Vectors and Arrays in M-File Environments in the chapter Basic Mathematical Computations.

Example 1

Table 1 shows speed as a function of distance for a braking Dodge Viper decelerating from \begin{align*}70 \;\mathrm{MPH}\end{align*} to \begin{align*}0 \;\mathrm{MPH}\end{align*}.

Note: This data was not measured; it was computed using the stopping distance reported for a Dodge Viper (http://www.caranddriver.com/article.asp?section_id=33&article_id=2420&page_number=2) and assuming constant deceleration. Thus, it may not accurately reflect the braking characteristics of a real Dodge Viper.

Dodge Viper Stopping Data.
Distance (ft) Velocity (ft/s)
\begin{align*}29.1\end{align*} \begin{align*}92.4\end{align*}
\begin{align*}55.1\end{align*} \begin{align*}82.1\end{align*}
\begin{align*}78.0\end{align*} \begin{align*}71.9\end{align*}
\begin{align*}97.9\end{align*} \begin{align*}61.6\end{align*}
\begin{align*}114.7\end{align*} \begin{align*}51.3\end{align*}
\begin{align*}128.5\end{align*} \begin{align*}41.1\end{align*}
\begin{align*}139.2\end{align*} \begin{align*}30.8\end{align*}
\begin{align*}146.9\end{align*} \begin{align*}20.5\end{align*}
\begin{align*}151.5\end{align*} \begin{align*}10.3\end{align*}

The following commands will create a graph of velocity as a function of distance:

dist = [0 29.1 55.1 78.0 97.9 114.7 128.5 139.2 146.9 151.5 153.0]
vel = [102.7 92.4 82.1 71.9 61.6 51.3 41.1 30.8 20.5 10.3 0.0]

Figure 1 shows the graph created by these commands.

Graph of the Viper's velocity as a function of distance.

Graph of the Viper's velocity as a function of distance.

This graph shows the data, but violates several important conventions of engineering practice. The axes are not labeled with quantities and units, and the graph does not have a title. The following commands, when executed after the plot command, will label the axes and place a title on the graph.

xlabel('Distance (ft)')
ylabel('Velocity (ft/s)')
title('Velocity vs Distance for the Dodge Viper')

The results of these commands are shown in Figure 2.

Graph of the Viper's velocity as a function of distance. The graph has a title and labels on the axes.

After creating a figure, you may wish to insert it into a document. The method to do this depends on the m-file environment, the document editor and the operating system you are using.

MATLAB, Word, and Windows: To paste a figure into a Word document on Windows, pull down the Edit menu of the window of the MATLAB figure you wish to paste, and select Copy Figure. Then go to the Word document into which you wish to insert the figure and use the paste command.

Exercise 1

Repeat Example 1 using the following data for a Hummer \begin{align*}H2\end{align*} (http://www.caranddriver.com/article.asp?section_id=33&article_id=2420&page_number=2).

Note: As in Example 1, this data was not measured; it was computed using the stopping distance reported for a Hummer \begin{align*}H2\end{align*} and assuming constant deceleration.

Distance (ft) Velocity (ft/s)
\begin{align*}46.3\end{align*} \begin{align*}92.4\end{align*}
\begin{align*}87.8\end{align*} \begin{align*}82.1\end{align*}
\begin{align*}124.4\end{align*} \begin{align*}71.9\end{align*}
\begin{align*}156.1\end{align*} \begin{align*}61.6\end{align*}
\begin{align*}182.9\end{align*} \begin{align*}51.3\end{align*}
\begin{align*}204.9\end{align*} \begin{align*}41.1\end{align*}
\begin{align*}222.0\end{align*} \begin{align*}30.8\end{align*}
\begin{align*}234.2\end{align*} \begin{align*}20.5\end{align*}
\begin{align*}241.5\end{align*} \begin{align*}10.3\end{align*}

Example 2

An m-file environment can also be used to plot functions. For example, the following commands plot \begin{align*}\cos(x)\end{align*} over one period.

x = 0:0.1:2*pi;
title('Plot of cos(x)')

Figure 3 shows the graph created by these commands.

Graph of one period of the cosine function.

Graph of one period of the cosine function.

Exercise 2

Exercise 3 in the chapter Basic Mathematical Computations describes how to compute the terminal velocity of a falling sky diver. Plot the terminal velocity as a function of the sky diver's weight; use weights from \begin{align*}40 \;\mathrm{kg}\end{align*} to \begin{align*}500 \;\mathrm{kg}\end{align*}.

Exercise 3

In electrical circuit analysis, the equivalent resistance \begin{align*}R_{eq}\end{align*} of the parallel combination of two resistors \begin{align*}R_1\end{align*} and \begin{align*}R_2\end{align*} is given by the equation

\begin{align*}R_{eq} = \tfrac{1} {\tfrac{1}{R_1} + \tfrac{1}{R_2}}\end{align*}

Set \begin{align*}R_2 = 1000 \;\mathrm{Ohms}\end{align*} and plot \begin{align*}R_{eq}\end{align*} for values of \begin{align*}R_1\end{align*} from \begin{align*}100 \;\mathrm{Ohms}\end{align*} to \begin{align*}3000 \;\mathrm{Ohms}\end{align*}.

Exercise 4

In an experiment, a small steel ball is dropped and its trajectory is recorded using a video camera with a checkered background behind the ball. The video sequence is analyzed to determine the height of the ball as a function of time to give the data in Table 3.

Height and time data for the falling ball.
Time (s) Height (in)
\begin{align*}0.0300\end{align*} \begin{align*}22.0\end{align*}
\begin{align*}0.0633\end{align*} \begin{align*}21.5\end{align*}
\begin{align*}0.0967\end{align*} \begin{align*}20.5\end{align*}
\begin{align*}0.1300\end{align*} \begin{align*}18.8\end{align*}
\begin{align*}0.1633\end{align*} \begin{align*}17.0\end{align*}
\begin{align*}0.1967\end{align*} \begin{align*}14.5\end{align*}
\begin{align*}0.2300\end{align*} \begin{align*}12.0\end{align*}
\begin{align*}0.2633\end{align*} \begin{align*}8.0\end{align*}
\begin{align*}0.2967\end{align*} \begin{align*}3.0\end{align*}

This experimental data is to be compared to the theoretically expected values given by the following equation:

\begin{align*}h = 22 - \tfrac{1}{2}gt^2\end{align*}

where \begin{align*}h\end{align*} is in inches, \begin{align*}t\end{align*} is in seconds,and \begin{align*}g = 386.4 \tfrac{in}{s^2}\end{align*}. Create a graph that compares the measured data with the theoretically expected values; your graph should conform to good conventions for engineering graphics. Plot the measured data using red circles, and plot the theoretically expected values using a blue line.

Notes/Highlights Having trouble? Report an issue.

Color Highlighted Text Notes
Show More

Image Attributions

Show Hide Details
Files can only be attached to the latest version of section
Please wait...
Please wait...
Image Detail
Sizes: Medium | Original