Computer Lab 2

advertisement
Math 1322, Fall 2002
Computer Lab 2
1. Sometimes we need to apply the ideas and techniques from calculus not to formulas but to
data collected in some survey or experiment. Usually, we think of the data as being “partial
information” about some function for which we don't have an exact formula. Since Matlab works
with arrays of numbers, we can simply enter the data in arrays and proceed with what we want to
do.
The following data is taken from Exercise 12 in Section 5.1 of the text:
On May 7, 1992, the space shuttle Endeavor was launched on mission STS-49,
the purpose of which was to install a new perigee kick motor in an Intelsat
communications satellite. The table, provided by NASA, gives the velocity data
for the shuttle between liftoff and the jettisoning of the solid rocket boosters. We
can think of this data as “partial information” about the velocity function v(t).
Event
Launch
Begin roll maneuver
End roll maneuver
Throttle to 89%
Throttle to 67%
Throttle to 104%
Maximum dynamic pressure
Solid rocket booster separation
Time (s)
0
10
15
20
32
59
62
125
Velocity (ft/s)
0
185
319
447
742
1325
1445
4151
Note about saving and loading data
Since this is a short data set, it wouldn't be much trouble to type it all in each time you need it.
But repeatedly reentering the same data would be a nuisance for a large data set. You should
have a way to enter the data once and save it in a file for future use.
Pick a name for a file (for example, filename)
save filename t v
saves the arrays t,v (or whichever you specify)
in a file named filename.mat
save filename
saves all the arrays currently defined in filename.mat
load filename
will reload the arrays stored in filename.mat
(The command “save” (by itself) saves all currently defined variables in a file with the
default name matlab.mat. The command “load” (by itself) loads the variables in matlab.mat)
The files are saved by default in Matlab's “Work” folder. If you want to save to (or reload from)
the diskette drive a: you can replace filename with a:filename
These files end (automatically) with the extension .mat and they are “binary files”  you won't be
able to read them if you try to open them in an editor.
Note: If you have data, separated by spaces, in a text file with the name, say, filename.txt
you can also load this data into a Matlab variable (say, z) by using a command like
z = load( ' filename.txt' )
This could be useful if you wanted to use a text editor (like Notepad) to record some data for
future use in Matlab. Also, some data collection devices can write data to such a text file while
they collect it.
Enter the NASA data in arrays (variables) t and v. Save v and t in a file (named nasa,
perhaps?). To see that this has worked, enter
clear
who
load nasa
who
to erase all your variables
to check that no variables are currently defined
to reload v and t
to check that v and t have been reloaded.
aÑ Use Matlab to make two plots in the same figure window: one plot with "+" signs
marking the data points only, and a second plot joining the points into a graph. The result will be
a graph with the data points also marked.
You can do the first plot using plot( t,v,'+' ). Then give the command
hold on
to prevent the second plot command from erasing the result of the first. (The command
hold off will let the figure window be overwritten later when you want to do something else.) Or,
you can do the both plots in one command: see the Matlab notes or try using the command
help plot ).
Appropriately title the graph, label the axes, and hand it in.
b) To find the total distance traveled by the rocket during this time interval, we need to
"#&
evaluate '! @Ð>Ñ .>. We don't have a formula for @Ð>Ñ, but we have the data (“partial information”
about the velocity function @Ð>Ñ ), and we can use the data to make some approximations  using
left and right endpoint approximations, and the trapezoidal approximation (the average of the left
and right approximations).
There are some differences from what we've done before:
1) As may often be the case with real data, the t-values are not equally spaced
When we do the approximations, we will simply have a different widths (?t values) for the bases
of the rectangles.
2) We don't have to evaluate the velocity function @Ð>Ñ at the endpoints (we don't
even have a formula for it!). The array v already contains the values of v for each value of t.
Two new Matlab commands can be helpful now:
a) diff
If z = [1,3,7,6,11], then diff (z) = [2,4,  1,5]
The command diff computes the differences of the numbers in an
array z. You can use this command to get the array of ?t values you
need (without doing the subtractions by hand).
b) Using array addressing:
If z = [1,3,7,6,11], then
w = z(1:3) gives an array w consisting of the first three elements
of z: w = [1,3,7]
Since length(z) = 5,
w = z(1:length(z)  1) produces w = [1,3,7,6]
You can use similar commands to “extract” from the t and v arrays the
left (or right) endpoint values that you need (without having to retype
data).
Write and print an m-file that computes the left, right, and trapezoidal approximations the total
distance traveled by the rocket. Report the values. Based on this data, which seems likely to be
the best estimate of the distance traveled? Explain. Could we do a midpoint approximation?
"
2. Integrals like '" / # .B are very important in probability theory. For example, the value of that
integral is “related” to how likely you are to get an SAT score in a certain range. ÐWe'll take a brief
look at the connection when we get to Section 6.7 in the text.) However, there is no elementary
B#
B#
antiderivative for the function / # . (An “elementary antiderivative” is one that can be built up
from polynomials, trig and inverse trig functions, ln and exponential functions by using addition,
subtraction, multiplication, division, composition and n>2 -roots.) Therefore we can't use the
Evaluation Theorem to find the value of the integral. We must settle for an approximation.
B#
hand in.
a) Use Matlab to graph the function C œ / # over the interval [  3,3 ]. Print the graph to
b) Write an m-file to compute the midpoint approximations to this integral on the smaller
interval [  1,1], and use it to find the approximations for n = 10, 100, 1000, 10000. (As a test that
your file is working correctly, it should give the value 1.7241 for n = 4.) Use Matlab to put these
results in a two-column table, the first column listing n and the second column listing the values of
the midpoint approximation. (You made a similar table in Lab 1). Use the command format long
g when you display the table. Hand in the m-file (with any one of the n values) and the table.
3. In Matlab, you can create your own user-defined functions which you can then use just like the
built-in functions sin, cos, exp, etc. To do this, you must write the definition of your function in a
as a special m-file (called a “function m-file”), and it must be stored in your working directory 
usually Matlab's “Work” folder.
B
Here's a function m-file that creates a user-defined function f(x) = "B
# . It's best to use names
that Matlab won't confuse with variables or common functions: don't give your function file a
name like function “sin” or ”x” !! Of course, you can use a descriptive name instead of a singleletter name, if you like: if you're defining a function v(t) to represent velocity, you could call it “vel”
instead of “v” . Be sure to read my comments in the example below
function y = f(x)
y = x./ (1+ x .^2);
% a function m-file must start with the word "function"
% specify the formula; use the " ; " so that each
% time you use "f" you don't get all its values
% printed on the screen
Save the m-file with the name f (SAME as the name given in line 1 of the file!) in your working
directory. As long as it's there, you can then use f in just the way you'd use sin, cos, ...
The sole purpose of the file is to define the function “ f ” Do not put anything else in this file as
steps to solving other parts of the problem. In another m-file you can use the function “f” just as
you would use “sin”, “exp”, etc.
Write this m-file and test it by using Matlab to evaluate f(0.173). It should give the value
f(0.173) = 0.1680
a) Using your function m-file, have Matlab plot the graph of f(x) over the interval [-1,1].
Add appropriate titles and labels. Add the coordinate grid to the picture. Turn in the graph.
b) Compute the midpoint approximation for '" f(x) dx for n = 100. (You should be able to
use the m-file from Problem 2 with only a minor modification. You should use the new function f
in your m-file rather than typing out its formula in the m-file.) Print the m-file and write on it, by
hand, the value of the approximation for n=100.
"
c) The exact value of the integral is 0. You could see this if you could find an
B
antiderivative for "B
# (can you?). Explain how you could have predicted the exact value for
geometric reasons.
Download