Sie sind auf Seite 1von 22

Matlab for Math 203

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

(x) over [, ] and label the graphs. Put an


arrow on your plot at a local maximum for the function f(x) and identify
the coordinates of that point. Indicate how you found the coordinates.
New Matlab Commands: Transpose operator (), hold on, Hold off,
hold
Drag the image with the mouse to adjust the view so that your graph shows
the plots of f and f

clearly. hold toggles between hold on and hold off. This


gives another easy way to graph several functions on the same set of axes.
5
Lab 2: Scripts and function m-les
Reading Assignment: Jensen, 1.2, 1.3
Exercise 0.4.
(1.2.3.2) Write a script to do the following. On the same graph plot y = lnx over
1/2 x 4 and y = e
x
over ln2 x ln4. Also plot the line y = x over
1 x 4.5. Put the same scale on both axes with the axis equal command.
Put title and axis labels on the graph. Put a legend on the graph. Somewhere on
the graph print the text Symmetry about y = x. Print the graph.
New Matlab Commands: Script m-les, color, line styles, axis equal
One of the advantages of Matlab is the ease of plotting several functions on the
same graph. This exercise shows how to plot several curves over dierent intervals
on the same graph. Your script should simply plot the three functions over the
specied intervals. Then, after running the script, add the labeling and tick marks.
But be sure to use dierent colors for the graphs in your display and dierent line
styles for your printed output. help plot will show you how to use dierent colors
and line styles. Print out your script as well as the graph.
Exercise 0.5.
(1.3.5.4) Plot the function
f(x) =
_
1
x
2
tan

8
x if 0 x 2
x 3 cos

4
x if 2 < x 4
over a suciently large interval that you can estimate its zeros. Use these estimates
in the fzero command to nd the zeros. On the plot of the function include the
zeros among the x tick marks.
New Matlab Commands: Function m-les, , <, , >, fzero
You are asked to construct script m-les and function m-les. These are les
that are saved with the extension .m and will then be used automatically by Matlab
if they are called from the command line. Matlab must, of course, know where to
look for m-les. So you may have to place a directory containing them in Matlabs
path. This is done by going to the main file menu and choosing set path .
This will allow you to add a directory to the Matlabs path in the usual Windows
manner. In particular you can bring m-les on a oppy and immediately execute
them from Matlabs command line if the a: drive is in Matlabs path. Function
m-les are useful tools since they can be used to dene a function which can then
be called from the command line.
Matlab includes a useful m-le editor. Click on the open page icon to start it.
Enter as pi for limits or in formulas.
Exercise 0.6.
Write a function m-le for f(x) = sin(x
2
) cos(x).
Function m-les are constructed by placing the word function at the begin-
ning of the rst line, dening the function, adding comment lines (prefaced with
%) to explain what the function does, and then giving the sequence of Matlab com-
mands. Variables in an m-le are local. The example below sets the rst output
6
variable to the x-coordinate and the second output variable to the y-coordinate of
a circle of radius equal to the input variable r.
function [u,v]=circle(r)
% This function outputs the x and y coordinates of a circle of radius r and
% plots the circle with a blue dashed line.
t = 0 : .01 : 2 pi;
u = r cos(t);
v = r sin(t);
plot( u, v, b)
Function m-les are used on the command line by typing the name of the
function (=name of saved le without the extension .m) together with the input
value(s). You can use the output variables from the command line by entering a
command such as [X,Y]=circle(2). X will then be the x-coordinate of the circle
saved as an array, etc. A script operates in a similar fashion except that you cannot
enter values for a variable.
Remark 0.1. Matlab line specications:
Line
Style
Symbol
Mar ker
Style
Symbol Color Symbol
solid(default) plus + red r
dashed circle o green g
dotted : asterisk blue b
dash dot . point . cyan c
cross x magenta m
square s yellow y
diamond d black k
up triangle white w
down triangle v
right triangle >
left triangle <
pentagram p
hexagram h
7
Lab 3: Plotting simple surfaces
Reading Assignment:: Jensen, 6.2, 6.3
Exercise 0.7.
(6.2.4.2) Plot the plane x + y z = 2. Find a view which shows the plane well.
New Matlab Commands: meshgrid, surf
Start by plotting a plane. Note that meshgrid is to three dimensional plotting
as linspace or the colon operator is to two dimensional plotting. Adjust your view
of the graph by using rotate3d and zoom on the Figure Toolbar.
Exercise 0.8.
(6.2.4.3) On the same graph plot the planes x + y z = 2 and 3x 4y + 5z = 8.
Adjust the view as needed.
Do not calculate the angle between the two planes. Drag the image with the
mouse to adjust the view so that your graph shows the two planes clearly. Recall
that hold toggles between hold on and hold off.
Exercise 0.9.
(6.3.5.5) Plot the graph of the plane x + 2y = 3.
Exercise 0.10.
(6.3.5.6) Plot the graph of the cylinder z = sin3x over 0 x 2, 1 z 1
Exercise 0.11.
(6.3.5.1) Plot the graph of f(x, y) =
_
x
2
y
2
_
2
. Make a plot containing 25 hori-
zontal traces of this surface.
New Matlab Commands: contour3
Now we introduce level curves on the graph. Be sure to look at the similar
pictures in Stewart so that you clearly understand contour lines.
8
Lab 4: Plotting surfaces in other coordinate systems
Reading Assignment: Jensen, 6.5
Exercise 0.12.
(6.5.4.1) Plot the part of the sphere of radius 3 whose latitude lies in the interval
[60

, 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

r (t) = cos(t), sin(t), t , 0 t 2.


>> syms t
>> x=cos(t)
x =cos(t)
>> y=sin(t);
>> z=t;
>> dx=di(x)
dx =-sin(t)
>> dy=di(y);
>> dz=di(z);
>> ds=(dx2+dy2+dz2).5
ds =(sin(t)2+cos(t)2+1)(1/2)
>> ds=vectorize(ds)
ds =(sin(t).2+cos(t).2+1).(1./2)
>> Ds=inline(ds)
Ds = Inline function:
Ds(t) = (sin(t).2+cos(t).2+1).(1./2)
>> Ds(pi/2)
ans = 1.4142
>> L=quad(Ds,0,2*pi)
L = 8.8858
Any line beginning with >> has been entered from the keyboard; other lines
are Matlab responses. Note that several responses have been eliminated by using
;. The symbolic calculation of the derivatives makes the calculation of ds relatively
straightforward. However ds is not a function that Matlab will recognize. This
requires rst using vectorize which prepares ds to accept arrays and inserts the
appropriate . and then declaring it as an inline function using inline. Note that
Ds is used to stand for the Matlab function and ds is used to stand for the sym-
bolic function. Notation like this makes clear what kind of an object ds or Ds is.
Incidentally I could now create an array T with linspace or the : operator and
calculate Ds(T) which would give an array as a result.
11
Lab 6: Contour Plots and Tangent Planes
Reading Assignment: Jensen, 8.1, 8.3
Exercise 0.17.
(8.1.7.1) Plot the graph of f(x, y) = xy
3
x
3
y over the domain 3 x, y 3.
Make a separate contour map of this function over this domain. Label the levels.
Use surfc to plot the graph and contour map in the same window.
New Matlab Commands: contour, clabel, surfc
Use clabel to identify some, but not all, level curves. Include at least z = 1
and z = 10.
Exercise 0.18.
(8.3.5.1) Plot the graph of z = sin(x
2
+ y
2
) and its tangent plane at the point
(1, 1, sin2) .
Do the computations rst with paper and pencil. Zoom in until the surface
and plane are indistinguishable. Print the graph from a viewpoint which clearly
shows the tangent plane and the graph.
12
Lab 7: Gradients
Reading Assignment: Jensen, 8.1, 8.3
Exercise 0.19.
Graph the function z = x
3
+y
3
+3xy, print it, and predict what the level curves will
look like by drawing some on the graph. Then graph a contour map of x
3
+y
3
+3xy
and add a eld of gradient vectors to the second plot. Use axis equal so that the
picture will look right. What do you observe about the relationship between the level
curves and the gradient vectors?
New Matlab Commands: quiver
Exercise 0.20.
(8.5.3.1) Graph and make a contour plot of f(x, y) = 2x
3
+ xy
2
+ 5x
2
+ y
2
over
the rectangle 2.5 x .6, 3 y 3. Locate all the critical points by hand on
the contour plot and identify them as local max, local min, or a saddle point. Then
add a gradient eld to the surface and identify local max and local min with max
and min. What is the behaviour of the gradient at the critical points? Check your
values by calculating the partial derivatives. Use Matlab symbolics for extra credit.
New Matlab Commands: quiver3, max, min
13
Lab 8: Maxima and Minima
Reading Assignment: Jensen, 8.1, 8.3, Stewart, p. 983
Exercise 0.21.
(8.5.3.2a)) Use graphical methods to estimate the extrema of f(x, y) = e
xy
on
the domain 4x
2
+ y
2
1. Put on the graph the maximum and minimum values
and whether it occurs in the interior or on the boundary. Use polar coordinates to
parametrize the domain over the parameter domain 0 r 1, 0 2. Select
a view that allows you to clearly identify the extreme values.
Verify your result with derivative calculations.
Exercise 0.22.
Do the Applied Project, page 983 of Stewart: Multivariable Calculus. You may
either measure the dimensions of a dumpster at City College or use the following
measurements taken from a dumpster in a loading bay in front of NAC: Length =
24

, 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.

Das könnte Ihnen auch gefallen