# Connexions

You are here: Home » Content » Freshman Engineering Problem Solving with MATLAB » Introduction to Graphing in M-File Environments

### Recently Viewed

This feature requires Javascript to be enabled.

Inside Collection (Course):

Course by: Darryl Morrell. E-mail the author

# Introduction to Graphing in M-File Environments

Module by: Darryl Morrell. E-mail the author

Summary: This module provides a tutorial introduction to graphing data in m-file environments.

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 module, we introduced the basics of graphing data in m-file environments through a series of examples. This module uses some fundamental operations on vectors that are explained in Vectors and Arrays in M-File Environments.

## Example 1

The table below shows speed as a function of distance for a braking Dodge Viper decelerating from 70MPH to 0MPH.

### Note:

This data was not measured; it was computed using the stopping distance reported for a Dodge Viper and assuming constant deceleration. Thus, it may not accurately reflect the braking characteristics of a real Dodge Viper.
 Distance (ft) Velocity (ft/s) 0 102.7 29.1 92.4 55.1 82.1 78.0 71.9 97.9 61.6 114.7 51.3 128.5 41.1 139.2 30.8 146.9 20.5 151.5 10.3 153.0 0.0
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]
plot(dist,vel)
Figure 1 shows the graph created by these commands. This graph shows the data, but violates several important conventions of engineering practice. The axes are not labeled with quantity 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.

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 H2:

### Note:

As in Example 1, this data was not measured; it was computed using the stopping distance reported for a Hummer H2 and assuming constant deceleration.
 Distance (ft) Velocity (ft/s) 0 102.7 46.3 92.4 87.8 82.1 124.4 71.9 156.1 61.6 182.9 51.3 204.9 41.1 222.0 30.8 234.2 20.5 241.5 10.3 244.0 0.0

### Solution

Figure 3 shows the graph of the Hummer H2 stopping data.

## Example 2

An m-file environment can also be used to plot functions. For example, the following commands plot cosx x over one period.

x = 0:0.1:2*pi;
y=cos(x)
plot(x,y)
xlabel('x')
ylabel('cos(x)')
title('Plot of cos(x)')

Figure 4 shows the graph created by these commands.

## Exercise 2

The module Exercises for Basic Mathematical Operations 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 40kg to 500kg.

## Exercise 3

In electrical circuit analysis, the equivalent resistance R eq R eq of the parallel combination of two resistors R 1 R 1 and R 2 R 2 is given by the equation

R eq =11 R 1 +1 R 2 R eq 1 1 R 1 1 R 2
(1)
Set R 2 =1000 Ohms R 2 1000 Ohms and plot R eq R eq for values of R 1 R 1 from 100 Ohms to 3000 Ohms.

## Exercise 4

In an experiment, a small steel ball is dropped and videoed against a checkered background. The video sequence is analyzed to determine the height of the ball as a function of time to give the data in the following table:

 Time (s) Height (in) 0.0300 22.0 0.0633 21.5 0.0967 20.5 0.1300 18.8 0.1633 17.0 0.1967 14.5 0.2300 12.0 0.2633 8.0 0.2967 3.0
This experimental data is to be compared to the theoretically expected values given by the following equation:
h=22 in12gt2 h 22 in 1 2 g t 2
(2)
where h h is in inches, t t is in seconds, and g=386.4ins2 g 386.4 in s 2 . 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.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

#### Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

#### Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks