You are on page 1of 9

Cody Anderson

ECE105

ECE105 Homework 5
Sections: Momentum & Kinetic Energy Plots; Plot SCC; More 2d Plot Types; 3d Plot Types
Submission type:
• sections 1a, 1b, 2, 3: write separate scripts, publish to pdf, upload to Canvas
• sections 4-5: work by hand, take scan/photo of work, upload to Canvas
Points: 20

Section 1a: Momentum and Kinetic Energy Plot


Our goal is to create a plot with two curves: one for momentum and one for kinetic energy as a
function of a car’s velocity. The car’s mass is 907 kg and we want to plot its velocity going from
0 to 8 mph.
The governing equations for kinetic energy and momentum are:
EK = ½mv2 L = mv

Where EK = kinetic energy [J] *1 J = 1 kg-m2/s2


L = momentum [kg-m/s]
m = mass of object [kg]
v = velocity [m/s]

The structure of the script should be as follows. Use comments to define units and explain
tricky steps.

%% Section 1: Title and Description


Describe purpose of script, provide author’s name and date

%% Section 2: Declare Parameter Values


Define value for mass
Define minimum velocity
Define maximum velocity

%% Section 3: Perform Computations


Create a vector of velocity values, using the min and max velocity variables defined above
Convert this vector to the appropriate units to be used in the given equation
Compute kinetic energy vector
Compute momentum vector
1
Cody Anderson
ECE105

%% Section 4: Make Plot


Put both Momentum vs. Velocity (blue dashed line) and Kinetic Energy vs. Velocity (red solid
line) on the same plot.
Label the x-axis (with units!). Adjust the font size to 18.
Do not label the y-axis (why? because two different things are plotted against it)
Use a legend to distinguish between the two curves (with units!). Put this in the top-left corner.

Publish your script as a pdf (should display the plot).

Section 1b: Momentum and Kinetic Energy Subplot


Write a new script that does the same thing as Section 1a, with this exception: instead of one
plot with two lines, create a subplot figure that has Kinetic Energy vs. Velocity in the top and
Momentum vs. Velocity in the bottom.
Use no legend. Label each of the y-axes appropriately (with units!).
Publish your script as a pdf (should display the plot).

2
Cody Anderson
ECE105

Section 2: COVID plot

The goal of this script is to make


the plot to the right, showing
the number of confirmed cases
and deaths from COVID-19 in
Arizona.

This data is found in the Excel


file named COVID_AZ.xlsx,
available on Canvas. First,
download that file and put it
into your MATLAB folder. Then,
write a script with the following
sections:

%% Section 1: Title and Description


Describe purpose of script, provide author’s name and date

%% Section 2: Load in and extract data


Use xlsread to load in the data from the file (explore this matrix to see what columns hold what
data)
Use array indexing to extract the column of data holding the number of cases
Use array indexing to extract the column of data holding the number of deaths

%% Section 3: Create vector for x-axis


Use the length function to identify the number of days in the data set
Create a vector that goes from 1 to the number of days in steps of 1

%% Section 4: Make plot


Use the bar function twice, as well as hold on
Include a title and x-label as shown (with a good font size)
Include a legend as shown

Publish your script as a pdf (should display the plot).

3
Cody Anderson
ECE105

Section 3: Plot SCC


Create a plot that looks like the image
to the right, with these specifications:

• Colors go green, yellow, green


• Letter outlines are black
• Each letter is 10 units tall and 7
units wide
• The linewidth for each letter is 5
• The bottom-left corner of the S
is at coordinate (1,1)
• The figure axis is set to nicely
frame the letters

The starting script below provides some large clues. Begin with this and make edits/additions as
needed. Note that this example script provides a blue letter outline, when it should be black.

Publish your script as a pdf (should display the full SCC plot).

4
Cody Anderson
ECE105

Section 4: More 2d plot types


Semilog
A logarithmic axis is useful when data on one plot crosses multiple orders of magnitude (i.e.
some large numbers, some small numbers). The following three commands create 2d plots
(similar to the plot command), but with at least one axis on a logarithmic scale:

• semilogx – x-axis is logarithmic, y-axis is linear


• semilogy – y-axis is logarithmic, x-axis is linear
• loglog – both axes are logarithmic
Type in the following commands into the Command Window. Then answer the questions

load census adds which two variables to the Workspace?

Looking at the plot, in the year 1810, approximately what is the U.S. population?
Can you be very precise with this estimate? Why not?

Enter the commands above into the Command Window. Then answer the questions.
What does the figure command accomplish?

In the year 1810, approximately what is the U.S. population?


Can you be more precise reading the semilog plot as compared to the linear plot? Why?

5
Cody Anderson
ECE105

Why is it advantageous to plot data with a large range of values on a logarithmic axis?

Would it make sense to use a logarithmic x-axis for this example? Why or why not?

The U.S. population appears to have a near-perfect exponential growth through year 1890
(straight line on the semilog plot). Make a conjecture as to why this tapers off in the last
century:

Plot gallery
Perform an internet search for “types of MATLAB plots”. Select the Mathworks link.
How many different plot types are shown?
What is the difference between a bar plot and a barh plot?

Give an example of where an errorbar plot is useful:

Give an example of where a compass plot is useful:

6
Cody Anderson
ECE105

Section 5: 3d plot types

There are two classes of 3-dimensional plots to be aware of now: line plots and surface plots.

3d line plots are accomplished with the plot3


command. They have one independent variable and
two dependent variables (you will use these
extensively for parametric equations in later math
courses). An example of creating one is shown here:

Here, t is the independent variable. Both x and y are computed as a function of t. In other
words, for every one value of t (let’s choose t = 10), there is a corresponding value for x (.055)
as well as for y (.123). Put these together and you have an ordered triplet which describes the
top point in the plot as (x, y, t) = (.055, .123, 10).

To give this plot a physical meaning, we could use it as a model for a spring’s position as a
function of time. We flick the spring at t = 0 seconds and monitor it until t = 10 s. At first the
vibrations both side-to-side (x position) and up-and-down (y position) are large, but as time
goes on they get smaller.
In a script, replicate the 5 commands given above. Run the script.
What does the command grid on accomplish?

Add commands to the script which will add in the three axis labels shown in the figure above.
Edit script line 4 so that the plot line has a width of 3.

Increase t’s step size to 1. Run the script. Describe what happens to the plot and explain why.

For fun, change sin(2*t) to sin(t). Examine the plot.


7
Cody Anderson
ECE105

There are three types of 3d surface plots: contour, mesh, and surface. They have two
independent variables and one dependent variable.

surf mesh contour (plus colorbar)


As seen here, these plots all show the same information in slightly different ways. The only
difference between surface and mesh plots is that mesh plots show only the grid lines while
surface plots fill in rectangles that lie between the grid lines. Contour plots are simply mesh
plots viewed from directly overhead. From this perspective, you can’t read the z values from
the grid, so a colorbar is necessary to provide the numeric z values.
In these plots, x and y are the independent variables and z is computed as z(x,y) = x2*y
Looking at the grid lines, notice how x values are always a whole number between 0 and 4 (e.g.
x = 2.7 doesn’t truly exist on these plots). Similarly, y only takes on the values 2, 5, 8, or 11.
Once those grid lines are established, the z is computed at every intersecting point.

In a script, replicate the given commands to create the example surface plot.
Write the contents of vector x:
Write the contents of vector y:
Write the contents of matrix x_grid:

Write the contents of matrix y_grid:


8
Cody Anderson
ECE105

x_grid and y_grid are the same size. Explain why this is important for script line 16.

Explain what the meshgrid command accomplishes

Write the contents of matrix z

Complete the following ordered triplets


(x, y, z) using the contents of the matrices

• (0, 2, ____)
• (3, 5, ____)
• (____, 11, 44)
• (____, _____, 72)

Circle each of these ordered triplets on


the given figure

Do each of the ordered triplets satisfy the governing equation z(x,y) = x2*y ?

You might also like