Beruflich Dokumente
Kultur Dokumente
Department of Mathematics
3
Introduction
Matlab is a powerful software program that uses arrays of numbers to do many
things. The labs that follow are designed to introduce some of its graphic and
calculus capabilities. Mostly the labs consist of selected problems from Using
MATLAB in Calculus by Gary Jensen with an occasional one from Stewart.
Using MATLAB in Calculus contains examples and detailed information about
the various Matlab commands we will use. Since the lab period is only one hour, you
should prepare for it by carefully reading ahead of time the material and problems
to be covered. You will be working in pairs at the computers, but you will be
tested individually at the end of the semester. So be sure to spend some of your
time working at the keyboard. In addition you will probably have to spend some
time outside of class working on the lab exercises. But by the end of the semester
you should have developed a reasonable facility with Matlab that you can use in
other courses.
Using MATLAB in Calculus was written some time ago and so omits some
of the more useful features of Matlab. The comments in each exercise sometimes
suggest easier ways of carrying out an exercise. A quite useful feature, that is
not mentioned, is the ability to recall a previous command line by using the up-
and down-arrow keys and modify it. (Try it!) You can also use the Windows
cut-and-paste feature to enter a new command line.
4
Lab 1: An Introduction to Matlab
Reading Assignment: Jensen, 1.1
Exercise 0.1.
(1.1.12.4) Graph s = 2t/(1 + t
2
) over the interval 3 t 3. Take increments of
approximately .01 for the independent variable. Put you names and a description
of the function in the title. Label the axes.
New Matlab Commands:: colon operator (:), array Multiplication,
array division, array exponentiation, plot
All labeling of the graph should be done interactively by using the cursor,
selecting the insert menu or symbol from the Figure Toolbar, and then entering
or editing text, etc.
Exercise 0.2.
(1.1.12.5) Graph x =
cos(t)
4
, y =
sin(t)
9
over 0 t 2.
New Matlab Commands:: None
This is a plot of a parametrically dened curve. Do not do the labeling from
the command line. Use the Figure Toolbar on the graph instead.
Exercise 0.3.
Consider f(x) = sin(x
2
) cos(x).
(1) Print a table of (x, y) coordinates with 25 entries for x . (Use
the Windows copy and paste commands to print the table in Notepad.)
(2) Evaluate f
(x) by hand.
(3) Print a plot of f(x) and f
, 60
] .
The scale on the x and y and z axes are not equal, and this will make images
look odd. You can correct for this with the axis equal command. See 6.3.4. This
will often be the case when using spherical coordinates.
Exercise 0.13.
(6.5.4.2) Plot the cone z
2
=
_
x
2
+ y
2
_
/9 and the cylinder y
2
+ z
2
= 1 on the same
graph.
Use hold on to create the second image.
Exercise 0.14.
Plot the surface z =
x
2
y
2
x
2
+y
2
near (0, 0). Does lim
(x,y)(0,0)
z exist? Carefully explain
your answer by printing and referring to a graph.
9
Lab 5: Curves and Arc Length
Reading Assignment: Jensen, 7.1
Exercise 0.15.
(7.1.4.1) Graph the vector function
r (t) = cos t, cos 2t, cos 3t , over 0 t 2.
Use rotate3d to experiment with the view. Print the view which you think best
exhibits the curve. By hand, mark with arrow heads the direction of movement on
the curve as t goes from 0 to 2. By hand, or with the text command, mark and
label the points on the curve where t = 0, /2, and 3/2. Is this curve smooth?
Find the length of the curve.
New Matlab Commands: plot3, quad
Omit stem3 from the problem of plotting this three dimensional parametric
curve. First plot it in red with a spacing of .15, hold the graph, and then repeat
with a spacing of .05 in blue. Recall how the arc length formula was obtained as
you view this graph. Calculate the polygonal length for these two spacings and
then calculate the length using quad, Matlabs procedure to calculate the integral.
Consult 3.3 or 7.3.1 for details on calculating the polygonal length. See help quad
for details. Print the graph with the ner spacing after identifying the points (try
using o as a line style) and putting in the arrow and the text box with the Figure
Toolbar commands.
Exercise 0.16.
(7.1.4.3) Plot the parabolic cylinder y = x
2
(see Section 6.3.3) and the top half of
the ellipsoid x
2
+ 4y
2
+ 4z
2
= 16, that is, where z > 0, (see Section 6.5.3) on the
same graph to show the curve of intersection. Find parametric equations for this
curve and plot it on the same graph, in red, with the plot3 command. Use axis
equal and nd a good view. Find the length of the curve.
New Matlab Commands: syms, diff, vectorize, inline
This time rst graph the two surfaces and use plot3 to then graph the curve
of intersection using o marks for the curve and the color green so you can see it
clearly. Next use the symbolics toolbox to calculate the derivatives whose squares
you need to calculate the length of the curve. You must rst declare your variables
with syms and then you can dierentiate functions with diff. (This is a Symbolic
Toolbox diff.) vectorize is then used to convert the symbolic expression into
vectorized form, i.e. vectorize prepares them to accept arrays and inserts . at
the appropriate places. You can then enter the function into quad. See Matlab
help or the appendix for details. Compare your result with the polygonal length of
the curve. Place these values on your graph.
Numerical integration is done with the quad command. Matlab will only
integrate functions that have been dened in vectorized form. This can be done
either by
(1) entering a vectorized string expression in one variable into quad or
(2) by dening an inline function rst and then using it in quad.
The following example shows the syntax:
Q=quad(1./(x.3-2*x-5),0,2);
10
which calculates numerically
_
2
0
1
x
3
2x5
dx or by the sequence of commands
F = inline(1./(x.3-2*x-5));
Q = quad(F,0,2);
The second method makes the function F =
1
x
3
2x5
available as a function for
other command lines. Note that you do not have to use fcnchk as described in
Jensen.
One of the strengths of Matlab is its access to the Maple kernel which makes
symbolic computation possible. This substantially simplies the calculation of
ds =
_
(x
(t))
2
+ (y
(t))
2
+ (z
(t))
2
dt
as we will see.
Below is the Matlab calculation of the length of the helix given by
, width = 6
, and height = 5
.
Exercise 0.23. (Extra Credit)
(8.4.4.3) Plot the surface x
2
+ 3y
2
+ 4z
2
= 9 by parametrizing it and on the same
graph plot the normal vector eld given by the gradient of x
2
y
2
.
New Matlab Commands: quiver3
See 6.5.3 for how to graph an ellipsoid. Be sure to use axis equal so your
graph is not distorted. For additional drama make your normal vectors red.
14
Lab 9: Double Integrals
Reading Assignment: Jensen, 9.1, 9.2, 9.3, p. 46
Exercise 0.24.
(9.2.3.1) Use Fubinis Theorem to express
_ _
R
xsin(x+y)dA, R = [0, /6][0, /3],
as an iterated integral. Use dblquad to nd a numerical estimate of this integral.
Use the symbolic toolbox to evaluate the integral symbolically. Plot the graph of
xsin(x + y) and use the text command to report the numerical estimate and the
value of the symbolic calculation (converted to oating point decimal) on the graph.
New Matlab Commands: dblquad, int
Check the help on dblquad for up to date details on how to use this operation.
This exercise will require converting a symbolic expression involving arithmetic
operations and to a number. This can be done with the double command (=
double precision). See the middle of p. 46 of Jensen for details.
Exercise 0.25.
(9.3.3.1) Consider the integral
_ _
D
x
3
+ 1dA, where D is the domain bounded by
y = x
2
, x = 1, and the x-axis.
(1) Calculate the Riemann sum with m = n = 50. Use the fcnchk command
to extend z =
x
3
+ 1 by 0 outside of D.
(2) Graph z, extended by 0 outside D, over the square 0 x, y 1. Use the
text command to report the value of the Riemann sum on it.
(3) Calculate the integral by hand.
(4) Use dblquad to estimate the integral. If dblquad doesnt return an answer
because of too much discontinuity, use as an optional method the Midpoint
Rule or write a function m-le which implements Simpsons Rule and use
it.
(5) Plot the curves dening D. Use the text command to label the curves, to
label D and to report your Riemann sum, the exact value and the estimate
given by use of dblquad.
New Matlab Commands: sum
Note that we dont have the le Midpoint that appears in that script. You
will have to think carefully about how to set up the Riemann sum. Also you cannot
use the relational operator when constructing a symbolic function. Hence you will
have to dene an inline function directly, or use a string, in dblquad.
15
Lab 10: Surface Area and Triple Integrals
Reading Assignment: Jensen, 9.4, 9.5
Exercise 0.26.
(Stewart: Multivariable Calculus, 16.6, #16) Find the exact area of the surface
z = 1 + x + y + x
2
, where 2 x 1 and 1 y 1. Place the text of the
integration command and the answer in a text box on the graph of the surface over
the domain. Note that the meshgrid and surf commands approximate the actual
surface by faceted triangles in a way very similar to 16.6 of Stewart.
Exercise 0.27.
Let V be the solid bounded by z = x
2
, z = 8 x
2
, y = 1 + x
2
, and y = 1.
(1) Plot the rst three functions on one graph, and imagine where the plane
y = 1 is located on that graph. By rotating the graph, decide on an
appropriate order of integration to nd the volume of V.
(2) Calculate the limits of integration either by hand or using fsolve.
(3) Use the Symbolic Toolbox to calculate the volume. Place the text of the
integration command and the answer in a text box on the graph generated
in a).
16
Lab 11: Sequences and Series
Reading Assignment: Jensen, 5.1, 5.2
Exercise 0.28.
A sequence a
n
is dened recursively by a
1
=
3 and a
n+1
=
a
n
+ 3 for n 1.
Plot the rst 50 terms, marking each with a dot, and guess the limit to 4 decimal
places. Then nd the exact value of the limit by solving an equation, and check
your guess.
New Matlab Commands: for. . .end
In a recursive sequence like this one you cannot use the : operator. Hence a
piece of Matlab programming technique is required. The plot command is also used
in a new way.
Exercise 0.29.
(5.2.3.1) Use symsum to nd
1
1
n
2
. Compare the answer numerically to the value
of the hundredth partial sum obtained by using the cumsum command. Graph the
rst 100 partial sums and use the text command to print on it the symbolic value
and the value of the hundredth partial sum.
New Matlab Commands: cumsum, symsum, inf
cumsum gives the sequence of partial sums of a series dened by a Matlab array.
Hence it can be used to display the sequence of partial sums when plotted.
Some series, such as
1
1
n
2
can be summed symbolically by introducing the
appropriate symbolic variables and calculating symbolically with symsum. You
need not use the text command in this problem since it is easier to use label and
place the result directly on the graph.
17
Lab 12: Approximation
Reading Assignment: Jensen, 5.3, 5.4
Exercise 0.30.
(5.3.2.1, omit d)) Consider the series
1
a
n
where a
n
=
ln n
n
2
.
(1) On the same graph make a dot plot of the rst 25 terms of this series and a
plot of the graph y = f(x), where f(x) =
ln x
x
2
. At the bottom of the printed
graph, record your interpretation of the evidence in favor of f satisfying
the conditions of the integral test. Use the integral test to show that the
series converges. This requires integration by parts. You may check your
answer by using the symbolic toolbox, but also do the work by hand.
(2) Use the remainder estimate for the integral test to nd an upper bound
for the remainder R
n
= s s
n
. What is the smallest value, n
0
, of n such
that this upper bound is less than .05?
(3) Use MATLAB to nd s
n
0
. Use the symbolic toolbox command symsum
to nd the actual sum s. Calculate the dierence and verify that it is less
than .05.
(4) Use the cumsum command to graph the rst n
0
terms of the sequence
of partial sums of this series. Is convergence evident from this graph?
Namely, is it evident that it is bounded? Before answering too hastily,
look at the graph of y = lnx over the same interval. Is it bounded?
You do not need to use the stairs command since you are not asked to do
d). (You will get extra credit if you do part d).) A crucial step in any numerical
problem is estimation. This problem uses the integral test to bound the size of the
remainder. You are to calculate how many terms of the convergent series must be
used to get a partial sum that is within .05 of the actual sum.
Exercise 0.31.
(5.4.2.1) For the series
1
(1)
n1
n
, make a dot plot of the rst n partial sums,
where n is chosen large enough that |s s
n
| < .01. Use the text command to print
on the graph the value of the average of s
n1
and s
n
, for this value of n.
This problem is similar except that you are to use the alternating series test,
instead of the integral test, to nd out how many terms are required to get a partial
sum within .01 of the actual sum.
APPENDIX A
Symbolic Expressions in MatLab
In the appendix any line beginning with >> has been entered from the key-
board; other lines are Matlab responses. Note that several responses have been
eliminated by using ;.
1. Creating symbolic variables in Matlab
To create symbolic variables in Matlab use the syms command:
>>syms x y real
Note that commas do not separate the variables you want declared symbolic.
The real designation is optional. If you are sure that the variables will not take on
complex values, then the real option may produce simpler formulas when Matlab
manipulates expressions that involve radicals or logarithms.
You can now dene a symbolic expression using the usual operators. Note that
vectorization is not applicable in symbolic expressions (at least not directly) and
does not have to be used in the denition. For example,
>>w = x2 3 x
w =
x2 3 x
denes a symbolic expression w. The dependent variable w does not have to be
declared symbolic prior to using it.
2. Evaluating Symbolic Expressions
To evaluate a symbolic expression use the subs command. Consider the fol-
lowing examples:
>>subs(w, 2)
ans =
2
The command subs(w, 2) substitutes the value 2 for the independent variable
in w.
>>subs(w, 1/3)
ans =
0.8889
Here the substitution in w has been done numerically. Since the input is an
exact fraction one might also expect that it should be possible to obtain an exact
fractional value.
>>subs(w,sym(1/3))
ans =
8/9
19
20 A. SYMBOLIC EXPRESSIONS IN MATLAB
The command sym tells Matlab to enter the symbolic fraction 1/3 rather than
the numerical fraction. We can equally well convert the symbolic answer to numer-
ical form using the double command.
>>double(ans)
ans =
0.8889
We mentioned above that symbolic expressions should not be entered with
array operators. Nonetheless, the subs command can evaluate these expressions
quite easily for an array of inputs.
>>subs(w, [1 1 1.5])
ans =
2.0000 4.0000 2.2500
If we have a symbolic expression in two or more variables the substitution
must be carried out more explicitly so that Matlab knows which values are to be
substituted for which variables. For example,
>>z = x2 + y2
z =
x2 + y2
>>X = [1 2 3]; Y = [1, 3, 5];% Lists of values we want substituted for x and y.
>>subs(z, {x, y}, {X, Y }) .
ans =
2 13 34
Observe the set brackets { } that are used to specify the variables in z and
the corresponding replacements. In this example, Matlab replaces the variables x
and y by the pairs obtained from the two arrays X and Y . Thus, it rst evaluates
z for x = 1, y = 1, then for x = 2, y = 3, etc. Observe that we have used
the fact that Matlab distinguishes between the variables x and X. We use the
capital form to hold the numeric values we wish substituted for x. When you
use a symbolic variable such as x, you should never assign a numerical value to x
through a statement such as x = 2. Doing so will destroy the symbolic quality of
the variable.
3. Some Calculus
Dierentiation of symbolic expressions is straightforward. Use the diff com-
mand. For the expression dened earlier we get
>>di(w)
ans =
2 x 3.
This is the rst derivative. Higher order derivatives are readily computed by
including the desired order in the diff command. For example, to nd a second
derivative of w enter
>>di(w, 2)
ans =
2 .
For a multivariate expression we can use diff to nd partial derivatives. Re-
ferring to z = x
2
+ y
2
, we have
3. SOME CALCULUS 21
>>di(z, x)
ans =
2 x
and
>>di(z, y)
ans =
2 y.
The diff command is vectorized, that is it will dierentiate all symbolic com-
ponents in a vector input. This is particularly useful in dealing with curves, for
example. Suppose we wish to study the curve dened by the parametric equations
x = t
2
, y = t
3
. Let us rst add t to our list of symbolic variables.
>>syms t real
Now we dene the symbolic parametric equations dening the curve:
>>r = [t2 t3]
r =
[t2, t3]
The components of the tangent vector to the curve are given by dierentiation
of the component functions.
>>dr =di(r)
dr =
[2 t, 3 t2]
As you know, integration of symbolic expressions may be considerably more dif-
cult than dierentiation. The int command can be used to nd an antiderivative
or to obtain an exact expression for a denite integral. For example,
>>int(w)
ans =
1/3 x3 3/2 x2
produces the anti-derivative (without the constant of integration). Using the pretty
command, you can get Matlab to write the expression in a somewhat easier to read
form.
>>pretty(ans)
3 2
1/3x 3/2x
The indicates that the symbolic variable has certain assumptions in its def-
inition. In this case we assumed the variables represented real quantities. If no
assumptions are made, the letters appear without the symbol.
Denite integrals can be evaluated by including the limits of integration.
>>int(w, 0, 2)
ans =
10/3
which computes
_
2
0
wdx.
In some cases exact symbolic answers cannot be obtained for the integration
problem you wish to consider. In such situations you can evaluate a denite integral
numerically using the quad command. (quad is an abbreviation for quadrature,
which is an old-fashioned name for integration). As a numerical routine, the quad
command expects its input to be vectorized, that is the expression it works with
22 A. SYMBOLIC EXPRESSIONS IN MATLAB
has to be able to accept array inputs. In order to use quad on the symbolic expres-
sion w, the latter must rst be converted to vectorized form using the vectorize
command.
>>W =vectorize(w)
W =
x.2 3. x
Now we can apply quad to the vectorized expression W.
>>quad(W, 0, 2)
ans =
3.3333
Of course we obtain the numerical approximation to the exact symbolic answer
-10/3 found above.
Exercise 3.1.
(1) Find symbolic expressions for the tangent vector to the curve C given by
the parametric equations r =
_
t
2
, 2t
3
t
.
(2) Use the symbolic expression found in 1) to nd a unit tangent vector to C
at the point corresponding to t = 1.5.
(3) Set up a symbolic denite integral (using int) for the arclength of C be-
tween [0, 0] and [1, 1]. Recall that the arclength is given by an integral of
the form
_
b
a
_
x
(t)
2
+ y
(t)
2
dt
Find the numerical (decimal) value using the double command.
(4) Use quad to evaluate the arclength specied in 3). Compare with the
answer you found in 3). Which answer is correct? What lesson is to be
learned from this?
(5) Plot the curve C for 2 t 2.