Vous êtes sur la page 1sur 6

Week 2 homework

ENGR 112

Arrays and plotting

Homework goals:
Understand how to create arrays, calculate with them, and extract data from them. Plot
the results of calculations. Be able to split up plots into multiple windows, plot multiple
things in the same window with different colors and attributes.
Homework expectations/How to start:
1) See previous homework expectations.
2) Plots: Make sure to label the axes (include units where known) and put in a title.
When plotting more than one thing on a graph, include a legend.
3) There are example scripts for plotting and vectors. If youre stuck, try looking at
the example scripts and find one that does something similar to what you want to
do.
4) In the editor you will often see orange and red bars on the right-hand side. The
orange bars are warnings, the red bars are errors. Put your cursor over the bar and
it will tell you what the problem is (eg, in this case, its a message that says use a
semi-colon to suppress output)

5)
Grading:
30% Comments/pseudo code: As before.
20% Output: As before. Plus: did you label your graphs/plots?
50% Functionality: Does your script compute/plot the correct value(s)? Did you
use the correct ranges for input variables?
Learning objectives:
How do you make, edit, and use an array? How do you write an equation using variables
that are arrays? How do you plot those variables? More practice on turning word
problems into MATLAB code.
New Matlab commands you will need:
plot(x,y) plot x versus y
linspace( firstValue, lastValue, numElements ) make an array
length(array) number of elements in an array
start:space:end or start:end - colon operator
fprintf(formatted %3.2f printing\n, var); - formatted printing to command window

1)
For the following, write the commands in a script file leaving the
semi-colon off so the answers print in the command window. Turn in
the script and output. Output should include:
a. number of elements in ts1

Week 2 homework

ENGR 112

Arrays and plotting

b. number of elements in ts2


c. first element in ts1
d. first element in ts2
e. sum of ts1
f. minimum value in ts1
g. mean of ts1
h. first element in tsRev
i. last element in tsRev
(Output formatting and comments not required.)
Create a script that first creates two variables, ts1 and ts2. Use linspace to create ts1
and the : operator to create ts2.
o Both ts1 and ts2 should start at 0 and end at 1, and have (approximately)
100 elements.
Check that the number of elements is approximately correct using the command
length on both variables
Check that the first element of each array is, indeed, zero, by accessing the first
element using the ts(1) syntax.
Check that the last element of each array is, indeed, 1, by accessing the last
element of each array
o Hint: What do you put in the () to get the last element?
Calculate the sum, min and average (mean) of the ts1 array
o What should the min value be?
o What should the mean value be?
o Use linspace to create an array tsRev that has the same values as ts1, but in
reverse order.
o Check the first and last values of tsRev .

2)
Write the following commands in the same script file from
problem 1. Again, leave off the semicolons so all answers print to the
command window. Every bulleted instruction should produce some
output. Turn in the script and all output. (Output formatting and
comments not required.)
For the following, you will be getting values out of ts1 and putting them in
new variables. All of your commands should be of the form: tsNew = ts1(
first:step:last ). Remember that if you leave step out, it defaults to 1 and
end is a shortcut for length(ts), ie, the index of the last element. Note, if
youre confused about values versus indices, double click on ts1 in the
variable window to bring it up in spread-sheet form. The indices are the

Week 2 homework

ENGR 112

Arrays and plotting

numbers along the top (1 2 3), and the values are the ones below that. All of
your first:step:last should be indices, not values.

Get the 3rd element out of ts1 and put it in a variable t3.
Get the first through third element of ts1 and put the in a variable ts1to3
Get the 3rd through 1st element of ts1 and put them in a variable ts3to1.
Get the elements of ts1 in reverse order and put them in a variable tsRev.
o Check that this is correct by printing out the first and last element of tsRev
Get every other element of ts1 and put it in a variable tsSkip.
o Check that this is correct by printing out the length of tsSkip
Add one to every element in ts1 and store the result back in ts1. Do this three
times. Question: the values in ts1 have changed. Have the values that you created
in the previous steps changed? (You can look at them in the variable window).
o Check the new first and last values of ts1

3)
For the following, write a script. Turn in the script and both plots.
(Output formatting and comments not required.)

Repeat problem 4 from homework 1 (solving equations for y and z), only this
time use array operations and do all three values of x at one time.
Now change your script so that x has 30 elements and goes from 1 to 10
Now add a plot command to plot both y and z on the same graph. (You want plots
of y versus x and z versus x.)
o reminder 1) plot(x,y,r) plots y versus x in red (b does blue)
o reminder 2) hold on keeps MATLAB from overwriting the previous plot

Open-ended problems start here.


4) Given two variables A and B, swap their values. You should not assume that you know what
the values in A and B are, or even that theyre scalar values.
a. Hint: You will need a 3rd, temporary variable
b. Test your script with A = [4 5 2] and B = [6 1 7 9]
c. Print out the values of A and B before the swap using fprintf.
d. Print out the values of A and B after the swap using fprintf.
5a) Write a script to plot the following mathematical function in the domain -5 t 1 in
increments of 0.05.
! = ! ! + 3! + 4
Assume that the x-axis is time measured in seconds, and the y-axis is distance measured in
meters. Print the minimum and maximum values of y for that range of ts, and the t values that
produce the minimum and maximum values (hint, look up the different versions of the function
min/max). Label the axes with time in seconds (t) and distance in meters (y).
5b) Check the minimum value against the analytic solution (hint: minimum and maximum values
occur when the derivative is zero). If you dont want to do the derivative by hand, try this web-

Week 2 homework

ENGR 112

Arrays and plotting

site (http://www.derivative-calculator.net/). This web-site


(http://www.numberempire.com/equationsolver.php) can solve the resulting equation for you.
6) The stress in a spherical pressure vessel (assuming the thin wall approximation) can be calculated by:
!=

!"
2!

where ! is stress (in Pascals), ! is pressure in (in Pascals), R is the radius of the tank (in
meters), and t is the thickness of the tank (in meters). Create a plot of the pressure in a
spherical pressure vessel vs the stress seen by the pressure vessel. Create a second plot of the
thickness vs stress. Use the subplot function to place both plots in separate graphs in the same
window. Use the following data:
Plot 1
1 ! 100 Pa (use increments of 1 Pa)
R = 1.1 m
t = 0.025 m
Plot 2
! = 4 Pa
R=2m
0.01 t 3 m (use increments of 0.03 m)
7) In reliability analysis, the Weibull distribution can be used to model the life of many different systems.
The reliability function for this distribution (which can be used to predict survival probability) is modeled
by:
! !
! ! = exp
!
where R(t) is the reliability as a function of time, is the shape parameter, is the scale parameter, and t is
time. Because reliability is a probability, it does not have units.
Write a MATLAB script to create three plots of reliability as a function of time, using scale parameter
values of 1.2, 1.5, and 2.5. Use a time domain between 0 and 3 years, in intervals of 0.2. For the shape
parameter, use 1.2. Create all lines on the same plot, and use a grid. Use a symbol or different line-type for
each plot, and include a legend.

Week 2 homework

ENGR 112

Arrays and plotting

8) A logarithmic spiral is a shape commonly found in nature, but it also has engineering implications. For
instance, the logarithmic spiral of a nautilus shell enables highly efficient fluid transfer without causing
turbulent flow. The equations to describe a logarithmic spiral are as follows
Polar Coordinates
! = !! !"

Cartesian Coordinates
! = !! !" cos !
! = !! !" sin (t)

a) Create two subplots of the logarithmic spiral in the same plot window, one using polar coordinates and
one using Cartesian coordinates, with the following values:
a=1/2
b=.3
0 10 (use 100 values total)
0 t 1000 (use 1000 values total)
Assume radians in all cases. Titles are required, but axis labels are not required. Yes, the second plot
will be blocky.
b) Find and print the distance ratio between two sets of adjacent points on the 0 radian axis (in the form of
the larger number over the smaller number). For example, in with the following picture, you would find
p2/p1 and p3/p2. The quantity is unitless. Format to 3 decimal places. (Hint: In the polar coordinate
system 0 rad = 2 rad = 4 rad etc)

. .p2

p1

p3

9) A parachutist who is loaded with equipment and has a mass m = 150 kg jumps out of a stationary hot air
balloon. The velocity of the parachutist as a function of time in seconds is given by the equation:

v(t) = gm/c (1 e c t /m )

The drag coefficient is c and g is the gravitational constant is g = 9.81 m/s2. In one MATLAB program,
a) Plot the velocity as a function of time in the domain 0 t 35 s in increments of 1 s for a value
of the drag coefficient c = 13.5 kg/s. Print the value of the velocity at 12 seconds with 2 decimal places.
b) On the same graph as in part (a), plot the velocity as a function of time for c = 9.0 kg/s. Use a symbol or
different line-type for this plot and include a legend. Print the value of the velocity at 12 seconds with 2
decimal places.
c) Print the average velocity over the full time period for each value of c.
(Hint: Remember that MATLAB indices start at 1, while your domain starts at 0.)

Week 2 homework

ENGR 112

Arrays and plotting

10) The dynamic storage modulus G and loss modulus G are measures of a material mechanical response
to harmonic loading. For many biological materials these moduli can be described by Fungs Model:
!
1 + !!! !
! ! ! = !! 1 + !"
2
1 + !!! !
! !! ! = !!! !"#!! !!! !"#!! !!!
where is the frequency of the harmonic loading and !! ,c, !! , and !! are material constants.
(tan-1 is atan in matlab).
a) Plot G and G versus as two separate plots using the following constants:
a. !! = 5 kilo pound per square inch (ksi), c=0.05, !! = 0.06 seconds, and !! = 500
seconds.
b. Let vary from 0.0001 to 1000 seconds-1. Use a log scale for this axis.
b) Why do you want to use a log scale for the axis? Write your answer as a comment in
the script. (Hint: What happens if you dont use a log scale? Try plotting both ways.)

Vous aimerez peut-être aussi