Objectives

- To learn to generate data in MATLAB
- To learn to visualize the data as plots

We will use MATLAB in this lab. To start MATLAB, double click on MATLAB icon on your PC.

In this lab manual every time that we show >> we are referring to the MATLAB command prompt where you can type a MATLAB command. Then you have to press Enter to execute that command. MathWorks provides an excellent online tutorial for plotting, click here to bring that page up.

This lab is designed to help you learn some of the most powerful tools in MATLAB that allows you to create different visual representations of your data. In general MATLAB Graphics provides tools for:

- Basic Plotting – describes how to plot vector and matrix data in 2-D representations.
- Formatting Graphs – describes how to customize the format of a plot to help explain your data. This section includes information about adding labels, titles, and annotations to a plot.
- Creating Specialized Plots – describes how to create bar graphs, histograms, and contour plots.
- Basic Printing and Exporting – describes frequently used ways to print or export a MATLAB figure, and explains the basic printing and exporting interfaces.
- Handle Graphics Objects – introduces MATLAB’s graphics objects and properties.
- Figure Properties – discusses the features that are implemented with figure properties.
- Axes Properties – discusses the features that are implemented with axes properties.

As it is the case for any visualization case we need data to generate plots or visualization. We can generate data using formulas or mathematical relationships or we can use the data from an experiment to generate visual representation of that data which helps us better understand the data.

In our first example we will generate data using the projectile motion from physics. The projectile motion is a two-dimensional motion where the projectile moves in x (distance) and in y (height) directions. In this motion the projectile is usually sent of from its initial location with an initial speed (v0) at an initial angle (angle0).

Suppose our projectile motion starts with an initial speed of v0 = 40 m/s at angle0 = 35 degree from ground (initial height of 0).

We want to create 4 different plots to:

- Visualize the horizontal distance of the projectile from its initial location at different times.
- Visualize the height of the projectile from its initial location at different times.
- Visualize the horizontal speed at different times.
- Visualize the vertical speed at different times.

- InitialVerticalSpeed = v0*sin(angle0)
- InitialHorizontalSpeed = v0*cos(angle0)

distance = InitialHorizontalSpeed*time

height = -(1/2)*g*time^2 + InitialVerticalSpeed * time + initialHeight

horizontalSpeed = InitialHorizontalSpeed

verticalSpeed = -g*time + IntialVerticalSpeed

Three of these are time-dependent, thus their values change with time. We need to compute these at some time instances. We will use the total time that the projectile is traveling before it comes to stop and will create equally spaced time instances between 0 to TMAX. TMAX is computed as:

TMAX = 2*v0*sin(angle0)/g

Lab Activity (1)

Now we will create time intervals between 0 to TMAX using a simple MATLAB command. But first we need to enter the initial values for speed, angle, and height. After each command press enter to execute that command. The values shown in font green are those that MATLAB displays. Pay attention that I have converted the angle to Radian by multiplying it by (pi/180).

>> v0 = 40

v0 =

40

>> angle0 = 35 * (pi/180)

angle0 =

0.6109

>> g = 9.8

g =

9.8000

>> initialHeight = 0

initialHeight =

0

>> InitialVerticalSpeed = v0*sin(angle0)

IntialVerticalSpeed =

22.9431

>> InitialHorizontalSpeed = v0*cos(angle0)

InitialHorizontalSpeed =

32.7661

>> TMAX = 2*v0*sin(angle0)/g

TMAX =

4.6823

In this example our projectile motion lasts 4.6823 seconds. Now let's create the time intervals to monitor the motion from the time that the projectile is sent of to the time that it will come to stop. The following command automatically generates different times and will store all of them in a variable that we will also time (actually time is what we refer to as an array of times).

>> time = 0: TMAX/10:TMAX

This simple call has three parts:

The first part indicates the time you want to start the time interval from (we start from 0 in our case)

The second part indicates the time steps (our time step is 1/10th of the TMAX)

The last part indicates the maximum time (maximum time in our case is the TMAX)

If you type the command:

>> time = 0: TMAX/10 : TMAX

you will produce:

time =

Columns 1 through 6

0 0.4682 0.9365 1.4047 1.8729 2.3411

Columns 7 through 11

2.8094 3.2776 3.7458 4.2140 4.6823

Now we can use this time intervals to compute distance, height, horizontalSpeed, and verticalSpeed at these time intervals. Type the following commands:

>> distance = InitialHorizontalSpeed*time

Columns 1 through 6

0 15.3419 30.6838 46.0258 61.3677 76.7096

Columns 7 through 11

92.0515 107.3934 122.7354 138.0773 153.4192

>> height = -(1/2)*g*time .^2 + InitialVerticalSpeed * time + initialHeight

Columns 1 through 6

0 9.6683 17.1880 22.5593 25.7821 26.8563

Columns 7 through 11

25.7821 22.5593 17.1880 9.6683 0.0000

horizontalSpeed = InitialHorizontalSpeed

32.7661

verticalSpeed = -g*time + InitialVerticalSpeed

verticalSpeed =

Columns 1 through 6

22.9431 18.3544 13.7658 9.1772 4.5886 0

Columns 7 through 11

-4.5886 -9.1772 -13.7658 -18.3544 -22.9431

Now we have generated data for distance, height, horizontalSpeed, and verticalSpeed at these time intervals. Let's plot them to see how they behave with time. That is where we can benefit from the power of MATLAB for doing this kind of work.

A simple plot of distance vs. time can be obtained using this simple MATLAB command:

>> plot( time, distance)

Which results in this plot.

So, this plot shows that distance changes linearly with time. In fact, we can observe that distance has increased by time. This is a good graph but we wish to add something to it to help viewers understand it better. Things like a tittle, caption, etc...

By executing the following three lines, I can add the tittle, and x and y labels.

>> title('Plot of Projectile Distance Vs. Time')

>> xlabel('time (sec)')

>> ylabel('distance (m)')

Below is the outcome.

Here is the list of all commands so far. You can save it in a .m file and execute it.

v0 = 40

angle0 = 35 * (pi/180)

g = 9.8

initialHeight = 0

InitialVerticalSpeed = v0*sin(angle0)

InitialHorizontalSpeed = v0*cos(angle0)

TMAX = 2*v0*sin(angle0)/g

time = 0: TMAX/10 : TMAX

distance = InitialHorizontalSpeed*time

height = -(1/2)*g*time .^2 + InitialVerticalSpeed*time + initialHeight

verticalSpeed = -g*time + InitialVerticalSpeed

plot( time, distance)

title('Plot of Projectile Distance Vs. Time')

xlabel('time (sec)')

ylabel('distance (m)')

How to create a Script?

Step 0: Click on File, then New, then Script - This will open a blank page

Step 1: Cut and paste the above code (everything in font red)

Step 2: Save the file using File, then Save As in your working Directory

Step 3: To execute this program at the MATLAB prompt type the name of the file you just saved (without .m at the end)

From now on you can modify this file as you wish and execute it to generate the graph. You can even add other things in this file.

Now, let me show you something even nicer. You can actually save this graph in several different formats for use in your class presentations. For example you can save this plot as a jpg image or as a bmp image and then insert it into your PowerPoint documents. Here are the steps to do this:

Step 1: On the plot, left click on the File at the top left, then left click on Save As...

Step 2: Choose the directory in which you want to save the file - at the top Save in: Use the arrow to browse to Desktop

Step3: Choose a file name for this image - for example File name: DistanceVSTime.jpg

Step4: Choose a file format - Save a type: JPEG image *.jpg

Step5: Click on Save

Once you created a graph you can use the MATLAB Graphical User Interface to make modifications or add things to it. Chapter 4 of the MATLAB Plotting Manual describes this in detail.

Lab Activity (2)

Let's follow the same procedure and create the graph for height of the projectile vs. time. This time we will create a plot but I want you to use the GUI to add the labels, legends, titles, etc. Note that we have already created the values for height and have stored them in variable height, so what we need to do now is to plot that.

>> plot( time, height)

You will get:

Now use the GUI to add things to this graph. Below you will see the MATLAB screen for the graph in which you will view your graph. You can get to the Edit Mode by clicking on the white arrow at the top of the screen, then by double clicking on the object you wish to modify.

This a MATLAB file that includes some of these activities, click here to download.

Assignment

1. Create similar graphs for height, horizontalSpeed, and verticalSpeed. Make sure each one of these graphs has correct labeling. Save each graph on your Desktop in jpg format.

Some interesting plots for this part would be to study the effect of various angles on the same plot. Or the effect of initial velocity on the same plot.