The Students Introduction To Mathematica
The Students Introduction To Mathematica
The Students Introduction To Mathematica
Second edition
Second edition
Bruce F. Torrence
Eve A. Torrence
CAMBRIDGE UNIVERSITY PRESS
Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo
www.cambridge.org
Information on this title: www.cambridge.org/9780521717892
Preface · ix
1 Getting Started · 1
Launching Mathematica · The Basic Technique for Using Mathematica · The First Computation ·
Commands for Basic Arithmetic · Input and Output · The BasicMathInput Palette · Decimal In, Decimal
Out · Use Parentheses to Group Terms · Three Well-Known Constants · Typing Commands in
Mathematica · Saving Your Work and Quitting Mathematica · Frequently Asked Questions About
Mathematica’s Syntax
4 Algebra · 147
Factoring and Expanding Polynomials · Finding Roots of Polynomials with Solve and NSolve · Solving
Equations and Inequalities with Reduce · Understanding Complex Output · Working with Rational
Functions · Working with Other Expressions · Solving General Equations · Solving Difference Equations ·
Solving Systems of Equations
5 Calculus · 195
Computing Limits · Working with Difference Quotients · The Derivative · Visualizing Derivatives · Higher
Order Derivatives · Maxima and Minima · Inflection Points · Implicit Differentiation · Differential
Equations · Integration · Definite and Improper Integrals · Numerical Integration · Surfaces of Revolution ·
Sequences and Series
viii The Student’s Introduction to Mathematica
8 Programming · 385
Introduction · FullForm: What the Kernel Sees · Numbers · Map and Function · Control Structures and
Looping · Scoping Constructs: With and Module · Iterations: Nest and Fold · Patterns
Index · 461
Preface
The mathematician and juggler Ronald L. Graham has likened the mastery of computer program-
ming to the mastery of juggling. The problem with juggling is that the balls go exactly where you
throw them. And the problem with computers is that they do exactly what you tell them.
This is a book about Mathematica, a software system described as “the world’s most powerful global
computing environment.” As software programs go, Mathematica is big—really big. We said that
back in 1999 in the preface to the first edition of this book. And it’s gotten a good deal bigger since
then. There are more than 900 new documented symbols in version 6 of Mathematica. It’s been said
that there are more new commands in version 6 than there were commands in version 1. It’s gotten
so big that the documentation is no longer produced in printed form. Our trees and our backs are
grateful. Yes, Mathematica will do exactly what you ask it to do, and it has the potential to amaze
and delight—but you have to know how to ask, and that can be a formidable task.
That’s where this book comes in. It is intended as a supplementary text for high school and college
students. As such, it introduces commands and procedures in an order that roughly coincides with
the usual mathematics curriculum. The idea is to provide a coherent introduction to Mathematica
that does not get ahead of itself mathematically. Most of the available reference materials make the
assumption that the reader is thoroughly familiar with the mathematical concepts underlying each
Mathematica command and procedure. This book does not. It presents Mathematica as a means not
only of solving mathematical problems, but of exploring and clarifying the concepts themselves. It
also provides examples of procedures that students will need to master, showing not just individual
commands, but sequences of commands that together accomplish a larger goal.
While written primarily for students, the first edition was well-received by many non-students who
just wanted to learn Mathematica. By following the standard mathematics curriculum, we were told,
the presentation exudes a certain familiarity and coherence. What better way to learn a computer
program than to rediscover the beautiful ideas from your foundational mathematics courses?
The organization of the book has not changed, but there are two notable new additions:
The second edition has exercises, several hundred in fact. These provide a means for experimenting
with and extending the ideas outlined in each section. They also provide a concrete and structured
framework for interacting with the software. It is through such interactions that familiarity and
(ultimately) competence and even mastery will be attained. Complete solutions are freely available
online, as discussed in the next section.
In addition, a new chapter has been added (Chapter 8) to address the fundamental aspects of
programming with Mathematica. While this topic is far too expansive to cover thoroughly in a single
chapter, many of the fundamentals of programming are conveyed here. It is a fact that many of the
new features of version 6 require a working knowledge of pure functions and other ideas that fit
naturally into this context. You are likely to find yourself reading a section of this chapter here and
there as you explore certain topics in the earlier chapters. Think of it as a handy reference.
The first chapter provides a brief tutorial for those unfamiliar with the software. The second delves a
bit deeper into the fundamental design principles and can be used as a reference for the rest of the
book. Chapters 3 and 4 provide information on those Mathematica commands and procedures
relevant to the material in a precalculus course. Chapter 5 adds material relevant to single-variable
calculus, and Chapter 6 deals with multivariable calculus. Chapter 7 introduces commands and
procedures pertinent to the material in a linear algebra course.
¿ Some sections of the text carry this warning sign. These sections provide slightly more
comprehensive information for the advanced user. They can be skipped by less hardy souls.
Beginning in Chapter 3, each section has exercises. Solutions to every exercise can be freely down-
loaded from the website at www.TUVEFOUTNBUIFNBUJDBDPN.
Mathematica runs on every major operating system, from Macs and PCs to Linux workstations. For
the most part it works exactly the same on every platform. There are, however, a few procedures
(such as certain keyboard shortcuts) that are platform specific. In such cases we have provided
specific information for both the Mac OS and Microsoft Windows platforms. If you find yourself
running Mathematica on some other platform you can be assured that the procedure you need is
virtually identical to one of these.
Preface xi
Acknowledgments
Time flies. When we wrote the first edition of this book Robert and Alexandra were toddlers who
would do anything to get our attention and wanted to sit on our laps while we worked. Now they
are teenagers who just want our laptops. Like Mathematica our kids have grown up. They have
become our best friends and terrific travel buddies. This project has again disrupted their lives and
we thank them for their attempts at patience. To quote Robert, “You guys aren’t going to write any
more books, are you?” Don’t worry kids, at this rate you’ll both be in college.
Special thanks go out to Paul Wellin at Wolfram Research, who handled the page design and who
dealt tirelessly with countless other issues, both editorial and technical. We would like to thank
Randolph-Macon College and the Walter Williams Craigie Endowment for the support we received
throughout this project. And we thank Peter Thompson, our editor at Cambridge, for his profes-
sional acumen and ongoing encouragement and support.
1
Getting Started
When you have located the icon, double click it with your mouse. In a moment an empty window
will appear. This is your Mathematica notebook; it is the environment where you will carry out your
work.
The remainder of this chapter is a quick tutorial that will enable you to get accustomed to the
syntax and conventions of Mathematica, and demonstrate some of its many features.
Entering Input
After typing a command, you enter it as follows:
Ê On a machine running Windows: Hit the combination Ú¹Ö, or hit the Ö key on
the numeric keypad if you have one (usually in the lower right portion of the keyboard).
Ê On a Mac: Hit the Ö key (usually in the lower right portion of the keyboard), or hit
the combination Ú¹Ê.
2 Getting Started
22
then hit the Ú¹Ö combination (Windows) or the Ö key (Mac OS). There may be a brief pause
while your first entry is processed. During this pause the notebook’s title bar will contain the text
“Running...”
In[1]:= 22
Out[1]= 4
The reason that this simple task takes a moment is that Mathematica doesn’t start its engine, so to
speak, until the first computation is entered. In fact, entering the first computation causes your
computer to launch a second program called the MathKernel (or kernel for short). Mathematica really
consists of these two programs, the Front End, where you type your commands and where output,
graphics, and text are displayed, and the MathKernel, where calculations are executed. Every subse-
quent computation will be faster, for the kernel is now already up and running.
In[1]:= 17 1
Out[1]= 18
In[2]:= 17 1
Out[2]= 16
Out[6]= 0.0355292
In[7]:= 34 s 4
17
Out[7]=
2
This last line may seem strange at first. What you are witnessing is Mathematica’s propensity for
providing exact answers. Mathematica treats decimal numbers as approximations, and will generally
avoid them in the output if they are not present in the input. When Mathematica returns an expres-
sion with no decimals, you are assured that the answer is exact. Fractions are displayed in lowest
terms.
6
1
In[1]:=
2
1
Out[1]=
64
You’ve surely noticed something else too (you’ll need to be running a live session for this), those
brackets along the right margin of your notebook window. Each input and output is written into a
cell, whose scope is shown by the nearest bracket directly across from the respective input or output
text. Cells containing input are called input cells. Cells containing output are called output cells. The
brackets delimiting cells are called cell brackets. Each input–output pair is in turn grouped with a
larger bracket immediately to the right of the cell brackets. These brackets may in turn be grouped
together by a larger bracket, and so on. These extra brackets are called grouping brackets.
At this point, it’s really enough just to know these brackets are there and to make the distinction
between the innermost (or smallest, or leftmost) brackets which delimit individual cells and the
others which are used for grouping. If you are curious about what good can possibly come of them,
try positioning the tip of your cursor arrow anywhere on a grouping bracket and double click. You
will close the group determined by that bracket. In the case of the bracket delimiting an input–output
pair, this will have the effect of hiding the output completely (handy if the output runs over several
pages). Double click again to open the group. This feature is useful when you have created a long,
complex document and need a means of managing it. Alternately, you can double click on any
4 Getting Started
output cell bracket to reverse-close the group. This has the effect of hiding the input code and display-
ing only the output.
Since brackets are really only useful in a live Mathematica session, they will not, by default, show
when you print a notebook. Further details about brackets and cells will be provided in Section 2.2
on page 27.
One last bit of terminology is in order. When you hit the Ú¹Ö combination (Windows), or the
Ö key (Mac OS) after typing an input cell, you are entering the cell. You’ll be seeing this phrase
quite a bit in the future.
If you see no such window, go to the Palettes menu and select BasicMathInput to open it.
1.6 The BasicMathInput Palette 5
The BasicMathInput palette is indispensable. You will use it to help typeset your Mathematica input,
creating expressions that cannot be produced in an ordinary one-dimensional typing environment.
Palettes such as this provide you with a means of producing what the designers of Mathematica call
two-dimensional input, which often matches traditional mathematical notation. For instance, use the
19
ef button in the upper left corner of the palette to type an exponential expression such as 17 . To
do this, first type 17 into your Mathematica notebook, then highlight it with your mouse. Next,
push the ef palette button with your mouse. The exponent structure shown on that button will be
pasted into your notebook, with the 17 in the position of the black square on the palette button
(the black square is called the selection placeholder). The text insertion point will move to the place-
holder in the exponent position. Your input cell will look like this:
17e
You can now type the value of the exponent, in this case 19, into the placeholder, then enter the
cell:
In[1]:= 1719
Out[1]= 239 072 435 685 151 324 847 153
¿ Another way to accomplish the same thing is this: First hit the palette button, then type 17
into the first placeholder. Next hit the Í key to move to the second placeholder (in the
exponent position). Now type 19 and enter the cell. This procedure is perhaps a bit more
intuitive, but it can occasionally get you into trouble if you are not careful with grouping. For
instance, if you want to enter +1 x/8 , and the first thing you do is push the ef button on the
palette, then you must type (1 + x) with parentheses, then Í, then 8. By contrast, you could
type 1 + x with or without parentheses and highlight the expression with your mouse, then hit
the ef palette button, and then type 8. The parentheses are added automatically, if needed,
when this procedure is followed.
If you don’t understand what some of the palette buttons do, don’t fret. Just stick with the ones that
you know for now. For instance, you can take a cube root like this: type a number and highlight it
f
with the mouse, then push the e button on the BasicMathInput palette, then hit the Í key, and
finally type 3. Now enter the cell:
3
In[2]:= 50 653
Out[2]= 37
In[4]:= 373
Out[4]= 50 653
Entering Input
Speaking in general terms, the buttons on the top portion of the BasicMathInput
palette (in fact all buttons containing a solid black placeholder e on this and any other
palette) are used this way:
Ê Type an expression into a Mathematica notebook.
Ê Highlight all or part of the expression with your mouse (by dragging across the
expression).
Ê Push a palette button. The structure on the face of the button is pasted into your
notebook, with the highlighted text appearing in the position of the solid black square.
Ê If there are more placeholders in the structure, use the Í key or forward arrow (or
move the cursor with your mouse) to move from one to the next.
The buttons on the middle portion of the BasicMathInput palette have no placeholders. They are
used simply to paste into your notebook characters that are not usually found on keyboards. To use
them, simply position the cursor at the point in the notebook where you want the character to
appear, then push a palette button.
For instance, the symbol can be used to test if one number is less than or equal to another:
Out[5]= False
Out[6]= True
The special symbol m is used to test if one quantity is equal to another. It has the same meaning as
the equal sign in standard mathematical notation:
Out[7]= True
1719
In[1]:=
1917
239 072 435 685 151 324 847 153
Out[1]=
5 480 386 857 784 802 185 939
3
In[2]:= 59 875
Out[2]= 5 4791s3
multiplication, and raising a number to the power 1 s 3 is the same as taking its cube root). The
Mathematica tells us that the answer is 5 times the cube root of 479 (remember that a space indicates
output is exact, but again it is difficult to grasp the magnitude of this number. How can we get a
nice decimal approximation, like a calculator would produce?
If any one of the numbers you input is in decimal form, Mathematica regards it as approximate. It
responds by providing an approximate answer, that is, a decimal answer. It is handy to remember
this:
17.019
In[3]:=
1917
Out[3]= 43.6233
3
In[4]:= 59 875.0
Out[4]= 39.1215
A quicker way to accomplish this is to type a decimal point after a number with nothing after it.
That is, Mathematica regards “17.0” and “17.” as the same quantity. This is important for understand-
ing Mathematica’s output:
3
In[5]:= 59 875.
Out[5]= 39.1215
30.
In[6]:=
2
Out[6]= 15.
Note the decimal point in the output. Since the input was only “approximate,” so too is the output.
Get in the habit of using exact or decimal numbers in your input according to the type of answer,
exact or approximate, that you wish to obtain. Adding a decimal point to any single number in your
8 Getting Started
input will cause Mathematica to provide an approximate (i.e., decimal) output. A detailed discussion
on approximate numbers can be found in Section 8.3 on page 392.
In[1]:= 3 +4 1/
Out[1]= 15
In[2]:= 3 4 1
Out[2]= 13
In[3]:= +3/2
Out[3]= 9
In[4]:= 32
Out[4]= 9
In[5]:= +3 1/ s 2
Out[5]= 2
In[6]:= 3 1s2
7
Out[6]=
2
The last pair of examples above shows one benefit of using the BasicMathInput palette instead of
typing from the keyboard. With the two-dimensional typesetting capability afforded by the palette
there is no need for grouping parentheses, and no chance for ambiguity:
31
In[7]:=
2
Out[7]= 2
1.9 Three Well-Known Constants 9
1
In[8]:= 3
2
7
Out[8]=
2
The lesson here is that the order in which Mathematica performs operations in the absence of
parentheses may not be what you intend. When in doubt, add parentheses. Also note: you do not
need to leave a space to multiply by an expression enclosed in parentheses:
In[9]:= 25+2 2/
Out[9]= 100
Note also that only round brackets can be used for the purpose of grouping terms. Mathematica
reserves different meanings for square brackets and curly brackets, so never use them to group terms.
In[1]:= S
Out[1]= S
In[2]:= S 0.
Out[2]= 3.14159
Again, note Mathematica’s propensity for exact answers. You will often use S to indicate the radian
measure of an angle to be input into a trigonometric function. There are examples in the next
section.
It is possible to enter each of these three constants directly from the keyboard, as well. You can type
ÈpÈ for S, ÈeeÈ for Æ, and ÈiiÈ for Ç.
¿ You can also type Pi for S, E for Æ, and I for Ç. The capitalizations are important. These do not
look as nice, but it illustrates an important point: it is possible to type any Mathematica input
using only the characters from an ordinary keyboard. That is, every formatted mathematical
expression that can be input into Mathematica has an equivalent expression constructed using
only characters from the keyboard. Indeed, versions 1 and 2 of Mathematica used only such
expressions. These days, the keyboard, or InputForm, of an expression is used when you
include a Mathematica input or output in an email message (say, to a friend or to your profes-
sor). If you copy a formatted expression such as S1s3 from Mathematica and paste it into an
10 Getting Started
email or text editor, you’ll find that it becomes Pi^(1/3) (or just S^(1/3) if the editor has the S
symbol available). The point is that it is exceedingly simple to include formatted Mathematica
expressions in plain text environments. Note that you can display any input cell in Inputg
Form from within Mathematica by clicking on its cell bracket to select it, and going to the Cell
menu and choosing ConvertTo # InputForm.
In[3]:= Pi m S
Out[3]= True
Commands are typically typed from the keyboard, and certain rules of syntax must be strictly
obeyed. Commands take one or more arguments, and when entered transform their arguments into
output. The typical syntax for a command is:
When you begin typing a command, the individual characters will be blue. They will change to
black as soon as they match the name of a built–in command. This syntax coloring mechanism is
designed to help you spot typing errors. If you were to type Arcsin instead of ArcSin, for example, it
would remain blue, indicating that it’s not right.
In[1]:= N#S'
Out[1]= 3.14159
In[2]:= 1730
Out[2]= 8 193 465 725 814 765 556 554 001 028 792 218 849
In[3]:= N$1730(
)
1
In[4]:= N%
250
If you were wondering, yes, typing 17.30 has the same effect as typing N[1730 ]. But the command N
is more flexible. You can add an optional second argument that specifies the number of significant
digits displayed in the output. Type N[x, m] to get a numerical approximation to x with m signifi-
cant digits:
Out[6]= 3.14159265358979323846264338327950288419716939937510582097494459230781640g
62862089986280348253421170679821480865132823066470938446095505822317253g
59408128481117450284102701938521105559644622948954930381964428810975665g
93344612847564823378678316527120190914564856692346034861045432664821339g
36072602491412737245870066063155881748815209209628292540917153643678925g
90360011330530548820466521384146951941511609433057270365759591953092186g
11738193261179310511854807446237996274956735188575272489122793818301194g
91
12 Getting Started
Trigonometric Functions
All trigonometric functions require that their argument be given in radian measure. The command
names themselves and the square brackets are most easily typed directly from the keyboard, while
S
many arguments (such as ) are best typeset with the BasicMathInput palette. Note carefully the
4
placement of capital letters in these commands. You can choose from Cos, Sin, Tan, Sec, Csc, Cot,
ArcCos, ArcSin, ArcTan, ArcSec, ArcCsc, and ArcCot:
Cos% )
S
In[7]:=
4
1
Out[7]=
2
)
S
In[8]:= Sin%
12
1 3
Out[8]=
2 2
)
1 3
In[9]:= ArcSin%
2 2
S
Out[9]=
12
)
S
In[10]:= Tan%
12
Out[10]= 2 3
)
S
In[11]:= Sec%
12
Out[11]= 2 .1 32
)
S
In[12]:= Csc%
12
Out[12]= 2 .1 32
1.10 Typing Commands in Mathematica 13
If you wish to use degrees, enter the degree measure multiplied by the degrees-to-radians conver-
S
sion factor of . This will simply convert your degree measure to radian measure. For instance, the
180
sine of 45 degrees is found as follows:
)
S
In[13]:= Sin%45
180
1
Out[13]=
2
S
Alternatively, you can use the built-in constant Degree, which is equal to . Either type Degree or
180
push the button on the BasicMathInput palette. Both of these have the effect of reading nicely,
S
although in reality you are simply multiplying the argument by :
180
In[14]:= Sin#45'
1
Out[14]=
2
1
Out[15]=
2
)
S
In[16]:= N%
180
Out[16]= 0.0174533
In[17]:= N#'
Out[17]= 0.0174533
Logarithms
Type Log[x] to find the natural logarithm of x:
In[18]:= Log#Æ'
Out[18]= 1
In[19]:= Log$Æ45(
Out[19]= 45
14 Getting Started
Note that it is possible to build up input by nesting one command inside another. Before long you’ll
be doing this sort of thing without giving it a second thought:
Out[20]= 1.14472988584940017414342735135
To find the base b logarithm of x, type Log[b, x]. Here is a base 10 logarithm:
Out[21]= 3
Out[22]= 9
In[23]:= 29
Out[23]= 512
Factoring Integers
You can factor any integer as a product of prime numbers using the command FactorInteger. Type
FactorInteger[n] to obtain the prime factorization of n:
The output here needs interpretation. It means that 4,832,875 can be factored as 53 23 412 . Note
the form of the output: a list whose members are each lists of length two. Each list of length two
encloses a prime number followed by its exponent value. Again, it is easy to check the answer:
In[25]:= 53
23
412
Out[25]= 4 832 875
¿ You may wonder why the output to FactorInteger appears in a form that at first glance is
somewhat cryptic. Why isn’t the output just 53
23
412 ? The rationale is subtle, but impor-
tant. The designers of Mathematica put the output in the form they did to make it easier for the
user to work programmatically with the output. That is, it is easy to extract just the primes 5,
23, and 41, or just the exponents 3, 1, and 2, from this output, and to input those values into
another command for further analysis. Remember that Mathematica is a sophisticated program-
ming language that is used by experts in many disciplines. In this and in many other cases,
commands are designed to allow their output to be easily operated on by other commands. It
1.10 Typing Commands in Mathematica 15
makes the task of assembling many commands into a single program much simpler for the
user. For the beginner, however, these advantages may not be immediately obvious.
In[26]:= Factor$t2 9(
Out[26]= +3 t/ +3 t/
In[27]:= Factor$64 128x 48x2 144x3 292x4 288x5 171x6 61x7 12x8 x9 (
Note that you do not need to type a space between a number and a variable to indicate multiplica-
tion as long as the number is written first; Mathematica will insert the space automatically in this
case.
You can also have Mathematica expand a factored polynomial by typing Expand[polynomial]. Below
we confirm the output above:
The commands Factor, Expand, and a host of others that perform various algebraic feats are
explored in Chapter 4, “Algebra.”
Plotting Functions
Mathematica has a variety of commands that generate graphics. One of the most common is the Plot
command, which is used for plotting functions. Plot takes two arguments. The first is the function
to be plotted, the second is something called an iterator, which specifies the span of values that the
independent variable is to assume. It is of the form
Here’s an example. Note that we view the function on the domain where the variable x ranges from
3 to 3. Mathematica determines appropriate values for the y axis automatically:
16 Getting Started
4
Out[29]=
3 2 1 1 2 3
1.0
0.5
Out[30]=
2 1 1 2
0.5
1.0
1.5
The Plot command is explored in greater depth in Section 3.2 on page 53.
Manipulate
Version 6 of Mathematica introduces the Manipulate command, which allows the user to create a
dynamic interface (with sliders or buttons that can be manipulated in real time). Like Plot, Manipug
late takes two arguments. The first is the expression to be manipulated, the second is an iterator
which specifies the span of values that the controller variable is to assume. Here’s an example:
x
Out[31]=
8
You can now move the slider with your mouse to control the value assumed by x, and watch as the
value of x2 1 is displayed in real time. This is far more interesting to play with than it is to read
about, so be sure to try it! Click on the button to the right of the slider to reveal a more sophisti-
cated user control panel:
1.10 Typing Commands in Mathematica 17
x
3
As you mouseover each button on the panel, a tooltip message will display on screen with a brief
explanation of that button’s function. Go ahead and try each button in turn to get a feel for what
you can do. You can even type a value for the variable x into the input field and hit Return (Mac) or
Enter (Windows PC) to see the value of x2 1 in the display area.
Out[32]=
2 1 1 2
1
2
As you type this input, be sure to leave a space between the a, the x, and Cos. The setting Plotg
Range 2 has been added after the second argument in the Plot command to fix the viewing
rectangle between 2 and 2 in both the x and y directions. This is needed so that the scaling on the
y axis does not change as the slider moves. You can find the symbol on the BasicMathInput
palette. Manipulate is explored in greater depth in Section 3.4 on page 76.
In[33]:= 144
Out[33]= 12
Or you can forgo the palette approach and type Sqrt[x] to get the square root of x:
In[34]:= Sqrt#144'
Out[34]= 12
¿ It is a fact that every palette button with a placeholder (such as the square root button) has an
equivalent syntax that may be typed entirely from the keyboard. In most cases you will find
the palette version of the command easier to use. However, if you are a good typist and use
Mathematica frequently you may find it easier to work from the keyboard more rather than
less. If you ever want to know the name of the InputForm of a palette command, follow this
procedure: First use the palette version of the command to create an input cell. Then use a
single click of your mouse to highlight the cell bracket for the cell. Go to the Cell menu and
select Convert to # InputForm from the pop-up menu. You will see the two-dimensional
formatted command replaced by its InputForm alternative. In the future, you can just type
the InputForm of the command directly instead of using the palette.
Out[35]= 2
Out[36]= 3
Out[37]= 2035
In[38]:= IntegerDigits#2010'
Out[38]= 2, 0, 1, 0
1.10 Typing Commands in Mathematica 19
The output is a list; it is comprised of items (digits in this case) enclosed in curly brackets and
separated by commas. Lists such as this are a fundamental data structure in Mathematica. Many
commands will produce lists as output and accept lists as input. Lists are so ubiquitous that many
operations that work on numbers will automatically be distributed over lists. For instance, we can
add 1 to every member of a list like this:
1 2, 0, 1, 0
3, 1, 2, 1
In[39]:=
Out[39]=
FromDigits will take a list of digits and assemble them back into a number.
Out[40]= 2010
Programming
The real utility of commands such as these lies in the ability to take the output of one and use it as
the input to another. Putting commands together in a way that does something useful is known as
programming. Mathematica is, among other things, a rich programming environment. Here we take a
number and form a new number by adding 1 to each of the original number’s digits:
Out[41]= 3121
Think about how little code is required to do that, and then think how you might accomplish the
same task in some other programming language, or in Excel.
The following input illustrates this embedding of commands, one within another, but taken to
another level:
3
In[42]:= ArrayPlot%NestList%Function%x, IntegerDigits%Floor% FromDigits#x, 2'), 2)),
2
1, 0, 200), Background Gray)
20 Getting Started
Out[42]=
While what’s happening here is far beyond what one needs to know at this early stage, it is possible,
with a bit of perseverance, to see what is going on. We read from the inside out: starting with x,
which represents the base–2 digit sequence of a number, it multiplies the number (FromDigits[x, 2])
3
by , rounds down if the result is not a whole number, then displays its IntegerDigits base–2. This
is invoked successively, starting on the number 2 (i.e., the number whose IntegerDigits are 1, 0),
2
and then on the result, and then on the result of that, a total of 200 times. So beginning with 2, one
3 3 3
next gets of 2, i.e., 3, then of 3 (rounded down), or 4, then of 4, i.e., 6, and so on. The num-
2 2 2
bers are displayed in base–2, one above the other as successive rows in an array, with zeros repre-
sented by white squares and ones represented by black squares. Chapter 8 presents the basic com-
mands used here in more detail.
Naming Things
It is easy to assign names to quantities in Mathematica, and then use those names to refer to the
quantities later. This is useful in many situations. For instance, you may want to assign a name to a
complicated expression to avoid having to type it again and again. To make an assignment, type the
name (perhaps a lowercase letter, or a Greek character, or even an entire word), followed by =,
followed by the quantity to which the name should be attached. For example (look for T in the
BasicMathInput palette):
1.10 Typing Commands in Mathematica 21
S
In[43]:= T
6
S
Out[43]=
6
S
Now whenever you place T in an input cell, Mathematica will replace it with :
6
In[44]:= T
S
Out[44]=
6
In[45]:= Sin#T'
1
Out[45]=
2
In[46]:= Sin#2T'
3
Out[46]=
2
In[47]:= Tan#4T'
Out[47]= 3
You can (and should) clear an assignment when you are done. This is accomplished with the Clear
command:
In[48]:= Clear#T'
No output will be produced when you enter the Clear command. You can check that no value is
attached to the symbol T by typing it into an input cell:
In[49]:= T
Out[49]= T
For a second example, we can assign to p the value of S rounded to 39 decimal places (the 3 fol-
lowed by 39 decimal places makes a total of 40 significant digits):
Out[50]= 3.141592653589793238462643383279502884197
In[51]:= p 22
Out[51]= 12.56637061435917295385057353311801153679
Note how Mathematica, in performing a calculation involving an approximate number p and an exact
number 22 , returns an approximate number with the same number of significant digits as p.
In[52]:= Clear$p(
For a final example, we’ll assign values to words. Each word is treated as a separate entity. The terms
miles and hour are not given values, but distance is assigned the value 540 miles, and time is
assigned the value 6 hour:
Out[54]= 6 hour
distance
In[55]:= rate
time
90 miles
Out[55]=
hour
We can clear all of these assignments in one shot with the Clear command—just put a comma
between each successive pair of names:
Since all built-in Mathematica objects begin with capital letters, it’s a good practice to make all your
names lowercase letters, or words that begin with lowercase letters. This practice assures that you
will never accidentally assign a name that Mathematica has reserved for something else. The only
Greek character that has a built-in value is S. All others make perfectly good names. You’ll find
these characters in the Special Characters palette.
It is also permissible to use numbers in your names, provided that a number is not the first charac-
ter. For instance, you might use the names x1 and x2. It is not alright to use the name 2x, for that
means 2 x.
1.11 Saving Your Work and Quitting Mathematica 23
¿ The file size of a Mathematica notebook tends to be quite small unless the notebook contains
lots of graphics. Notebook files are also portable across computer platforms, as the files
themselves are plain text (ascii) files. The Mathematica front end interprets and displays
notebook files in much the same way that a Web browser interprets and displays HTML files.
For information on the structure of the underlying notebook file, select Documentation
Center from the Help menu, type “notebooks as Mathematica expressions” in the text field,
then read the tutorial Notebooks as Mathematica Expressions.
If you have created a large notebook file, and want to shrink its file size (for instance to make
it small enough to attach to an email) do this: Open the notebook and delete the graphics
cells. To do this, click once on a graphic’s cell bracket to select it, then choose Cut in the Edit
menu. Do not cut out the input cells that generated the graphics. Now save the notebook.
When you open the notebook next time, you can regenerate any graphic by entering the
input cell that created it. An even simpler approach is to select Cell # Delete all Output , and
then save your notebook. When you open the file later, select Evaluation # Evaluate Note–
book to re-evaluate every input cell in the notebook.
After a notebook has been saved once, the title bar will bear the name you have assigned. As you
continue to work and modify the notebook, you can and should save it often. This is easy to do:
choose Save from the File menu. This will write the latest version of the notebook to the location
where the file was last saved. Should the power fail during a session, or should your computer crash
for some reason, it is the last saved version of your notebook that will survive. Many hardened souls
will save every few minutes.
To end a Mathematica session, select Quit from the application’s main menu. If you have modified
your notebook since it was last saved, you will be prompted and asked if you care to save the
changes you have made since it was last saved. Answer Save or Don't Save as appropriate.
24 Getting Started
Out[1]= 3043
Why capital letters? The main reason is that you will find yourself assigning names to quantities,
such as x 3 or pi 3.14. Since you don’t know the name of every built-in object, there is a danger
that you may choose a name that coincides with the name of a built-in command or constant.
Without getting into the technicalities, that would be bad. But it can be avoided if you simply stick
to the convention of beginning all your assignment names with lowercase letters. By doing this you
guarantee that you will never choose a name that conflicts with any existing Mathematica symbol.
Out[2]= Plot#x'
The caret indicates that you forgot something; Plot needs two arguments (a function and iterator),
and here we did not add the iterator. The caret points to where you need to type something.
1.12 Frequently Asked Questions About Mathematica’s Syntax 25
You may recall that in our usual mathematical notation, we often write f +x/ to denote the value of
grouping terms. When you write f +12/, for instance, it is not clear whether you intend for a function
the function f with argument x. This won’t do in Mathematica, for parentheses ( ) are reserved for
named f to be evaluated at 12, or whether you want the product of a variable named f with 12. Since
ambiguous. You and I can usually flesh out the meaning of the notation f +12/ from its context, but
parentheses are routinely used for these two very different purposes, the traditional notation is
a computer needs unambiguous instructions. Hence in Mathematica, square brackets are used to
enclose function arguments, while parentheses are used to group terms.
When working with Mathematica, never use round parentheses for anything other than grouping
terms, and never use square brackets for anything other than enclosing the arguments to functions.
What happens if you slip and muff the syntax? First of all, don’t worry. This will happen to you. The
computer won’t explode. For example, behold:
In[3]:= Log#7.3
Here our input is close enough to the correct syntax that Mathematica suspects that we goofed, and
tells us so. Upon entering an incomplete or erroneous input, version 6 and higher will show a
warning flag in the expression’s cell bracket, and will often highlight the offending part of the
input. Click once on the warning flag and any relevant warning messages will be displayed.
In[3]:= Log#7.3
Syntax::bktmcp : Expression "Log#7.3" has no closing "'".
You will certainly generate messages like this at some point, so its good to acquaint yourself with
some. Error messages are somewhat cryptic to the new user, and are rarely a welcome sight. But do
read the text of these messages, for you will often be able to make enough sense out of them to find
the source of the problem. In this case we left off the closing square bracket. Note that as you type
your input, each opening bracket will appear brightly colored until the corresponding closing
bracket is added, at which time both brackets will turn black. This makes mistakes of this type easy
to spot. If an expression has one or more brightly colored brackets, it is incomplete and should not
be entered.
26 Getting Started
But worse than getting an error message or input flag is getting neither. It is not difficult to enter
syntactically correct, but meaningless input. For example, consider this:
In[4]:= ln +7.3/
Out[4]= 7.3 ln
No warning is given (other than the command name ln appearing in blue before the cell is entered),
but the output is not the natural logarithm of 7.3. Mathematica has instead multiplied the
meaningless symbol ln by the number 7.3 (remember round brackets are for grouping only). Always
look carefully and critically at your output. There will certainly be times when you need to go back
and edit and re-enter your input before you get the answer you desire.
2
Working with Mathematica
Mathematica’s text environment is a joy to use. It wraps lines for you within each text cell. You can
use any palette to paste a mathematical symbol or expression into your text, just as you paste into
an input cell. There is a full-featured spell checker—just place the cursor where you want to start
spell checking and choose Check Spelling… in the Edit menu. We’ll soon see that it is highly adept at
formatting complex mathematical expressions. For these reasons, you may find yourself using
Mathematica as your word processor of choice for technical papers. You can also highlight portions
of text with your mouse and cut, copy, or paste (look in the Edit menu for these and other features).
You can change the size, face, font, and color of highlighted text by choosing the appropriate item
28 Working with Mathematica
in the Format menu. There are buttons on the toolbar to control the centering and justification of
your text. Use these features to make your notebook a masterpiece.
You can cut, copy, paste, and format entire cells or groups of cells. You select a cell or group of cells
by positioning the tip of the cursor arrow on a cell bracket or grouping bracket along the right side
of the notebook window. The bracket becomes highlighted. Now choose Cut or Copy from the Edit
menu, position the mouse where you wish to paste the selection (in the current notebook or in any
other notebook that is open), click once, and select Paste from the Edit menu. Similarly, the com-
mands in the Format menu will be applied to the text in any cell or group of cells whose bracket is
selected.
Mathematica’s cell structure makes it easy to organize your notebook into collapsible sections. You
simply add preformatted title or section headings. To do this, click between existing cells (or below
the last cell in the notebook, or above the first cell), and then go to the pull-down menu in the
toolbar and select Title, or Section, or Subsection, or the like, and start typing. Upon adding a title to
a notebook, you will notice a gigantic grouping bracket on the far right of your notebook window
that spans the entire notebook. Place the cursor anywhere along this bracket and double-click to
close the group. You will see the title, but the rest of the notebook will disappear. Don’t worry, it’s
still there; double-click again on the bracket to open the group. When you create sections or subsec-
tions, grouping brackets will appear to show their respective domains, and these too can be toggled
open or closed with a double-click on the appropriate grouping bracket. These features allow you to
keep your work organized, and minimize the amount of scrolling needed to navigate a large
document.
If you click between cells in a notebook and then start typing, you will by default create a new input
cell. This makes it easy to enter input during a Mathematica session; as soon as you get output from
one computation, you can just start typing to generate a new input cell. You only have to specify
cell type (Text, Title, Section, etc.) when you want to create some type of cell other than input. By
the way, you can forgo the toolbar if you want, and select your cell types from the pop-up menu
that appears when you select Style in the Format menu.
2.2 Adding Text to Notebooks 29
If you accidently start typing text in an input cell, don’t despair. The fix is simple: click once on the
cell’s bracket to select it, then use the toolbar (or go to the Format menu) to change the cell to a text
cell.
Suppose that you wish to place a mathematical expression in the middle of a sentence of text. Begin
by creating a text cell (as outlined in the previous section) and typing the text that will precede the
mathematical expression. When you are ready to insert the math, from the menus select Insert #
Typesetting # Start Inline Cell. Or from the keyboard hit ŹÎ9Ï (for both Mac OS and Windows). The
state of the cursor will change to a placeholder within a lightly colored box. This colored box
delimits what is called an inline cell. Now type your mathematics, using palettes if you like, being
careful not to exit the inline cell as you add the mathematics (if you exit the colored box, use the
backarrow to get back into it). When you are finished typing the mathematical expression, hit the
forward arrow to exit the inline cell. You can also exit the inline cell by hitting ŹÎ0Ï. It’s easy to
remember these keyboard shortcuts as ŹÎ(Ï to start a mathematical formula and ŹÎ)Ï to end it.
When you are typing your mathematics within an inline cell, you’ll notice that what you type is
displayed differently than ordinary text. For instance: any single letter will be italicized, a hyphen
will change subtly to a minus sign, and spacing will be different. For example, here is an equation
when typeset within an inline cell: f +x/ x 0. The difference is striking, and clearly illustrates the
typeset without using an inline cell: f(x)-x=0. And here is what is produced by the same keystrokes
Note that whenever you switch stylesheets, the items in the Format # Style menu will change to
reflect the cell styles available in that stylesheet. Note also that stylesheets can be used to control
both the on-screen and print versions of notebooks, and even to make each look different from the
other if you wish. Stylesheets may also be used to change what the default cell style is in a particular
notebook (the type of cell that will be created if you just start typing). In order to see just how
powerful these concepts are, try this: Open a new blank notebook, and switch to the Format #
Stylesheet # Utility # Correspondence stylesheet. Now pay attention: you are going to write a formal
letter. First type your name and address, using carriage returns to create new lines. It appears in a
special “Sender” cell, complete with a gray label to remind you of that fact. Now hit the down-arrow
on the keyboard to jump to the insertion point for the next cell. Immediately start typing. This time
a “Date” cell will be created, so type today’s date. Again, when you’re done hit the down-arrow.
Now type the recipient’s name and address. Down-arrow. Type a salutation, such as “Dear Stephen.”
Down-arrow. Now type the body of your letter, using the carriage return to create new paragraphs.
Down-arrow. Type your closing, such as “Sincerely” or “Cheers.” Down-arrow. And finally, add your
signature. Now print it. All the gray cell labels do not appear in the printed version; the formatting
is just right. If you have to write a lot of letters, this stylesheet streamlines your workflow. That’s
exactly what a stylesheet should do. Creating a sophisticated stylesheet like this one takes a bit of
work, but using it takes almost none. And if you just wish to modify an existing stylesheet to better
suit your purposes, well that’s a breeze. Read on…
Suppose you wish to modify an existing cell style, for instance, to change the look of the “Section”
headings. Or suppose you wish to create an entirely new cell style, say a custom text cell that puts a
light-gray background color behind your text. To do this you add a local modification to an existing
stylesheet (the principle is that of a cascading stylesheet, which is common in web design). This is the
best way to ensure that you produce a document with consistent style parameters; it prevents you
from having to apply the desired style features one by one onto each relevant cell in your notebook.
Here’s how to set it up: create a notebook using one of the included stylesheets. Choose Format # Edit
Stylesheet…, and a stylesheet notebook will appear. At the top of this notebook you may either
choose an existing style to modify (if, for instance, you just want to change the default text font), or
type the name of a new style you would like to create (if, for instance, you want to keep the default
text style, and add a second text style for some other purpose). In either case, a cell will appear in
this new notebook, and its cell bracket will be selected. Go directly to the Format menu and apply
the formatting changes you desire to this selected cell. You can change the font, the font size, the
font slant, the font color, the background color, the alignment, etc. When you are finished, close
the stylesheet window, and return to your notebook. If you created a new style, its name will appear
at the bottom of the Format # Style menu, so you may easily apply it to any cell in your notebook at
any time. If you modified an existing style, all cells of that style in your notebook will now reflect
that change.
2.3 Printing 31
2.3 Printing
As long as your computer is properly hooked up to a printer, and the printer is turned on, you can
print your current notebook by going to the File menu and choosing Print…. If your notebook
contains graphics or two-dimensional input using special math fonts, it may take a moment to start
printing, so be patient.
You can also select one or more cells to print, rather than printing an entire notebook. This can save
vast quantities of paper, so we repeat: You don’t have to print the entire notebook. To print a single
cell or any group of cells delimited by a grouping bracket, position the tip of the cursor arrow on the
cell or grouping bracket and click once. This selects the cell or group. Now go to the File menu and
choose Print Selection….
To select several adjacent cells when there is no grouping bracket, hold down the Ú key and click
on their cell brackets one by one. They will all become selected. To select several nonadjacent cells,
hold down the Ð key (Mac OS), or the Å key (Windows), while clicking on cell brackets. You can
then print your selection as above: Go to the File menu and choose Print Selection….
Printing a notebook that has graphics can sometimes lead to less-than-optimal page breaks. It is easy
to add more page breaks, but it can be tricky to force pages not to break. To add a page break, simply
click between the cells where the break should occur and select Insert # PageBreak. To remove a break
above or below a graphic, try resizing the graphic. It is often the case that when printed, smaller
graphics look better, so this may be a good idea in any event. If this does not help, or if the
unwanted page break is not adjacent to a graphic, select the bracket of the first cell to appear after
the unwanted break, then summon the Option Inspector by visiting Format # Option Inspector....
Make sure that the first pull-down menu reads Selection (which it should by default). Now type
“PageBreak” into the text field. Change the PageBreakAbove setting for the selected cell to False.
Repeat as necessary for nearby cells. If you are working on a Mac, be sure to make use of the Preview
button in the Print dialog before committing your notebook to paper.
It is possible to control what is printed in the header and footer areas on each page of a printed
notebook. By default, the header is comprised of the filename of the notebook and the page num-
ber. To change this, go to the File menu and select Printing Settings # Headers and Footers…. The
resulting dialog box gives you the option of not displaying any header on the first page, which is
handy if you have a nicely typeset title page. There are also text fields for the content of the left,
right, and center portion of each header and footer. What appears by default in some of these fields
will look complex, but don’t worry. You may replace the content of any of these fields with any text
you like enclosed in double quotation marks, and you’ll be good to go. The complex structures
appearing by default are needed only if you wish to place page numbers or other such non-constant
values into your headers. Look up any of the command names appearing in these fields in the
Documentation Center for further information.
32 Working with Mathematica
¿ Each text field for the left, right, and center portions of a header or footer will accept a
complete Cell expression. This ensures total control over the style of the header, and allows
you to include CounterBoxes (for page numbers), and other such objects. To create a styled
text heading in any one of these locations, type Cell$"your header text ", "Header "( in the text
field. The first argument of the Cell command is your header text wrapped in double quotes.
The second argument is a cell style name, also in double quotes. Other common style names
for headers include "PageNumber", "Footer", or any other style name that appears in the
Format # Style menu. You can modify styles with a third argument, such as:
Cell$"your header text", "Text", FontSlant "Italic"(. If you don’t like the look of the page
numbers that appear in the default headers, the most simple means of manually putting a
page number into one of the text fields is to type
Cell$TextData$CounterBox$"Page"( (, "Header"(
where you may change the second argument from "Header" to "PageNumber" or to any other
style name that appears in the Format # Style menu.
To get started simply grab the SlideShow palette from the Palettes menu. The top button, New
Template, will open a new notebook with three pre-formatted generic slides. You are not bound to
this format, it is just to give you an idea of what a very simple Slide Show looks like. You can delete
everything in these slides and fill them with any Mathematica content, but before you do that let’s
take the generic one for a quick drive.
At the bottom of the palette there are two buttons labeled Normal and Slide Show. You can edit your
slides in either environment. The Normal environment shows all your slides at once with cell
brackets grouping the content of each slide. This is handy for cutting and pasting content into
several slides. The Slide Show environment shows one slide at a time. You can toggle back and forth
between the two environments as often as you wish.
In the Slide Show environment you can advance your slides by pushing the buttons in the toolbar at
the top of the window or the small gray arrows at the bottom right. If you don’t want the gray
arrows at the bottom of a slide you can delete that cell when you are in the Normal environment.
The leftmost arrow button among the four in the toolbar takes you to the beginning of the Slide
Show while the one on the right takes you to the end. Hit the button in the far top left corner of the
2.5 Creating Web Pages 33
toolbar in order to toggle into full-screen presentation mode. Push it again to toggle out of full-
screen mode.
Now you are ready to create your own Slide Show. You can use any format to add content to a slide.
You can even change the Stylesheet. If you want to add another slide go to the Normal environment,
position your cursor between the slides where you want to add a slide and click once to make a
horizontal bar appear, then click the New Slide button in the palette.
Push the Table of Contents button and you get a window listing all your slides. Finally, the Convert
Notebook button will convert any notebook into a Slide Show.
You can cut and paste content from other programs, such as photos from the web or sketches from
Geometer’s Sketchpad, into your Mathematica Slide Show. Once you get started you’ll find it is very
simple and intuitive to work in the Slide Show environment.
We know of a student who was getting nowhere trying to explain a mathematics problem over the
phone to a fellow student. He then typed the equations he was thinking of into Mathematica, saved
the notebook as HTML, posted it to his website, and had the fellow student go to the freshly minted
page. This seems a bit extreme, but if you maintain a web site and are handy with posting web
pages, it’s nice to know that it’s a simple matter to compose in Mathematica.
When you launch Mathematica by opening an existing notebook, the kernel is not needed. You can
scroll through the notebook and view and even edit the contents. It is only when you place the
cursor on an input cell and enter the cell, or type a new command line and enter it, that the kernel
will be launched.
Be mindful that the numbering is determined by the sequential order in which the commands are
received by the kernel, and not necessarily by the order in which commands appear in the note-
book. For instance, if you were to start a Mathematica session by opening an existing notebook, then
scroll through to some input cell in the middle of that notebook, click on that cell and enter it, it
would be labeled In[1]:=.
It is important to realize that when you start a new Mathematica session by opening an old note-
book, you should not enter any input cell that makes reference to another cell (or variable, or
anything you created) that has not been entered in this session. For instance, suppose you opened a
notebook that contained the following input and output cells (they are not numbered, since they
have not been entered in this session):
2.7 Mathematica’s Kernel 35
a 90
90
a2
8100
What would happen if you were to click on the second input cell (containing the text a2 ) and enter
it? Mathematica would be unaware of the cell containing the assignment a 90 since that cell has
not been entered in the current session. The resulting notebook would look like this:
a 90
90
In[1]:= a2
Out[1]= a2
In practice this means that when reopening an old notebook to continue work that you started in a
previous session, you should reenter, one by one, all the cells to which you will refer later in the
session.
You can automate this procedure if you like. After opening a previously saved notebook, go to the
Evaluation menu and select Evaluate Notebook. This will instruct the kernel to evaluate every cell in
the notebook, in order, from top to bottom. It’s a handy way to pick up your work where you left
off. Alternately, you may ؖclick on the cell bracket of any cell (Mac OS), or Öclick (Windows),
to select all cells of that type the notebook. Do this to an input cell, and all input cells will be
selected. Now go to the Evaluation menu and choose Evaluate Cells.
¿ Many notebooks contain certain input cells that will be evaluated each time the notebook is
used; this is often the case with notebooks created for students by teachers. Such notebooks
utilize special types of input cells called initialization cells. When a cell is an initialization cell,
it will be automatically evaluated before any other input cells in the notebook. Typical
initialization cells will define a special command to be used throughout the notebook, or load
a Mathematica package (more on packages later in this chapter). When you send your first
input to the kernel from a notebook containing one or more initialization cells, you will be
prompted and asked if you want to automatically evaluate all initialization cells in the
notebook. If you ever see such a prompt, answer “Yes.” Moreover, if you want to make an
input cell in one of your own notebooks an initialization cell, select the cell by clicking once
on its cell bracket (or Ú–click on several cell brackets), then go to the Cell menu and choose
Cell Properties # Initialization Cell.
36 Working with Mathematica
You will notice that the cell bracket gets a little vertical tick mark at the top. Now when you
reopen this notebook in the future, the cell (or cells) that are initialization cells can be
automatically processed by the kernel. When you first save a notebook containing one or
more initialization cells, you will be prompted, “Do you want to create an Auto Save Package?”
Answer “No.” This feature is for programmers who are creating Mathematica packages.
2120
In[1]:=
2021
278 218 429 446 951 548 637 196 401
Out[1]=
2 097 152 000 000 000 000 000 000 000
In[2]:= N#'
Out[2]= 0.132665
If you want to keep the old input and output cells, click below the old output cell and select Insert #
Output from Above from the menu. It will paste the contents of the output cell that resides directly
above the position of the cursor into a new input cell, regardless of when that cell was processed by
the kernel. You can then edit the new input cell and enter it.
If you want to keep (rather than overwrite) the old input and output cells, click below the old
output cell, go to the menu, and select Insert # Input from Above. The old input cell will be copied
into a new input cell, which you can then edit and enter.
Another option is to use your mouse to copy and paste text from one cell (input or output) to a new
input cell. You can highlight text with your mouse to select it, then choose Copy from the Edit
2.8 Tips for Working Effectively 37
menu, click to position the cursor where you want the text to appear, and finally choose Paste from
the Edit menu.
We’ve seen examples such as Sin$ ( and Log[10, 243]. When a command has only one argument,
S
4
another way to apply it is in postfix form. The postfix form for a command is:
argument //Command
This form is useful when the command is applied to an existing expression as an afterthought. For
instance, if you copy the contents of an earlier input or output cell into a new input cell, you can
easily apply a command in postfix form to the entire copied expression. Here are some examples:
) ss N
S
In[3]:= Sin%
12
Out[3]= 0.258819
((.
S
This is equivalent to entering N$Sin$
12
Out[4]= 12 4 x 19 x2 3 x3
Command@argument
Like the postfix form, this form can useful when the command is applied to an existing expression
as an afterthought. It allows you to apply the command without worrying about adding the closing
square bracket. Here are some examples:
Out[5]= 2
This is equivalent to entering First[{2, 4, 6, 8}].
38 Working with Mathematica
sin2 +x/
Out[6]//TraditionalForm=
Undoing Mistakes
If you make a bad mistake in typing or editing, the kind that makes you say, “I wish I could undo
that and return my notebook to its former state,” chances are you can. Look for Undo in the Edit
menu. It will reverse the previous action. The catch is that it will only undo the most recent action,
so use it immediately after making your mistake.
Another option is to close your notebook (choose Close in the File menu), and answer Don’t Save
when you are prompted. You can then reopen your notebook (choose Open Recent in the File
menu). You will find your notebook in the state that it was in when it was last saved. Of course you
should only do this if you have saved the notebook recently.
A more frightening scenario is entering an input cell and finding that Mathematica appears to be
stuck. For a long time you see the text “Running…” in the notebook’s title bar, but no output is
being generated. You may have inadvertently asked Mathematica to perform a very difficult calcula-
tion, and after a few minutes you may get tired of waiting. How can you make it stop? Go to the
Evaluation menu and select Abort Evaluation. Depending on the situation, it may halt immediately or
you may have to wait a minute or two before it stops. Be patient. If more than a few minutes pass
with no response, refer to Section 2.11, “Troubleshooting” on page 47.
Keyboard Shortcuts
If you have quick fingers you may find it easier to type characters than make repeated trips to the
menus with your mouse. Next to many menu items you will find keyboard shortcuts for
accomplishing the same task. We summarize some of the most common in Table 2.1.
Table 2.1 Keyboard Shortcuts. When reading this table, ĹÎqÏ means hitting the command key and the q key at the same
time. On a Mac, the command key is marked Ð.
Type to get
ÈpÈ the symbol S
È ee È the symbol Æ
È ii È the symbol Ç
È inf È the symbol
È deg È the symbol ,for entering angles in degrees0
È th È the symbol T ,no builUJO meaning, but often used0
È
È the symbol ,for multiplication0
ŹΠ^ Ï or ŹÎ6Ï to the exponent position
ŹΠt Ï into a fraction
ŹÎ2Ï into a square root
Å¹Û out of an exponent, denominator, or square root
Í from one placeholder to the next
Table 2.2 Keyboard Shortcuts for Typesetting. When reading this table, ŹÎ2Ï means hitting the control key and the 2 key
at the same time, while Å 2 means hitting the control key followed by the 2 key.
x
S2
y
by typing the following key sequence:
S2 x
y
by typing the following key sequence:
Of course if you consider yourself a poor typist, you may want to use palettes more rather than less.
Check out the BasicMathInput palette (in the Palettes menu). It contains buttons that will paste
templates of commonly used commands into your notebook. This keeps your typing to a minimum,
and helps you remember the correct syntax for commands. Whichever approach you take, you’ll
eventually find the way to typeset Mathematica input that works best for you.
S
In[7]:= x
12
S
Out[7]=
12
Here the output is not necessary. If you would like to suppress the output of any input cell, simply
type a semicolon ; after typing the contents of the cell:
S
In[8]:= x ;
12
You can enter a sequence of several commands in a single input cell by putting semicolons after all
but the final command. Only the output of the final command will be displayed. When you are
typing, you can use the character return key (Ê on a Mac or Ö on a PC) to move to a new line in
the same input cell, or you can keep it all on one line if it will fit:
In[9]:= x 3;
Expand%,x y0 )
8
Clear#x'; Expand%,x y0 )
8
In[11]:=
Out[11]= x8 8 x7 y 28 x6 y2 56 x5 y3 70 x4 y4 56 x3 y5 28 x2 y6 8 x y7 y8
2.9 Getting Help from Mathematica 41
A different means for suppressing output is the Short command. This command is useful if you
generate output that is just plain too long. If you enter a cell and produce screen upon screen of
output, append the text //Short to the input and reenter it. You will get the very beginning and end
of the total output, with a marker indicating how much was chopped out of the middle. Here’s an
example that makes use of factorials. The factorial of a positive integer n is the product of n with
every other positive integer less than n. So the factorial of 5 is equal to 5 4 3 2 1 120. The
common mathematical notation for the factorial of n and the Mathematica notation agree: type n :
Here Mathematica tells us that there are over 2500 digits missing from the output.
If you want to find out whose computer is faster, or if you want to know how long it takes Mathemat-
ica to arrive at an answer, use the Timing command. Wrap any input with this command, and the
output will be a list containing two items (they will be separated by a comma). The first item in the
list is the number of seconds that it took the kernel to process your answer (it doesn’t include the
time it takes to format and display the answer), and the second item is the answer itself. If the input
to the Timing command is followed by a semicolon, the second item in the list will be the word
Null rather than the answer. This is useful when the output is large:
In[13]:= 20 ss Timing
In[1]:= ?N
You can click on the j symbol at the end of this output to get more detailed information in the
Documentation Center.
Command Completion
Mathematica can finish typing a command for you if you provide the first few letters. This is useful if
the command has a long name; it saves time and guarantees that you won’t make a typing mistake.
Here’s how it works: After typing a few letters choose Complete Selection from the Edit menu. If more
than one completion is possible, you will be presented with a pop-up menu containing all of the
options. Just click on the appropriate choice. Try it—type Cos in an input cell and attempt the
completion. You will find that there are four Mathematica commands that start with these letters:
Cos, Cosh, CosIntegral, and CoshIntegral.
Command Templates
If you know the name of a command, but have forgotten the syntax for its arguments, type the
command name in an input cell, then choose Make Template from the Edit menu. Mathematica will
paste a template into the input cell showing the syntax for the simplest form of the command. For
example, if you were to type Plot, and then choose Make Template, the input cell would look like
this:
Command templates and command completions work well together. Type a few letters, complete
the command, then make the template. It’s an easy way to avoid syntax errors. See Table 2.1 for
keyboard shortcuts.
One could spend the rest of his or her natural life wading through the documentation center; it’s a
big place.
2.10 Loading Packages 43
¿ If so many packages are “standard” and ship with the software, why does one have to load
them separately? Why are they not just built-in? The reason is two-fold. On the one hand,
keeping these packages on the shelf, so to speak, until needed makes Mathematica leaner and
more nimble. If a user will not need these commands in most sessions, keeping them out of
the system means that there will be more resources available for everything else. The user
simply loads packages as they are needed. On the other hand, the design of packages allows for
the possibility that the same command name could have one meaning in one package, and an
entirely different meaning in another. Common mathematical terms such as “tensor,” for
instance, have different meanings in different mathematical contexts, and indeed there are
different packages available that define the command Tensor differently. Thus packages allow
the user flexibility to customize Mathematica to suit the purpose at hand.
To understand in a very basic way how packages work, it is necessary to understand that the
built-in commands have a “full name” and a short name. So far we have only mentioned the
short name. The full name of a built-in command can be had by attaching System` to the
front of it. For instance, the full name of Plot is System`Plot. We say that the Plot command
lives in the System context. The commands found in a package, by contrast, have a context
other than System. For instance, below we give examples from the Units` package. The
command whose short name is Convert is defined in this package; its full name is Units`Cong
vert. One can always call a command by typing its full name, but this is almost never done. It is
only necessary if two commands have the same short name, a situation that we generally try
to avoid. When a package is loaded, its context is recognized, so that calls can be made to the
short name of any command defined in the package.
Hundreds of additional packages are available to download (for free) from the web site
http://library.wolfram.com. At this site, type a topic of your choosing in the search field, and search
within “MathSource.” A listing of hits is displayed, each with a brief summary, a link, and the date
on which it was posted (recent dates are generally better than old). It is a simple matter to follow a
link, then download and install the relevant package. Package files are identical regardless of which
operating system your computer uses. To install a package, simply place the package file (ending in
.m) into the ~/Library/Mathematica/Applications folder in your home directory (Mac OS), or the
Documents and Settings\username\Application Data\Mathematica\Applications folder (Windows).
The following input can be used to get a listing of the standard packages included in your installa-
tion of Mathematica. It looks rather complicated, but it simply instructs Mathematica to look in the
appropriate directory on your computer and report the names of the files that are stored there. Note
44 Working with Mathematica
that it is possible to do this with slightly shorter input, but the following (redundant but simple)
input will work on all platforms (Mac, PC, etc.):
In[1]:= SetDirectory$$InstallationDirectory(;
SetDirectory#"AddOns"';
SetDirectory$"Packages"(;
FileNames#'
It is highly likely that you will at some point need to load a package into Mathematica. To do so, the
Needs command is used. Suppose that you wish to use a package, and either it is a standard package
or you have already downloaded it and placed it in the Applications directory. For example, there is
a standard package called Units that allows you to easily convert units of measurement. To load it,
enter a cell containing the text:
In[5]:= Needs#"Units`"'
This must be typed with perfect precision. The argument to Needs is a String, that is, it is enclosed
in double quotation marks. And the package name will invariably contain one or more backquote
characters ` (look in the upper left portion of your keyboard for the backquote character. Do not use
an apostrophe ’). If the cell is entered properly, there will be no output. If you get an error message,
chances are good that you didn’t type the input exactly right; fix it, then reenter the cell. Under no
circumstances should you attempt to use the commands in the package until it has been properly
loaded. You can check that the package loaded properly by typing and entering
In[6]:= $Packages
The output shows all currently loaded packages; your output may be slightly different. What you
need to look for is the name of the package you tried to load. Since Units` appears in the output, all
is well. If your package does not appear in the list, try using the Needs command again until it does.
Once the package has loaded you can use the commands it contains just as if they were ordinary
Mathematica commands. The Units package contains the command Convert, which allows the
2.10 Loading Packages 45
conversion of just about any imaginable pair of measurement units. The syntax is:
Convert#from, to'
For example, how many miles are there in a light year? How many teaspoons in a 16 gallon tank of
gas? Bartenders take note: How many jiggers in a 1.75 liter bottle?
Note that all units of measurement are given in the singular, so you should type Foot rather than
Feet and Mile rather than Miles. Note also that you may arithmetically combine basic units of
measurement; for instance, you can convert miles per hour to feet per second like so:
You can deal in thousands with the prefix Kilo, which is simply equal to 1000. For instance there is
no unit named Kilometer. Rather, you should use the product (note the space) Kilo Meter.
This brings a natural question to mind: How do you find out what commands are available in a
given package? For instance, what units of measurement are available in the Units package? To find
out, use the Names command (do this after loading the package). The syntax for Names is just like
that of the Needs command, except that you need to place an asterisk between the last backquote
and double quote (the asterisk is the “wild-card” symbol common in many computer applications
and operating systems). You can save yourself some typing by clicking once under the input cell
containing the Needs command, and from the menus choosing Insert # Input From Above. Then edit
the new cell, adding the asterisk and changing Needs to Names. To save space below, we Take only
the 40th through 70th names from over 250 names in the package.
Out[12]=
Candle, Carat, Celsius, Cental, Centi, Centigrade, Centimeter, Century,
CGS, Chain, ChevalVapeur, Cicero, Convert, ConvertTemperature,
Cord, Coulomb, Cubit, Curie, Dalton, Day, Deca, Decade, Deci
Here we see that there are a host of objects defined in the package. Most of them are units of mea-
surement, but two of them, Convert and ConvertTemperature, are commands. You can now find
out about any of these names in the usual way:
In[13]:= ? Butt
In[14]:= ? ConvertTemperature
Out[15]= 100
There is an important thing you need to know about packages. If you accidentally attempt to use a
command defined in a package before the package has been loaded (you’ll know if you’ve done this
because the command won’t work; the output will simply match the input), you’ll create a bit of a
challenge for both yourself and for Mathematica. Suppose, for instance, that you tried to use the
Convert command before loading the Units` package. By calling the Convert command prematurely,
you have inadvertently created a symbol of that name. Mathematica notes that Convert is now a
recognized symbol, albeit a symbol that has no meaning. The next logical step is for you to realize
that you forgot to load the package, and proceed to load it. Now here’s the rub: As Mathematica goes
about loading all the new symbols in the package it will encounter two symbols with the name
Convert, the one in the package and the meaningless one you (inadvertently) created. This will lead
to a warning message as the package loads. It will also lead to the symbol Convert being displayed
in red when it is typed, to flag it as a symbol with conflicting meanings. However beyond this rather
disturbing red display, nothing bad will happen. The package definition takes precedence over your
meaningless one, and everything will work as it should.
You can avoid these issues by simply loading the package before calling any commands in it. And if
you do inadvertently call a command prematurely (this is known as premature evaluation), and
would rather not see the command displayed in red, simply type Remove[Convert] (or whatever
command you accidentally called) before loading the package. This will purge the offending symbol
from the system registry so that there will be no conflict upon loading the package.
2.11 Troubleshooting 47
Exercises 2.10
1. How many gallons are in a butt? Load the Units package and investigate. Make a joke out of the
answer.
2.11 Troubleshooting
The most common problem with learning Mathematica is adapting to a system in which spelling and
syntax must be perfect. What happens if your syntax is wrong (say you typed a period instead of a
comma, or forgot to capitalize a command name)? Usually you will get an error message. Don’t
panic. Most error messages can be traced to a simple typing mistake. Just go back to your last input
cell, edit it, and reenter it. If you can’t find your mistake, ask a friend or your instructor. You may
also want to try the online help features discussed earlier.
In any event, if your input is either generating error messages or not generating the output you
want, look first for spelling or syntax problems. If you are reasonably certain that the command has
been entered correctly, there are a few other things you might try. If Mathematica beeped when you
attempted to enter your input cell, you can go to the Help menu and select Why the Beep?…. This
will provide you with an explanation that may be quite helpful. Another tactic that cures a common
source of problems is to clear the names of any variables appearing in your input, then try reenter-
ing the cell. For instance, if your current input involves a variable called x, and somewhere long ago
you typed x 3, then Mathematica will substitute 3 for x every chance it gets for as long as the
current kernel is running. You may have forgotten that you made such an assignment, and no
longer want it. Type and enter Clear[x] to remove any previous assignment to x, then reenter your
input cell (you will need to clear the values of all expressions that have been assigned values in your
current session; such expressions may or may not be called x in your notebook). Get in the habit of
clearing variable names as soon as you are done with them.
Another reality that you may encounter at some point is that your computer can crash. This occurs
only very rarely under ordinary usage on a computer of recent vintage, but it’s good to be able to
recognize one should it occur.
Recognizing a Crash
When you enter a command to Mathematica’s kernel, the title bar to the notebook window will
display the text “Running….” This label will vanish when the output appears. It is Mathematica’s
way of telling you that it is working on a calculation. Some calculations are fast, but some are slow,
and some are very slow (hours, days, even weeks). How much time a calculation will require depends
48 Working with Mathematica
on the complexity of the calculation and the type of computer being used. If you have entered a
command and nothing seems to be happening, don’t despair. It is likely that you have simply asked
a difficult question (intentionally or not) and it will take Mathematica a bit of time to answer.
If you don’t have time to wait and just want Mathematica to stop, read on.
Or if (heaven forbid) the cursor does not respond when you move the mouse, and the keyboard does
not seem to work, it is likely that a crash has occurred. Don’t panic, and don’t pull the plug just yet.
Read on…
If nothing happens when you attempt to abort, you will have to take slightly more decisive action:
You will have to quit the kernel. To do this, go again to the Evaluation menu, but this time select
Quit Kernel (you then have to select the kernel that is running, usually the local kernel), then hit the
Quit button when it asks if you really want to quit the kernel. The only consequence here is that if
you wish to continue working, you will have to start a new kernel. This will happen automatically
when you enter your next input. Remember that the new kernel will not be aware of any of your
previous calculations, so you may have to reenter some old cells to bring the new kernel up to date
(if your new commands make reference to any of your previous work).
Now for those of you who have lost control of the mouse and keyboard due to a crash, none of the
above is possible. Ideally, you would like to be able to quit Mathematica without losing any of your
unsaved work. It’s not always possible; this is why it’s a good idea to save your work often.
The action that you should take depends to some extent on what type of computer you are using.
Let’s proceed by platform:
Mac OS Procedure
First, try simultaneously hitting Ð and . (that’s the period key). This is just the keyboard equivalent
of selecting Abort Evaluation from the Evaluation menu as described above. It probably won’t work,
but give it a try. We’ve seen instances in which the mouse failed in the middle of a long calculation.
No crash, just a worn mouse that died at an inopportune time.
If that doesn’t work, try simultaneously hitting ÐQ. If this works you will be presented with a dialog
box asking if you wish to save your work. Answer “Yes.” In this case, the result will be quitting the
entire Mathematica program (front end and kernel).
If that doesn’t work, simultaneously hit the ÐØ¹È keys. A dialog will appear asking which applica-
tion you wish to Force Quit. Choose Mathematica. This is almost always effective. The front end and
kernel will quit, but you will not have an opportunity to save your work.
2.11 Troubleshooting 49
As a last resort, you will have to turn off your computer manually. Any unsaved changes will be lost.
If the computer has a reset button, use it. Otherwise find the “off ” button (often on the back of your
computer) and use it. Wait a few seconds and restart the computer in the usual way.
Windows Procedure
First, try simultaneously hitting à and . (that’s the period key). This is just the keyboard equivalent
of selecting Abort Evaluation from the Evaluation menu as described above. It probably won’t work,
but give it a try. We’ve seen instances in which the mouse failed in the middle of a long calculation.
No crash, just a worn mouse that died at an inopportune time.
If that doesn’t work, simultaneously hit the Å¹Ã¹Æ keys. This is usually effective. You should be
presented with a dialog box. Hit the Task Manager button, then look under the Applications Tab for
Mathematica. Select it, and hit the End Task button to quit Mathematica altogether. It may be possi-
ble to save your notebook before quitting. You should restart your computer before launching
Mathematica again. This will decrease the likelihood of another crash.
As a last resort, you will have to restart your computer. Again, any unsaved changes will be lost.
First, quit other programs (such as your web browser) when using Mathematica. Other programs also
require RAM, so running them at the same time steals valuable memory from Mathematica. Also,
even though it is possible to have multiple notebooks open at one time, avoid having more note-
books open than necessary. Each open notebook will consume memory. You should also save your
notebooks often. Doing so will allow Mathematica to store part of it on your computer’s hard drive,
rather than storing all of it in RAM. Finally, if you work on your own computer and are in the habit
of leaving Mathematica running for days or weeks at a time, quit the kernel from time to time to
flush out any symbols that are not being used.
3
Functions and Their Graphs
In[1]:= Log#1'
Out[1]= 0
You can define your own function in Mathematica like this (use the BasicMathInput palette to type
x2 ; see Section 1.6, page 4):
In[3]:= f#1'
Out[3]= 1
In[4]:= f#S'
Out[4]= 4 2 S S2
As a second example, here is a function that will return the multiplicative inverse of its argument
(again, use the BasicMathInput palette to type the fraction):
1
In[5]:= inv#xB' :
x
Let’s try it:
In[6]:= inv#45'
1
Out[6]=
45
You can also create functions by combining existing functions:
52 Functions and Their Graphs
In[8]:= g#45'
Out[8]= 0.0222222
Defining a Function
Follow these rules when defining a function:
Ê The name of the function (such as f or inv) should be a lowercase letter, or a word that
begins with a lowercase letter. This is because all built-in functions (such as Log and N)
begin with capital letters. If your function begins with a lowercase letter, you will never
accidentally give it a name that already belongs to some built-in function.
Ê The function argument (in these examples x) must be followed by an underscore _ on
the left side of the definition.
Ê Use square brackets [ ] to enclose the function argument.
Ê Use the colon-equal combination := to separate the left side of the definition from the
right.
After typing the definition, enter the cell containing it. Your function is now ready for action.
¿ The := operator (called the SetDelayed operator) used in defining functions differs in a subtle
way from the = operator (called the Set operator) used for making assignments (the = operator
was discussed in Section 1.10—see page 20). Essentially, when you use := the expression
appearing to its right is evaluated anew by the kernel each time that the expression appearing
to its left is called. The = operator, by contrast, evaluates the expression on its right only once,
at the time the assignment is made. In many settings = and := can be used interchangeably;
however, there are cases when one is appropriate and the other is not. Using SetDelayed for
function definitions will work in virtually every setting, and we will use it consistently for that
purpose throughout this book.
An illustrative example is the following: Type and enter x RandomInteger#100'; x, x, x.
Then change = to := and do it again. In the first case, x is set to be a single (randomly chosen)
integer, so the output is a list in which that same number appears three times. In the second
case, each x causes a new random integer to be chosen, so the output is a list of three
(probably) distinct numbers.
For more information, go to the Documentation Center and type SetDelayed in the text field,
then follow the link to the tutorial titled “Immediate and Delayed Definitions.”
3.2 Plotting a Function 53
Clearing a Function
A word to the wise: Once you are finished working with a function, get rid of it. Why? One reason is
that you may forget about the function and later in the session try to use the name for something
else. But Mathematica won’t forget, and all sorts of confusion can result. Another is that in getting
rid of a function you will clear out a little bit of memory, leaving more room for you to work. To see
if a letter or word has been defined as a function, use the ? command just as you would for a built-
in Mathematica command:
In[9]:= ?f
Global`f
f#xB' : x2 2 x 4
This indicates that f is still retained in memory. You can use the Clear command to erase it, just as
you would to erase the value of a constant:
In[10]:= Clear#f'
Now if you use the ? command you will find no such definition:
In[11]:= ?f
Global`f
¿ To clear out every user-defined symbol from the current session, try Clear["Global`*"]. The
asterisk is a wild-card symbol; this essentially says, “Clear all symbols defined in the Global
context” (the Global context is the default location where user-defined symbols are stored).
In[1]:= Clear#f';
f#xB' : x2 2x 4
54 Functions and Their Graphs
5
Out[3]=
1 1 2 3
The Plot command takes two arguments, separated (as always) by a comma. The first (in this case
f[x]) is the function to be graphed, and the second (in this case {x, -1, 3}) is called an iterator. It
describes the span of values that the variable x is to assume; that is, it specifies the domain over
which the plot will be constructed. The curly brackets are essential in describing this domain. In
fact, Mathematica uses this iterator structure in numerous commands, so it warrants a bit of discus-
sion. The first item (x) names the variable, and the next two items give the span of values that this
variable will assume (1 through 3). Values in this domain are displayed along the horizontal axis,
while the values that the function assumes are displayed along the vertical axis.
Note that the axes in this plot do not intersect at the origin, but rather at the point +0, 3/. Every time
you use the Plot command Mathematica decides where to place the axes, and they do not always
cross at the origin. There is a good reason for this. As often as not you will find yourself plotting
functions over domains in which the graph is relatively far from the origin. Rather than omit one or
both axes from the plot, or include the axes together with acres of white space, Mathematica will
simply move the axes into view, giving your plot a frame of reference. If you really want to produce
a plot with the axes intersecting at the origin, you can. The details are provided in the next section
of this chapter, “Using Mathematica’s Plot Options.”
You can zoom in on a particular portion of a plot simply by editing the domain specified in the
iterator, then reentering the cell. Let’s take a close look, so to speak, at the function in the last
example, this time with x values near 2. Notice how “flat” the graph becomes:
4.1
Out[4]= 4.0
3.9
You could zoom in even more (say with a domain from 1.99 to 2.01) and get a more detailed view of
the function’s behavior near the point x 2. In the plot below we show an extreme zoom (and make
use of the lowercase Greek letter G, which mathematicians often use to denote small quantities).
Here we also employ the With command, a device that allows you to make local assignments; the
3.2 Plotting a Function 55
assignment G 1010 will only be utilized within the Plot expression, and will not be remembered
by Mathematica later.
Out[5]= 4
2 2 2 2
Note that the numerical values on each axis display identically; this is simply because so many
decimal places (ten in this case) are needed to distinguish them that there isn’t room for their
display. In principle you could keep zooming in forever, but in practice this is not possible. See
Exercise 3 for a discussion on the limits of zooming.
Another little trick that’s good to know about is how to resize a graphic. This technique is best
learned by trying it, so get yourself in front of the computer and produce a plot. Position the cursor
anywhere on the graph and click once. A rectangular border with eight “handles” appears around
the graph. Position the cursor on a handle and drag (hold down the mouse button and move the
mouse) to shrink or enlarge the graphic. It’s easy; try it.
In[6]:= Clear#f';
x5 4x2 1
f#xB' :
1
x
2
60
40
Out[8]=
20
3 2 1 1 2 3
20
Something strange seems to be happening when x 1 s 2 (see that vertical blip?). What’s happening
is this: the function is not defined when x 1 s 2, since the denominator of f is equal to zero at this
value of x. Think of there being an imaginary vertical line, an asymptote, at x 1 s 2 through which
the graph of f cannot pass. In order to understand Mathematica’s output it is important to
understand how the Plot command works. Plot samples several values of x in the specified domain
56 Functions and Their Graphs
and numerically evaluates f +x/ for each of them. After refining its selection of sample points via an
adaptive algorithm, it then plots these points and “connects the dots” with little line segments.
There are so many that the graph appears in most places like a smooth curve. The important issue
approximations. For instance, the vertical-looking segment that crosses the x axis near 1 s 2 is not
here is that Mathematica’s plots are not exact in a mathematical sense; they are only
x 1 s 2, the function values got smaller and smaller. The last point that was plotted to the left of the
part of the true graph of f . As Mathematica plotted successively larger values of x, just to the left of
true asymptote took a large negative value. The very next point plotted, just to the right of the true
asymptote, took a large positive value. Mathematica then connected these two points with a line
segment (so in fact that vertical-looking segment tilts ever so slightly from lower left to upper right).
Mathematica had no way of knowing that in fact the true graph of f never crosses the vertical
asymptote. Although technically inaccurate, this isn’t a bad state of affairs. You can interpret the
plot as the graph of f with the asymptote roughly drawn in. And an important lesson can be learned
here: Never trust the output of the computer as gospel; it always demands scrutiny.
Beware also that vertical asymptotes (and other “narrow” features) in a plot will change in appear-
ance as the specified domain changes. Asymptotes may disappear or become barely noticeable. For
instance, here is another view of the function f , this time zoomed out to accommodate the domain
from 10 to 10. The asymptote appears to have vanished:
8000
6000
Out[9]=
4000
2000
10 5 5 10
The asymptote is almost invisible because Mathematica (by chance) skipped over those values of x so
close to the asymptote that f +x/ would return very large or very small values. The “true” graph of f
still spikes up toward infinity just to the right of the asymptote and down to negative infinity just to
the left of it. The point of all this is to make clear that the plots Mathematica produces are approxima-
tions. They may hide important features of a function if those features are sufficiently narrow
relative to the domain over which the function is plotted. When it comes to finding a function’s
asymptotes, for instance, looking for them on a plot is not necessarily the best approach. We’ll
discuss better methods for finding vertical asymptotes (by finding explicit values of x for which the
denominator is equal to zero) in the next chapter.
We note that if you do know the precise numerical position of a function’s vertical asymptote(s),
you can add these values between the lower and upper numbers in the iterator. Mathematica will
omit such points from the resulting plot, and will hence produce a more accurate plot. In the next
3.2 Plotting a Function 57
section, the Exclusions option will be introduced; this provides a sophisticated means of excluding
points from a domain.
5
10
A more subtle issue may arise from the manner in which Mathematica utilizes the complex number
system. There are cases in which there are two potential definitions for a function: one which
disallows complex numbers, and another which embraces them. Mathematica will always embrace
them, and this can lead to some unexpected results. In particular, students in precalculus or calculus
1.5
Out[11]= 1.0
0.5
5 5
know that +2/3 8, so the cube root of 8 should be 2, shouldn’t it? The issue is a subtle one. In
The left side of the plot is empty. That seems odd; don’t negative numbers have cube roots? We
the complex number system there are three numbers whose cube is 8, (they happen to be 2 and
the two complex numbers 1 3 Ç). Mathematica, savvy as it is regarding the complex numbers,
takes one of the complex numbers to be the cube root of 8. In a similar manner, it regards the cube
root of every negative number to be complex. It can’t plot a complex number, and so the left half of
the plot is empty. A thorough discussion of why Mathematica chooses complex values as the cube
roots of negative numbers can be found in Section 4.4 on page 162. Suffice it to say that there are
very good reasons for doing so, but that it can be an annoyance to those who would like to study
the real-valued cube root function (and remain blissfully ignorant of the complex number system).
If you would like to see the plot of the real-valued cube root function found in many precalculus
and calculus texts (where the cube root of 8 is taken to be 2), one can define an alternative Power
58 Functions and Their Graphs
command, as follows:
A discussion of the If command can be found in Section 8.5. At this point it’s okay to ignore the
those powers are rational numbers with odd denominator (powers such as 1 s 3). Here is how to use
details of this definition, and to use it freely. It will modify the powers of negative numbers when
this alternate Power command to produce a plot of the real-valued cube root function:
Out[13]=
5 5
1
2
realPower will differ from the built-in Power command only if the power (1 s 3 in the example
above) is a rational number. This will suffice for the types of power functions typically encountered
in precalculus and calculus courses. Just be sure to enter an exact rational number (no decimal
points) as the second argument to realPower.
Exercises 3.2
1. Plot the following functions on the domain 10 x 10.
a. sin+1 cos+x//
b. sin+1.4 cos+x//
S
c. sin, cos+x/0
2
d. sin+2 cos+x//
appears at different zoom levels. For instance, consider the square root function f +x/
2. One can zoom in toward a particular point in the domain of a function and see how the graph
x when
x is near 2.
a. Enter the input below to see the graph of f as x goes from 1 to 3.
b. Now zoom; change the value of G to be 101 and re-enter the input above to see the graph of f
as x goes from 1.9 to 2.1. Do this again for G 102 , 103 , 104 , and 105 .
3.3 Using Mathematica’s Plot Options 59
c. Use the last plot to approximate 2 to six significant digits. Check your answer using N.
d. When making a Plot, the lower and upper bounds on the iterator must be distinct when
rounded to machine precision. Enter the previous Plot command with G 1020 . An error
message results. Read the error message and speculate as to what is happening. The bottom
line is that zooming has its limits.
3. Use the realPower command to plot the real-valued function f +x/ x4s5 on the domain
32 x 32. What is the value of f +32/?
800
600
Out[1]= 400
200
3 2 1 1 2 3
when « x « exceeds 2.5 or so). This is because beyond the portion shown Mathematica observed no
Notice how Mathematica only showed us a portion of what we asked for (the graph is not shown
interesting behavior; the graph just kept going up on the left and on the right. The (boring) informa-
tion near the edges was clipped off to give a better view of the middle portion of the plot. Mathemat-
ica will do this clipping by default.
The option PlotRange is set by default to an automated setting which will sometimes result in a
graph with a truncated vertical scale. But suppose you really want to see the function over the full
domain from 3 to 3. You can indicate this by adding PlotRange Full as an additional argument
to the Plot command. It must be placed after the two required arguments. The arrow is found on
the BasicMathInput palette; alternatively, it can be typed from the keyboard as a “minus” sign
followed by a “greater than” sign: ->. After typing these two symbols one after the other, they will
turn into the arrow on their own at the next keystroke.
60 Functions and Their Graphs
8000
6000
4000
Out[2]=
2000
3 2 1 1 2 3
Look at the output. The left and right sides of the plot now climb almost ten times higher, and as a
result the detail in the middle is harder to surmise. It’s a very different picture.
After exploring this function in the previous two graphs it is clear that the interesting behavior
occurs above the x-axis and below y 250. You can specify the exact range of values you wish to
display using PlotRange ymin, ymax . If your desired range is n to n, PlotRange n will suffice.
250
200
150
Out[3]=
100
50
3 2 1 0 1 2 3
In general, you type the name of the option, followed by , followed by the desired setting for the
option. The philosophy of allowing commands such as Plot to accept options is simple: very little
typing is required to allow the command to be used in its default form. But when the default output
is not entirely to your liking you have the ability to tweak the default settings to your heart’s con-
tent. There are over 50 options for the Plot command, several of which are discussed below. You
may add several option settings to a command, and in any order you wish (provided each optional
argument is listed after the required arguments); just use commas to separate them.
2.5
Out[4]= 2.0
1.5
Be mindful that in many cases you definitely do not want your axes to have the same scale. You
could, for instance, very easily ask for a plot that was a few inches wide and a few miles high.
Imagine the plot at the beginning of this section if you are skeptical. That is why the default aspect
ratio (the ratio of height to width) is set to a fixed value. In other words, by default Plot will scale
the axes in such a way that the graph will fit into a rectangle of standard proportions. It’s best to
add the AspectRatio Automatic option only after you have viewed the plot and determined that
its use won’t result in a plot that’s too long and skinny.
Note that you can also set AspectRatio to any positive numerical value you like. The plot will have
the height to width ratio that you specify. For instance, the setting AspectRatio 3 will produce a
aspect ratio. In Mathematica, we can obtain these dimensions with the setting AspectRatio 9 s 16.
plot that is three times as high as it is wide. Widescreen televisions are advertised to have a 16 : 9
2.5
2.0
Out[5]= 1.5
1.0
0.5
1 2 3 4 5
62 Functions and Their Graphs
Note that the domain specified is 3 x 5, yet the option setting extends the graphic beyond these
values. You may need to adjust the AspectRatio as well if you end up with something too long and
thin.
0.8
0.6
Out[6]=
0.4
0.2
1 2 3 4 5 6
Note that more points are generated in regions where the function bends sharply. The graph itself is
comprised of line segments joining these points.
To show points whose x coordinates are regularly spaced, use the option Mesh Full or Mesh n
where n is the desired number of points (not counting endpoints).
0.8
0.6
Out[7]=
0.4
0.2
1 2 3 4 5 6
0.8
0.6
Out[8]=
0.4
0.2
1 2 3 4 5 6
¿ One can display any finite collection of mesh points by setting Mesh to a list of x coordinates,
such as Mesh 1, 2, 3. One can programmatically generate this list of x coordinates using
3.3 Using Mathematica’s Plot Options 63
Range (for equally spaced x coordinates), or Table (see Section 3.5 for a discussion of the
Table command). Even more control can be garnered by setting the MeshFunctions option,
which specifies which function or functions are to be set to the list of Mesh values. Typically
such functions are given as pure functions (see Section 8.4 for a discussion of the Function
command). By default, MeshFunctions is set to Ó1 &, meaning that the list of Mesh values is
a list of x coordinates. With the setting MeshFunctions Ó2 &, the list of Mesh values
becomes a list of y coordinates. See Exercise 5 for examples.
2.5
2.0
Out[9]=
1.5
You may use any standard color name; for a list of all colors go to the Documentation Center, type
“Colors” in the search field, and navigate to the guide page of that name. You may also use a lighter
or darker version of any color; just replace Red, for instance, with Lighter#Red', or Lighter#Red, .7'
or Darker#Red, .2'. The second numerical argument may be omitted. If present, it determines the
extent of the lightening or darkening, and should be set to a value between 0 (no effect) and 1
(maximal effect).
2.5
2.0
Out[10]=
1.5
You can also blend two or more colors. Setting PlotStyle to Blend#Blue, Red, .3' will produce a
blend of 70% blue and 30% red. And one could nest these settings to create a custom color such as
Lighter#Blend#Blue, Red, .3', .4'. Other color settings are discussed in Exercise 3.
64 Functions and Their Graphs
These color settings are examples of graphics directives. The PlotStyle option may be set to any single
graphics directive (such as the color directives outlined above), or simultaneously to several such
directives. Multiple directives should be wrapped in the Directive command. For instance, one can
apply the directives Thick, Gray, and Dashed as follows:
2.5
2.0
Out[11]=
1.5
Other common Plot options that accept graphics directive settings are AxesStyle, Background,
FillingStyle, FrameStyle, and MeshStyle.
Out[12]=
To replace the axes with a frame around the entire graph, add the option Frame True :
3.3 Using Mathematica’s Plot Options 65
2.5
2.0
Out[13]=
1.5
1.0
3.0 3.5 4.0 4.5 5.0
In[14]:= Plot$2+x 4/2 1, x, 3, 5, AxesStyle Arrowheads#.05', AxesOrigin 2, 0(
2.5
2.0
1.5
Out[14]=
1.0
0.5
Out[15]= 1
1 1
When adding arrowheads, it may be desirable to manually increase the PlotRange for both axes to
place the arrowheads farther from the center of your plot. This will allow room to display more tick
marks on the axes.
66 Functions and Their Graphs
Out[16]= 1
2 1 1 2
1
0.8
0.6
Out[17]=
0.4
0.2
1 2 3 4 5 6
The appearance of the grid lines is controlled by the GridLinesStyle option, which can be set to any
graphics directive.
0.8
0.6
Out[18]=
0.4
0.2
1 2 3 4 5 6
To adjust the placement of the grid lines, set GridLines to a list of two lists: the first consists of x
values indicating the positions of the vertical lines, and the second consists of y values indicating
the positions of the horizontal lines:
3.3 Using Mathematica’s Plot Options 67
Plot%Sin#x'2 , x, 0, 2S, GridLines , S, , 2S!, .2, .4, .6, .8, 1!)
S 3S
In[19]:=
2 2
1.0
0.8
0.6
Out[19]=
0.4
0.2
1 2 3 4 5 6
For fine grids, use Range to generate each of the x and y lists. Range is used to generate a list of
evenly spaced numerical values. For example, Range#0, 1, .1' generates a list of numbers from 0 to 1
in increments of one tenth:
Out[20]= 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.
0.8
0.6
Out[21]=
0.4
0.2
1 2 3 4 5 6
Numerical tick marks on the axes are controlled via the Ticks option, which works in a manner
similar to GridLines. If you are happy with the default list of tick marks on one of the axes, just use
Automatic instead of a specific list of values.
68 Functions and Their Graphs
0.8
0.6
Out[22]= 0.4
0.2
S 3S
0 S 2S
2 2
In[23]:= Plot$Sin#x'2 , x, 0, 2S, Ticks 0, S, 2S, 0, .5, 1, AxesLabel x, Sin#x'2 (
sin2 +x/
1
Out[23]=
0.5
x
S 2S
You can put a label on the entire plot with the option PlotLabel.
3.3 Using Mathematica’s Plot Options 69
In[24]:= Plot$Sin#x'2 , x, 0, 2S, Ticks 0, S, 2S, 0, .5, 1,
AxesLabel x, y , PlotLabel Sin#x'2 (
sin2 +x/
y
Out[24]=
0.5
x
S 2S
Labels that include operators (such as =), or that are comprised of more than one word, should be
entered as a String, i.e., put in double quotation marks. In this case, the text between the quotation
marks will be reproduced exactly as you write it. Below, for instance, we italicized the x and the y as
we typed the label text.
In[25]:= Plot$Sin#x'2 , x, 0, 2S, Ticks 0, S, 2S, 0, .5, 1,
AxesLabel x, y , PlotLabel "y sin2 +x/"(
y sin2 +x/
y
Out[25]=
0.5
x
S 2S
If one had not used double quotation marks, Mathematica would have actually made the nonsensical
assignment y sin2 x, possibly causing confusion later in the session. When in doubt, wrap your
plot labels in double quotes.
An alternate means of labeling will work not only for plots, but for labeling any Mathematica expres-
sion. Simply wrap the expression to be labeled in the Labeled command, and add a second argu-
ment that specifies the text for the label. The label appears at the bottom by default, but if present a
third argument may be given to specify the position of the label. Look up Labeled in the Documenta-
tion Center for information about micro-positioning the label text.
70 Functions and Their Graphs
In[26]:= p Plot$Sin#x'2 , x, 0, 2S, Ticks 0, S, 2S, 0, .5, 1, AxesLabel x, y (;
Labeled$p, Text$"y sin2 +x/"(, Right(
y
sin +x/
Out[27]= 0.5 2
y
x
S 2S
+x 3/+x 4/
, x, 0, 7)
+x 2/+x 5/
In[28]:= Plot%
2
Out[28]=
1 2 3 4 5 6 7
2
4
+x 3/+x 4/
, x, 0, 2, 5, 7)
+x 2/+x 5/
In[29]:= Plot%
2
Out[29]=
1 2 3 4 5 6 7
2
Even more control can be garnered using the options Exclusions and ExclusionsStyle. Exclusions
can be set to a list containing an equation or equations whose solution(s) you wish to exclude. Use
3.3 Using Mathematica’s Plot Options 71
two equal signs back to back m when typing an equation. ExclusionsStyle specifies the directive(s)
applied to the vertical line(s) through the points to be excluded, enabling you to include vertical
asymptotes in your plot. Multiple directives should be wrapped in the Directive command, as was
done earlier with PlotStyle.
+x 3/+x 4/
, x, 0, 7, Exclusions x m 2, x m 5, ExclusionsStyle Dashed)
+x 2/+x 5/
In[30]:= Plot%
2
Out[30]=
1 2 3 4 5 6 7
2
4
The benefit of expressing exclusions as equations is illustrated in the following example, where a
single equation has many solutions in the specified domain:
Note that Exclusions has little visible effect at a point unless there is an essential discontinuity
there. See Exercise 4.
To put a logarithmic scale on the vertical axis, use the command LogPlot:
72 Functions and Their Graphs
500
400
Out[32]= 300
200
100
x log+b/.
100
50
Out[33]=
10
5
To put a logarithmic scale on the horizontal axis, use the command LogLinearPlot:
2.5
Out[34]=
2.0
2.5
2.0
Out[35]= 1.5
1.0
0.5
5 10 50 100 5001000
800
600
Out[36]=
400
200
20 40 60 80 100
100
50
Out[37]=
10
5
2 5 10 20 50 100
Exercises 3.3
1. Use the GridLines and Ticks options, as well as the setting GridLinesStyle Lighter$Gray(, to
produce the following Plot of the sine function:
1.0
0.5
2 S S S 2S
0.5
1.0
2. Use the Axes, Frame, Filling, FrameStyle, PlotRange, and AspectRatio options to produce the
cos+15x/
following plot of the function y :
1x2
74 Functions and Their Graphs
3. Color values such as Red, Blue, and Orange are easy to type and to remember, but they present
you with a very limited color palette. Standard color spaces used in graphic design, such as RGB
(for Red-Green-Blue) and HSB (for Hue-Saturation-Brightness), are supported. The command
RGBColor take three arguments, each a number between 0 and 1. They represent the relative
amounts of red, green, and blue, respectively, in the final color. Hue takes either one argument
(the color setting), or three, where the second and third are saturation and brightness levels. Each
is a number between 0 and 1. You may type in values yourself (such as RGBColor#.2, .8, .8' or
Hue#.6, .5, .5'), or you may do this: type an option setting such as PlotStyle , and with the
cursor still at the tip of the arrow go to the Insert menu and select Color…. A dialog box appears,
and you can use it any way you like to choose the color you’re after. When you have it, hit the
OK button. You’ll find the appropriate RGBColor setting pasted in your notebook at the position
of the cursor. Experiment with both methods, direct typing and using the menu, to custom-color
the Plot of a function of your choosing.
4. Plot the function f +x/ x2 on the domain 2 x 2, and set Exclusions to x m 1. Note that f
has no vertical asymptote at x 1. What happens?
5. In order to place mesh points on the graph so that their y coordinates are equally spaced, one
may set the MeshFunctions option to {#2&}. This notation is explained in depth in Section 8.4
on page 403, but for our purposes it will suffice to understand that #1 refers to x and #2 refers to
y, and that the ampersand character & is needed to make it a function. The mesh points will be
displayed at the specified values for this function. For example, a numerical Mesh setting of 9
indicates that there should be 9 equally spaced values. A list of Mesh values indicates that the
specific values in the list should be used as values for the function. For instance, consider the
input and output:
3.3 Using Mathematica’s Plot Options 75
100
80
60
Out[38]=
40
20
2 4 6 8 10
a. Replace the None in the input above with the appropriate of list of x values to add vertical
GridLines that pass through these same mesh points.
b. Add a GridLines setting to the input below so that the output includes (equally spaced)
vertical grid lines that pass precisely through the mesh points, and (unequally spaced) horizon-
tal grid lines that pass through the same mesh points.
80
60
Out[39]=
40
20
2 4 6 8 10
6. Add Mesh and MeshFunctions options to the input below so that the mesh points are precisely
the points where the graphs of the two functions intersect.
0.5
Out[40]=
1 2 3 4 5 6
0.5
1.0
76 Functions and Their Graphs
x
Out[1]=
The controller, aptly called a manipulator, initially displays as a slider. Clicking on the button to
the right of the slider, however, will reveal additional controls beneath:
x
2.6
6.76
is displayed in the input field directly under the slider. The function value f +x/
Now operate the slider. It ranges over the values from 1 to 10 in this example, and the current value
x2 is displayed
below. Try it. As you position your mouse over any control button, a tooltip will appear that
describes that button’s function. For instance, the Play/Pause button ¨ is used to start and stop an
animation, while the buttons on either side of it will advance it forward or backward one frame at a
time. The double up and down arrow buttons are used to adjust the speed of the animation, and the
direction button on the far right is used to determine whether the animation will play forward,
backward, or oscillate (forward to the end, then backward to the beginning, and so on). You may
also type a specific numerical value directly into the input field, followed by the ¢ key. Note that if
you hit Ú¹Î¢Ï (or Ö on a Mac) after typing in the input field, you will generate a second output
cell.
The Manipulate command in its most basic form takes two arguments, separated by a comma. The
first (in this case x2 ) describes the expression to be manipulated. The second (in this case {x,1,10}) is
an iterator. If you wish to have your variable increase in unit steps only, you can add a fourth
element to the iterator that will specify the amount by which the variable will skip from one value
to the next. For example, here we increment the variable x in steps of size 5. When the slider is
moved, x jumps from 0 to 5 to 10, etc.
3.4 Investigating Functions with Manipulate 77
x
Out[2]=
In the next example, we make a plot of the function f +x/ x sin+1 s x/, the right endpoint of which is
controlled with a slider, while the left endpoint is fixed at 0. When the controller is moved to the
left, the plot’s domain narrows, and the user is afforded a zoomed-in view of the function’s behav-
ior near x 0.
0.05
Out[3]=
0.02 0.04 0.06 0.08 0.10
0.05
In cases such as this it would be nice to put a more descriptive label on the slider. This can be
accomplished either by giving the controller variable a more descriptive name (e.g., one might use
xmax or rightEndpoint), or by replacing the controller variable r in the iterator above by a list of
the form {var , init , label}. Here var is the variable name, init is the initial value to be assumed by the
variable upon evaluation, and label is the label you want to be displayed on the interface. For
instance, below we generate the same output as above, except that we create a slider with the label
right endpoint, and whose initial value upon evaluation will be 0.2.
78 Functions and Their Graphs
right endpoint
0.10
0.05
Out[4]=
0.05 0.10 0.15 0.20
0.05
0.10
0.15
It is possible to place several controller variables in a single Manipulate. It is also possible to simulta-
neously animate all of them using their individual controls. Below we explore three directive set-
tings for PlotStyle.
Thickness
Dash Size
Percent Blue
Out[5]=
4
10 5 5 10
2
4
3.4 Investigating Functions with Manipulate 79
When manipulating a Plot, it is often desirable to include a PlotRange setting to maintain a fixed
viewing rectangle as the controllers are moved. Here we use three controls to adjust some coeffi-
cients on a parabola:
In[6]:= Manipulate$
Plot$a+x b/2 c, x, 5, 5, PlotRange 5, PlotLabel "y a +xb/2 c"(,
a, 1, 1, b, 1, 3, 3, c, 2, 3, 3(
y a +xb/2 c
Out[6]=
2
4 2 2 4
2
4
There are numerous control types available other than sliders. Below we force Manipulate to use a
SetterBar (a row of buttons, only one of which can be selected at a time) simply by changing the
syntax of the iterator. The values to be assumed by the controller variable are given explicitly as a
list. This example is useful for exploring the roles of the PlotPoints, MaxRecursion, and Mesh
options in producing a Plot. Note that the default setting for PlotPoints is 50, so most of the settings
for this option below force Mathematica to produce a poor image.
Manipulate$
Plot$ Sin#4 s x', x, 2S, 2S, PlotPoints pp, MaxRecursion mr, Mesh m(,
In[7]:=
PlotPoints 4 8 16 32 64
MaxRecursion 0 1 2 3 4
1.0
Out[7]=
0.5
6 4 2 2 4 6
0.5
1.0
The control type adapts to the syntax used in the iterator for that control. For instance, if a list of
values associated with a controller variable contains six or more items, the controller will change
from a SetterBar (as in the previous example) to a PopupMenu. While a PopupMenu is desirable if
there is a very long list of choices, we prefer a simple SetterBar as long as there is room for it. In the
next example we override the default behavior with an explicit ControlType option setting.
Manipulate$
Plot$f#x', x, 0, 4S, Ticks Range#0, 4S, S s 2', Automatic , PlotLabel f#x'(,
In[8]:=
f, Tan, "function", Sin, Cos, Sec, Csc, Tan, Cot, ControlType SetterBar (
0.
Out[8]= 2
S 3S 5S 7S
S 2S 3S 4S
2 2 2 2
2
4
6
There are many other useful controller types. For instance, you can produce the controller known as
Slider2D by creating a Manipulate variable whose value is set to an ordered pair of the form {x,y} (i.e.
3.4 Investigating Functions with Manipulate 81
a point in the plane), and specifying its bounds as {xmin,ymin} and {xmax,ymax}. Below we illustrate
this by letting the user manipulate the AxesOrigin setting with a two-dimensional slider:
20
Out[9]=
20 10 10 20
10
20
Another means of manipulating a point in a graphic is to use an iterator of the form {var,Locator},
as shown below. Drag the Locator icon with your mouse to move it directly across the graph, or
simply click on the graphic to move the Locator to that location. The {0,0} in the input below
specifies the initial position of the locator when the cell is first evaluated.
20
10
Out[10]=
20 10 10 20
10
20
The simple iterator structure {var , colorSetting} will produce a color slider. You can drag over the color
field with the mouse to adjust the color continuously in real time.
82 Functions and Their Graphs
color
0.4
0.3
0.2
Out[11]=
0.1
5 10 15 20
0.1
0.2
A complete listing of permissible iterator syntax structures and their corresponding default control-
ler types can be had in the More Information section of the documentation page for Manipulate.
We summarize this information in Table 3.1.
u, umin ,
Iterator Form Default ControlType
Slider2D
Table 3.1 Iterator structures for Manipulate variables and the default control types they produce.
When making a Manipulate object it is important to put the right controllers in place for the task
other than the default. For example one might replace the Manipulate iterator u, 0, 10 by
at hand. Options may be added to any of the iterator forms so that one may specify a controller
u, xmin , ymin , xmax , ymax , dx, dy InputField, Locator, Slider2D, None
u, color
SetterBar, Slider, TogglerBar, VerticalSlider, None
u
ColorSetter, ColorSlider, InputField, None
InputField, None
Table 3.2 Valid control type settings for the various iterator structures used in Manipulate.
Each of the commands ListAnimate, FlipView, PopupView, OpenerView, and SlideView accepts a
list of expressions, and creates an environment in which the user can dynamically interact with the
individual expressions. OpenerView accepts a list containing only two expressions: a header, and an
expression to display when it is in the “open” state.
In[12]:= OpenerView$Style$"click the triangle", "Text"(, Style$"Hah, you did it", "Section"( (
The commands MenuView and TabView accept lists of the form {label1 expression1 , label2
expression2 ,…}, and return a menu of labels or a collection of tabs, respectively, associated with their
corresponding expressions.
84 Functions and Their Graphs
sine cosine
1.0
0.5
Out[13]=
1 2 3 4 5 6
0.5
1.0
Exercises 3.4
1. The following simple Manipulate has two sliders: one for x and one for y. Make a Manipulate
that also has output {x,y}, but that has a single Slider2D controller.
y
Out[14]=
0, 0
starting value of 1 s 5 (five times as wide as it is tall) to an ending value of 5 (five times as tall as it
2. Make a Manipulate of a Plot where the user can adjust the AspectRatio in real time, from a
is wide). Set ImageSize to Automatic, 128 so the height remains constant as the slider is
moved.
3. Make a Manipulate of a Plot where the user can adjust the Background in real time.
a. Use the setting Background RGBColor$r, g, b(, where r , g , and b are Manipulate variables
that range from 0 to 1. They will control the relative amounts of red, green, and blue in the
background, respectively. This allows you to interactively explore the RGB color space.
b. Use the setting Background Hue#h, s, b', where h, s, and b are Manipulate variables that
range from 0 to 1. They will control the values of hue, saturation, and brightness in the
background, respectively. This allows you to interactively explore the HSBcolor space.
4. It is often the case that one wants to create a Manipulate that includes some sort of explanatory
text that can be manipulated. A robust means of accomplishing this is to (1) transform any
3.4 Investigating Functions with Manipulate 85
variable quantity in the text to a String using ToString, and (2) join together the static and
variable text strings with StringExpression.
a. You can type ~~ between two text strings to sew them together into a single string. Techni-
cally, you are invoking the StringExpression command when you do this. Try it; type and
enter the following. We use FullForm so that the double quotes will display.
x
Out[15]=
The square root of 2 is 1.41421.
c. Create a Manipulate showing a Plot of the sine function, with a PlotLabel that indicates the
value of the function for any value of x between S and S. The user can control x with a slider.
5. The following input will create a useful interactive interface in which every available option for
the Plot command appears in a popup menu. Select an option in this menu, and the usage
message for that option is displayed. Try it (Courtesy of Lou D’Andria, Wolfram Research).
Manipulate$
ToExpression$SymbolName$option( aa "::usage"(,
option, Map$First, Options#Plot'( (
a. Modify the input above to create an option explorer for the Grid command, and use it to get
information on the ItemSize option.
b. See if you can figure out how this Manipulate works. This will entail finding information in
the Documentation Center on the commands Map, First, Options, SymbolName, ToExpresg
sion, and StringExpression (~~). Note that by replacing Plot (in the last line) by any other
command that accepts options, an option explorer for that command can be generated.
6. Use TabView with two tabs to produce the output below. You’ll have to find the answer to the
riddle on your own, or look at the solution. (Riddle by Alexandra Torrence, age 10.)
Riddle Answer
I'm a wave that does not move, and to you I want to prove, that
if you knock I'm hard as rock and if you kick me I'll stick to your sock.
86 Functions and Their Graphs
7. This exercise discusses the use of gamepad controllers to operate a Manipulate output. If you
have a gamepad for your computer the first thing to do is to plug it in, select the cell bracket for
a Manipulate and try it. In most cases it’s plug and play. To bind a particular Manipulate local
variable to a given controller axis, replace the iterator {var, spec} for that variable with axisName
{var,spec}, where axisName is the string name of the given controller axis. Typical axis names are
given below. Create a Manipulate where you bind a specific local variable to a specific controller
axis.
In[1]:= f#xB' : x2
Out[2]=
first describes the contents of each table entry, while the second (in this case x, 1, 10) is an itera-
Like Plot and Manipulate, the Table command takes two arguments, separated by a comma. The
tor. Unlike Plot and Manipulate, however, the values of the variable will increment by 1 (by
default) in a Table. As with Manipulate, a fourth number can be added to the iterator to specify the
step size.
Out[3]=
You can also shorten the iterator to contain only two items—the name of the variable and a stop-
until the stopping number is reached. So for instance, using the iterator x, 10 is the same as using
ping number. When you do this, Mathematica starts at 1 and increments the variable in steps of 1
The output of the Table command is a basic data structure in Mathematica called a list. A list is
comprised of an opening curly bracket, individual items (such as numbers) separated by commas,
and a closing curly bracket.
Table will also accept a special iterator structure of the form {var,{value1 ,value2 ,…}}. In this case the
3.5 Producing a Table of Values 87
Out[5]=
One of the most useful applications of the Table command is producing something that actually
looks like a table. We accomplish this by constructing a Table where the first argument is itself a
list. The result is a list of lists. We then apply Grid to the result in order to create a two-dimensional
display in which each inner list becomes a row. Here’s an example where both the input value x and
the output value f +x/ for a function are given in each row:
Out[6]= 1, 1, 2, 4, 3, 9, 4, 16, 5, 25
In[7]:= Grid#data'
1 1
2 4
Out[7]= 3 9
4 16
5 25
Grid will display any list of lists in a two-dimensional format like this; each sublist appears as a
separate row. Numerous options are available that allow all manner of presentation possibilities. But
perhaps the most simple formatting tip is to apply Text to an entire grid. This will apply textual
formatting to the individual items (numbers in this case) that occupy each grid cell. Here we use
prefix form (@, see Section 2.8 on page 37) instead of square brackets when applying the Text
command, and add the Grid option setting Alignment Right to align each column to the right.
1 1
2 4
Out[8]= 3 9
4 16
5 25
Another simple but valuable technique is to add headings to the columns of a table by prepending an
additional row containing these headings to your table data. Typically each item in the header row
is a string; this is accomplished by enclosing each item in double quotes.
Out[9]= x, x2 , 1, 1, 2, 4, 3, 9, 4, 16, 5, 25
88 Functions and Their Graphs
x x2
1 1
2 4
Out[10]=
3 9
4 16
5 25
The Spacings option can be used to add a bit of space between successive columns. The Dividers
option is used above to add dividing lines in a Grid. The setting is of the form {vertical
or far right, only between the columns. The False, True specifies the horizontal dividing lines:
dividers,horizontal dividers}. The Center setting specifies that there are no vertical lines on the far left
there is no line above the first row, while there is one above the second row, and none for any
subsequent rows. The following syntax may also be used for Dividers. It can be handy in cases like
this where few dividers are required. It simply specifies that only the second vertical divider and the
second horizontal divider will be rendered, and no others.
x x2
1 1
2 4
Out[11]=
3 9
4 16
5 25
With these tools in hand, you can create tables to your heart’s content. Here we use powers of ten as
the values of the function variable, and the simple Dividers All setting to put in all possible row
and column dividers:
Clear#data';
Table$10n , f$10n ( , n, 0, 5(
In[12]:=
data
x x2
1 1
10 100
Out[14]= 100 10 000
1000 1 000 000
10 000 100 000 000
100 000 10 000 000 000
Clear#data';
Table$x, x2 , 2x , x, 10(
In[15]:=
data
1, 1, 2, 2, 4, 4, 3, 9, 8, 4, 16, 16, 5, 25, 32,
6, 36, 64, 7, 49, 128, 8, 64, 256, 9, 81, 512, 10, 100, 1024
Out[16]=
x x2 2x
1 1 2
2 4 4
3 9 8
4 16 16
Out[17]= 5 25 32
6 36 64
7 49 128
8 64 256
9 81 512
10 100 1024
Manipulating a Grid
Here is a grid with a header row, and a second row of content. The values in this second row can be
manipulated. This gives a compact table that allows one to display the row of his or her choosing:
Manipulate$
Text Grid$"x", "x2 " , x, x2 , Dividers All, ItemSize 5(, x, 5.3, 1, 10, .1(
In[18]:=
Out[18]=
x x2
5.3 28.09
Manipulate#Text Grid#"C", "F", c, 1.8c 32, Dividers All, ItemSize 5',
c, 0, 40, 100, 1'
In[19]:=
Out[19]=
C F
0 32
The two examples above make use of the ItemSize option to the Grid command. When set to a
single numerical value (as we did here) it specifies the width of each cell in the grid in ems (the
width of the letter m). Other common settings for this option include All (which specifies that all
cells have identical width and height values, determined by the content of the largest cells), or a list
of two numerical values such as {5, 2} (which specifies the width of each cell in ems and the height
of each cell in line heights, respectively). When manipulating a grid, it is a good idea to set ItemSize
to a specific numerical value (or to a list of two such values) in order to keep the table dimensions
steady as the controller is adjusted.
Exercises 3.5
1. The Partition command is used to break a single list into sublists of equal length. It is useful for
breaking up a list into rows for display within a Grid.
a. Enter the following inputs and discuss the outputs.
Range#100'
3.5 Producing a Table of Values 91
Partition$Range#100', 10(
b. Format a table of the first 100 integers, with twenty digits per row. The first two rows, for
example, should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
c. Make the same table as above, but use only the Table and Range commands. Do not use
Partition.
d. Make the same table as above, but use only the Table command (twice). Do not use Partition
or Range.
2. The Style command is used to apply a particular style to an expression.
a. Enter the following inputs and discuss the outputs.
Style#4, Red'
Style#4, 72'
Style#4, "Section"'
c. Alternately, one can apply style elements to an entire grid by selecting the cell bracket of the
cell containing the grid, and visiting the Format menu. For instance, Format # Text Color # Blue
will make all the text blue. Reproduce the Grid above, this time using the menu items to
change the style.
3. A statement that is either true or false is called a predicate; in Mathematica a predicate is any
expression that evaluates to True or False. In this exercise you will learn how to use predicates to
apply Styles selectively.
a. There are many built-in predicate commands. Most end in the letter Q (for “Query”). Enter
the following inputs and discuss the outputs.
? PrimeQ
?
Q
b. The If command is used to generate one output if a specified condition (i.e., a predicate) is
true, and another if that condition is false. The predicate is the first argument to If. The next
92 Functions and Their Graphs
argument is what is returned if the predicate is true (If is discussed in Section 8.5). A third
argument specifies the expression to be returned if the predicate is false. Enter the following
input and discuss the output.
13 23 33 43 53 63 73 83 93 103 113 123 133 143 153 163 173 183 193 203
b. Make a table of values for x 1, 2, … ,10 for the function
f +x/
x x x x x x x x x x x
1 2x 3x 4x 5x 6x 7x 8x 9x 10 11 12 13 14 15 16 17 18 19 20
c. Plot f +x/ on the domain 1 x 10.
5. Comments can be inserted directly into your input code. Any text placed between the (* and *)
tokens will be ignored by the kernel when an input is entered. Comments do not affect the
manner in which your code is executed, but they can be helpful to you or someone else who has
to read and understand the code later. Look at the solution to the next exercise to see an example
in which comments are used to help a reader find each of four items in a somewhat complex two-
by-two Grid.
6. Use a two-by-two Grid within Manipulate to create the interface below for zooming in on a
graph of the sine function. The “Center” controller corresponds to a variable named x0, and the
“Zoom Level” controller corresponds to a variable named G. The iterator for the lower Plot is of
the form {x0-G,x0+G}.
3.5 Producing a Table of Values 93
Center
Zoom Level
Full View
S S
S S
2 2
1
0.85
0.80
0.75
0.70
Zoom View
0.65
0.60
0.55
7. In this exercise you will explore the syntax for applying options to a Grid. Mastery of this syntax
will allow you to construct stunningly beautiful tables. There are two common syntax forms that
work for several options. To illustrate the possibilities we use the Dividers option, which speci-
fies the placement and style of vertical and horizontal dividing lines in a Grid. First enter the
input below to generate a 10 10 table of invisible data (each entry is simply a string comprised
of a single space character). Note: The Partition command is discussed in Exercise 1.
d. Take your last input and add the following option setting, then explain the output.
e. Other options that utilize these syntactical conventions are Alignment , Spacings, ItemSize,
and ItemStyle. Some simple but useful Alignment settings to try are Alignment Right or
Alignment"." (to align numbers at the decimal point). Produce the following Grid using the
options mentioned. The Helvetica font is used for the entries in the first column, while the
default text font is used in the second. Once you can do this, you will be equipped to produce
a rich assortment of useful tables.
5
10 0.00001
4
10 0.0001
3
10 0.001
2
10 0.01
1
10 0.1
0
10 1
1
10 10.
2
10 100.
3
10 1000.
4
10 10 000.
5
10 100 000.
called piecewise defined functions. For instance a function may be defined by the rule f +x/ x when x
Certain functions are defined by different rules over various disjoint pieces of their domain, so-
is between zero and one, inclusive; by the rule f +x/ x when x is strictly between negative one and
zero; and by f +x/ 1 for all other values of x. In standard mathematical notation we write:
3.6 Working with Piecewise Defined Functions 95
x 0x1
f +x/ x 1 x 0
1 otherwise
Here “otherwise” means that either x ! 1 or x 1. How can this be conveyed to Mathematica? It is a
simple matter to enter a piecewise function directly from the keyboard in standard notation. To do
so, first type f[x_]:=, then create the single bracket by typing ÈpwÈ, and finally produce a grid to
the right of the bracket by typing ÅÎ , Ï. If more than two rows are needed, type ÅÎ ¢ Ï. Each time
you hit ÅÎ ¢ Ï you will add one additional row. Now move the cursor to the first placeholder and
type in a function expression, then use the Í key to move to the adjacent placeholder and type a
logical expression. This is typically an inequality such as 0 x 1, but in all cases is an expression
that evaluates to either True or False when x is a specific real number. Fill in the remaining pairs of
placeholders; the first in each pair holds a function expression, the second a logical expression. The
following example shows how one would enter the function above. Note that the logical expression
in the final row can simply contain the expression True, which conveys the meaning that this rule
is applied to all values of x for which the logical expressions in earlier rows are False; that is, it
behaves like the word “otherwise” in the example above. When you’re finished typing, enter the cell.
x 0x1
In[1]:= f#xB' : x 1 x 0
1 True
Once entered, this function behaves like any other. You may Plot it, Manipulate it, apply to it any
transformations that you might apply to any other function. In short, it behaves exactly as it
should. For instance:
0.8
0.6
Out[2]=
0.4
0.2
2 1 1 2
The underlying Mathematica command that is being utilized to create the function above is called
Piecewise. In most cases it is easiest to use the syntax above, which has the effect of calling the
Piecewise command. Equivalently, one can use Piecewise directly; the following example shows
how to enter the function above using this syntax:
x2 +2 x 1/ «« +1 x 2/
In[4]:= g#xB' : 1 1 x 1
4 True
Here is an equivalent formulation, using the absolute value function:
x2 1 Abs#x' 2
In[5]:= g#xB' : 1 Abs#x' 1
4 True
3
Out[6]=
2
3 2 1 0 1 2 3
Piecewise functions provide a rich setting in which to explore discontinuous functions. Plot is aware
of discontinuities appearing at the boundary between regions, and excludes such points. This leads
to accurate plotting of such discontinuous functions.
0.5
Out[7]=
3 2 1 1 2 3
0.5
1.0
0.5
Out[8]=
3 2 1 1 2 3
0.5
1.0
Exercises 3.6
1. Show the second condition in the last example above could just as well be True.
2. Make a plot of the piecewise function below, and comment on its shape.
0 x0
x2
0x1
f +x/
2
3
x2 3x 1x2
2
1
+3 x/2 2x3
2
0 3x
3. A step function assumes a constant value between consecutive integers n and n 1. Make a plot of
the step function f +x/ whose value is n2 when n x n 1. Use the domain 0 x 20.
0.5
0.0
Out[1]=
0.5
1.0
1.0 0.5 0.0 0.5 1.0
By default, a ContourPlot will display with a frame and no coordinate axes, but it is a simple matter
to change this behavior.
In[2]:= ContourPlot$x2 y2 m 1, x, 1, 1, y, 1, 1 , Frame False, Axes True(
1.0
0.5
Out[2]=
1.0 0.5 0.5 1.0
0.5
1.0
Note that by default the AspectRatio of a ContourPlot will be set to 1, meaning that the coordinate
axes will be scaled as necessary to produce a perfectly square plot. Such a plot can be misleading; for
instance, the ellipse below looks like a circle!
3.7 Plotting Implicitly Defined Functions 99
0.4
0.2
0.0
Out[3]=
0.2
0.4
Set the AspectRatio to Automatic to give your axes a uniform scale. We do not recommend this as
a default setting, however, as it is all too easy to ask for a plot that is thousands of times higher than
it is wide. But in cases such as the ellipse above where a common scaling of axes is called for, this
setting is important.
In[4]:= ContourPlot$x2 4y2 m 1, x, 1, 1, y, .5, .5 , AspectRatio Automatic(
0.4
0.2
0.0
Out[4]=
0.2
0.4
ContourPlot works in a fundamentally different way than Plot does, as there is no explicit expres-
sion to evaluate for each numerical value of x. Rather, it samples points in the rectangular region
specified by the two iterators, and recursively applies an adaptive algorithm in an attempt to find a
smooth curve (or curves) satisfying the given equation. It is possible that in some cases the default
parameters governing the algorithm are insufficient to produce an accurate plot. For example, note
the jagged appearance in some parts of the output below:
100 Functions and Their Graphs
0.5
0.0
Out[5]=
0.5
1.0
10 5 0 5 10
To cure a case of the “jaggies,” try setting the PlotPoints option to a large value such as 25, 50, or
100. PlotPoints controls how many points are initially sampled in the domain. Larger values tend to
produce more accurate plots but may lead to significantly slower evaluation time, so use the lowest
setting that produces a satisfactory plot.
In[6]:= ContourPlot$Sin$x2 ( y2 m Cos$x
y(, x, 10, 10, y, 1, 1 , PlotPoints 100(
1.0
0.5
0.0
Out[6]=
0.5
1.0
10 5 0 5 10
Several implicitly defined functions can be simultaneously displayed by providing a list of equations
as the first argument to ContourPlot. Mousing over a curve on the plot yields a tooltip displaying
the equation corresponding to that curve, so it is easy to interpret the output when multiple equa-
tions are plotted.
3.7 Plotting Implicitly Defined Functions 101
0.5
0.0
Out[7]=
0.5
1.0
1.0 0.5 0.0 0.5 1.0
As with the Plot command, the option setting Mesh Full will reveal which points are sampled
initially, while the setting Mesh All will reveal the final points used to construct the curves after
the algorithm has run. The following Manipulate is a useful aid for understanding how the options
PlotPoints and MaxRecursion work in a ContourPlot. When MaxRecursion is set to 0, no itera-
tions take place and the initial and final meshes are the same. We saw a similar example for Plot in
Section 3.4 on page 79.
myPlot Plot#2 x, x, 2, 2'. A colon-equal sign := is used to make a delayed
single equal sign = is used to assign a name to an expression, such as a 3 or
Manipulate$
ContourPlot$ 2x2 y2 m 1, x, 2, 2, y, 2, 2 ,
In[8]:=
plotPoints 2 3 4 8
maxRecursion 0 1 2 3
Out[8]=
Exercises 3.7
1. The option ContourStyle (not PlotStyle) is used to change the style of a ContourPlot. Plot the
implicit function x2 sin,x y0 m 3 as a thick, blue, dotted line.
f ,x, y0 m z1 , f ,x, y0 m z2 , f ,x, y0 m z3 , and so on, where z1 , z2 , etc... are constants, the following
2. If you ever wish to simultaneously view contour plots of implicitly defined functions of the form
syntax will work. Suppose, for instance, f ,x, y0 x2 y2 , and the z-values are 2, 1, 0, 1, and 2.
Enter the following input to see overlaid plots of x2 y2 2, x2 y2 1, x2 y2 0, x2 y2 1,
and x y
2 2
2.
Superimposing Plots
It is often desirable to view two or more plots together. If you simply want to plot several functions
on the same set of axes, enter a list containing these functions as the first argument to the Plot
command and you’ll have it:
1.5
1.0
Out[4]=
0.5
On your monitor the three functions are given three distinct colors. To better distinguish between
them, one may wrap the list of functions with the Tooltip command. When you mouseover any
curve in the resulting plot, a tooltip will pop up displaying that function’s expression. Note that the
output in printed form is indistinguishable from the prior output, so this feature is only useful in a
live session.
1.5
1.0
Out[5]=
0.5
One may also use the PlotStyle option to change the appearance of the three functions. This is
sometimes useful for printed output when using a black and white printer. Just set PlotStyle to a list
of three directives. These will be applied to the functions (in the order listed).
In[6]:= Plot$f#x', g#x', f#x'
g#x' , x, 1, 1, PlotStyle Gray, Black, Dashing#.01' (
2.0
1.5
1.0
Out[6]=
0.5
Finally, with a little extra work you can add a legend to the plot that will explain to the reader
which function is which. You must first load the PlotLegends package. Be sure to type the double
quotes and the backquote character.
In[7]:= Needs$"PlotLegends`"(
Plot$f#x', g#x', f#x'
g#x' , x, 1, 1, PlotStyle Gray, Black, Dashing#.01' ,
PlotLegend f#x', g#x', f#x'
g#x' , LegendPosition 1, .5(
In[8]:=
2.0
1.5
1.0
Out[8]=
1x
0.5 x2
+1 x/ x2
The option PlotLegend is set to the list of labels to be placed in the legend box. In this case we just
used the functions themselves, but textual expressions or strings (expressions enclosed in double
quotes) are also fine. The LegendPosition option specifies where the legend box is placed relative to
the plot. To be more precise, it specifies where the midpoint of the left side of the legend box is
placed. You will almost certainly want to change its default placement. To do this, set LegendPosig
tion to a coordinate pair where each coordinate ranges from 1 to 1. The setting {-1, -1} places the
legend in the lower left corner, while {1,1} places it upper right.
Alternatively, it is a simple matter to build your own plot legend from scratch using the drawing
tools (discussed in Section 3.9 on page 112); simply plot your functions, then click on the output
image and use the drawing tools to place on it (for instance) some text and some lines to which the
3.8 Combining Graphics 105
same directives used in the plot are applied. Finally, place a rectangle with opacity and a thick black
edge on top of your text. For instance, here’s a plot:
3.0
2.5
2.0
1.5
Out[9]=
1.0
0.5
If you need to superimpose a large number of plots, you can use Mathematica’s Table command to
generate the list of functions:
In[10]:= Plot$Table$n x2 , n, 40, 40(, x, 2, 2, PlotRange 50(
40
20
Out[10]=
2 1 1 2
20
40
¿ In cases like this where the expression appearing as the first argument to Plot is generated
programmatically, it may be beneficial to wrap the expression with Evaluate. The necessity of
the Evaluate command is a subtle business. Generally, Plot will hold the expression appearing
as the first argument unevaluated, then evaluate it multiple times, once for each numerical
value of x sampled in the domain. Evaluate forces Plot to first evaluate its initial argument
before plugging in any values of x. In some settings this can lead to a plot that works versus
one that does not. In other cases, Evaluate can reduce the time it takes to produce the plot. In
106 Functions and Their Graphs
the example above, the processing time is reduced (and curves become individually colored), if
one replaces Table$n x2 , n, 40, 40( by Evaluate$Table$n x2 , n, 40, 40((.
Out[11]=
And one can shade the region between two curves like so:
Out[12]=
When there are more than two functions there are many ways to shade the various regions between
them. Below, filling is added from the first function to the third, and from the second function to
the top of the plot. Note that the filling is transparent, so the two filling styles can be layered one
over the other. Look up Filling in the Documentation Center for more information.
In[13]:= Plot$x2 , x4 , Sin#20x' , x, 0, 1.5, PlotRange 0, 1.5, Filling 1 3, 2 Top (
Out[13]=
3.8 Combining Graphics 107
Superimposing Graphics
To overlay one graphic on top of another, simply feed the component images to the Show
command. The individual images will be superimposed upon a common coordinate system. Below
we demonstrate this by assigning names to the component images and suppressing their individual
output with semicolons.
The plot domain, and option settings, such as AspectRatio, Axes, and so on will be inherited from
their settings in the first image listed within Show. Changing the order of the graphics listed within
Show may therefore change the appearance of the output:
0.5
0.0
Out[17]=
0.5
1.0
0.0 0.5 1.0 1.5 2.0 2.5 3.0
Note also that the order of the component images listed within Show is the order in which they are
rendered. The first graphic is rendered first, with the next graphic overlaid on top of it, and so on.
108 Functions and Their Graphs
In[18]:= ellipse
0.5
0.0
Out[20]=
0.5
1.0
2 1 0 1 2
0.5
Out[21]=
2 1 1 2
0.5
1.0
One may also include within Show any options accepted by Graphics. Such options can be used to
override settings inherited from the component images.
Out[22]=
Keep in mind also that while Show is an extremely useful and versatile command, it is often not
needed. To plot two functions together, for instance, recall that one can simply provide a list of the
two functions as the first argument to Plot. One may also use the Epilog option in commands such
3.8 Combining Graphics 109
as Plot and ContourPlot to overlay primitive graphic elements on a plot (the Epilog option is
discussed in Section 3.9, on page 117).
Graphics Side-by-Side
A simple but rather primitive means of arranging graphics side-by-side is to simply create a list of
graphics. Of course, the curly brackets enclosing the list will be displayed in the output, and there
will be commas separating the images:
In[23]:= Plot#Sin#x' Cos#x', x, S, S', Plot#Sin#x' Cos#x', x, S, S'
1.0 1.0
!
0.5 0.5
Out[23]= ,
3 2 1 1 2 3 3 2 1 1 2 3
0.5 0.5
1.0 1.0
A better way to accomplish a side-by-side display is to use GraphicsRow. Its argument is a list of
graphics. It will integrate this list of individual Graphics objects into a single conglomerate graphic
that can, for instance, be moved or resized as a whole.
In[24]:= GraphicsRow#'
1.0 1.0
0.5 0.5
Out[24]=
3 2 1 1 2 3 3 2 1 1 2 3
0.5 0.5
1.0 1.0
The Frame, FrameStyle, and Dividers options may be used to add frames around each item, or to
place divider lines between some of them. The syntax for these options works as it does in a Grid.
The list of graphics can be generated programmatically, using Table for instance:
Out[25]=
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
0.5 0.5 0.5
Graphics in a Grid
There is also a GraphicsGrid command to lay out graphics in a grid pattern. The syntax and many
of the options are the same as for Grid.
GraphicsGrid$
Table#Plot#Csc#m
x', x, 0, 2S, Axes False', m, 5',
In[26]:=
Out[26]=
One could also use Grid instead of GraphicsRow or GraphicsGrid. The main difference is that the
output of these latter commands is a single graphic that may be edited as such, for instance using the
drawing tools. The entire output can be resized by selecting it and dragging a handle. In a plain
Grid, only the individual component graphics can be edited.
Exercises 3.8
1. Name at least three strategies for determining which function is which in the graph below. You
may alter the input and re-enter it.
0.8
0.6
Out[27]=
0.4
0.2
sin+.4t/sin+1.6t/
2. In this exercise you will examine the function and the function cos+.6t/.
2sin+t/
a. Simultaneously plot both functions on the domain 0 t 8S and describe what you find.
sin+.3t/sin+1.7t/
b. Repeat for the functions and cos+.7t/. What do you find?
2sin+t/
sin+.2t/sin+1.8t/
c. How about for the functions and cos+.8t/?
2sin+t/
3.8 Combining Graphics 111
sin+k t/sin++2k/ t/
d. Make a conjecture as to the value of for any real numbers k and t , where t is not
2sin+t/
an integer multiple of S.
e. Enter the following, which illustrates the equivalence and allows the viewer to control k.
Comment on the graphical implications of the fact that the third function is the sum of the
other two. Note that in Exercise 2 in Section 4.6 we will return to this example and show why
the equivalence holds.
In[28]:= Manipulate%
Out[28]=
f +x/ p
xn , for real parameters p and n, with p positive and with domain 0 x 4. Include text
3. Make the following Grid showing the plots of power functions, i.e., functions of the form
next to each plot indicating the values for the parameter n that will produce plots of the same
general shape.
112 Functions and Their Graphs
n!1 n!1
n 1 n 1
0n1 0n1
n0 n0
There are two basic ways to add information to an existing graphic: Use drawing tools and your
mouse to interactively add the elements you desire, or use the Graphics command and primitive
graphics elements to proceed programmatically. Each method has its advantages, and we’ll address
each in turn.
Drawing Tools
Drawing tools are found in the Graphics menu. The idea is simple and intuitive: elements are added
to a graphic using the Drawing Tools and your mouse. This approach is appropriate when you are
making a single image, or perhaps just a few, and when the placement of the elements on the
graphic allows some leeway. It’s great for adding labels with arrows pointing to items in a plot, for
example.
1.5
1.0
0.5
Out[1]=
x
1.5 1.0 0.5 0.5 1.0 1.5
0.5
1.0
1.5
Now go to the Graphics menu and select Drawing Tools. Begin by clicking once on the graphic you
wish to modify; an orange border appears around it. Now the tools on the palette are bound to a
target image. Explore the palette by mousing over its buttons. As you do, a tooltip will give a brief
description of that tool’s function. Generally speaking, click a tool button once to use that tool
once, or double click it to keep it active. If you click only once, the Selection tool will become active
immediately afterward. This is a good way to work in many cases; you push a palette button to
activate a tool, use the tool to add an element to your graphic, then (without another trip to the
palette) you can select and move or resize the new element. For example, in the graphic above let’s
add labels for the two curves and an arrow pointing from each label to the appropriate curve. Click
on the graphic. Then push the arrow button on the palette (left column, half way down) to activate
the arrow tool (or just type the letter “a” after clicking on the graphic). Now position the cursor over
the graphic where you want the tail of the arrow to appear, and (left) click once. Holding the mouse
button down, drag the cursor to where the arrowhead should be, and release. The arrow appears,
with an orange bounding box around it. The palette has now resorted back to the default Selection
tool (the cursor button in the upper right is now highlighted, not the arrow button). If you drag the
edge of the orange box surrounding the arrow, you can move it. If you drag the handle by either its
head or tail, you will move only that end of the arrow while the other end remains anchored. You
really have to try this to get a feel for it. After you have finished, click outside the graphic. Note that
you can continue to make adjustments on your arrow at any time in the future. Click once on the
graphic to select it, then click again on any element to select that element. The orange bounding
box appears, allowing you to move or resize it. Alternately, if you double click on an element, you
can edit it. Finally, if you push the Inspector button at the bottom of the Drawing Tools palette, a
second palette appears. Use this to fine-tune the appearance of any graphic element. Select any
item, such as an arrow, and you can adjust its thickness, color, opacity (how transparent it is when
overlaid on another element), and so on. With arrows you can easily adjust the size, shape, and
position of the arrowheads.
The best way to learn about the drawing tools is simply to use them. You can create a new (empty)
graphic by pushing the button in the upper left corner of the Drawing Tools palette, or by choosing
New Graphic in the Graphics menu. Then play to your heart’s content with the tools. Below we show
a few simple labels added to our previously generated plot:
114 Functions and Their Graphs
1.0
0.5 sin+x/
x
1.5 1.0 0.5 0.5 1.0 1.5
0.5
1.0
1.5
The Drawing Tools are a simple and powerful set of tools for creating all manner of creative and
revealing information graphics. With no training whatever and in a matter of minutes, our 13 year
old son Robert added the archer to the plot below:
50
40
30
20
10
0
0 50 100 150 200 250 300
Detailed information on each drawing tool can be had in the Documentation Center. Type “Editing
Mathematica Graphics” in the text field and follow the link to the Mathematica overview of that
name. Some of the most commonly used keyboard equivalents for the drawing tools are given in
Table 3.3.
Graphics Primitives
It is, of course, possible to forgo the freehand palette approach and work programmatically instead.
This method is painstaking when you want to add a simple label with an arrow, as above, but it is
absolutely essential when you have to add many elements, and at precise locations. We ask the
reader to be patient here; this section will introduce ideas that take some practice and perseverance
to master. The long term benefit, however, will be substantial.
3.9 Enhancing Your Graphics 115
s activate the Line Segment tool make any segment+s/ horizontal or vertical
Let’s first meet the graphics primitives. These are the building blocks from which all two-dimensional
Mathematica graphics are constructed. They are: Point, Line, Rectangle, Polygon, Circle, Disk,
Raster, and Text. Let’s look at a few of these on their own. Later, we’ll show how to combine them
into a single graphic. We note that three-dimensional versions of some of these primitives (and
some new ones) exist as well; these will be discussed in Section 6.2 on page 276.
The most common elements you will use are points and lines. We will illustrate the ideas involved
ments joining the points +0, 0/, +1, 1/, and +2, 0/. To join any finite collection of points in the plane,
by drawing lines; the other primitives work in a similar manner. Let’s first construct the line seg-
Individual points, such as +2, 0/, are input as lists of length two, like this: {2,0}.
feed a list of the Cartesian coordinates of the points as the sole argument to the Line command.
116 Functions and Their Graphs
Out[2]=
Not too interesting yet. To view any primitive graphics element, wrap it in the Graphics command:
Out[3]=
The visual appearance of any primitive object or objects can be tweaked using graphics directives
(these were introduced in Section 3.3, in the subsection How to Add Color and Other Style Changes:
Graphics Directives on page 63). Some commonly used directives are Red, Thick, Opacity#.5', and
Dashed. The syntax works like this: put the graphics primitive(s) in a list whose first item is the
directive. If there is more than one directive, wrap them in the Directive command:
Out[4]=
Note that Graphics will accept many of the same options discussed for the Plot command:
In[5]:= Graphics#Directive#Thick, Dashed', Line#0, 0, 1, 1, 2, 0', Axes True'
1.0
0.8
0.6
Out[5]= 0.4
0.2
And combining primitive elements is as simple as putting them all into one big list within the
Graphics command:
3.9 Enhancing Your Graphics 117
Graphics$
Directive$Thick, Gray(, Line#Table#x, Sin#x', x, 0, 6.3, .1'' ,
In[6]:=
0.5
Out[6]=
1 2 3 4 5 6
0.5
1.0
Note that the order in which the individual elements are specified matters. The first item is rendered
first, and each additional item is placed “on top” of earlier items. The point above, for instance,
would be obscured by the thick, gray sine curve had it been listed first.
Finally, it may be the case that you wish to include some primitive elements with the output of, say,
the Plot command. There are a few ways to do this. One is to take advantage of the Epilog option in
the Plot command. Set this option to any list of primitives that you could feed to the Graphics
command. The effect is to overlay the primitives on top of the plot.
(
1.0
0.5
Out[7]=
1 2 3 4 5 6
0.5
1.0
And now, at last, we demonstrate the true benefit of understanding Graphics primitives. Below we
combine a (static) plot of the sine function with a dynamically controlled point that the user can
adjust with Manipulate:
118 Functions and Their Graphs
1
3
Out[8]=
S S S 2S 5S 7S 4S 3S 5S 11 S
S 2S
6 3 2 3 6 6 3 2 3 6
1
2
3
2
1
One final word is in order that pertains to printing. If you would like to produce a quality print of
that beautiful graphic you spent hours getting just right, wouldn’t it be nice to lose the cell label
Out[117]= that appears to its left? There are two simple means of achieving this. First, you can high-
light the cell label and hit the delete key. Mission accomplished. Alternately, wrap your input with
the Print command, and the same output will appear but without the label. Note that Print will not
send your output to a printer; rather, it “prints” an unlabeled cell in your notebook.
3 2 1 1 2 3
1
2
The same comment applies to that wonderful table you produced with Grid. One may use
Print$Style#Grid#', "Text"'( to generate a table with textual styling in an unlabeled cell, suitable
for inclusion in the finest of publications.
3.9 Enhancing Your Graphics 119
Exercises 3.9
1. Make the following figure using the commands Graphics, Rectangle, and Circle, and including
the Graphics option setting Frame True. You will want to look up Circle in the Documenta-
tion Center to find out how to draw an ellipse. Do not use the Drawing Tools palette.
2
1
2
4 2 0 2 4
2. In this exercise you will explore various graphics directives using Manipulate.
a. The following command will produce a red disk of radius 1 centered at the origin. Type and
enter it:
Graphics#Red, Disk#''
b. Replace Red by Lighter[Blend[{Blue, Red}, .3], .4].
c. Finally, make this into a Manipulate, replacing .3 and .4 above by the control variables r and
d. Make two disks of radius 1 centered at +0, 0/ and +1, 0/ with the commands Disk#' and
s, respectively. Investigate the effects.
Disk[{1,0}, 1], respectively. Make the first disk blue. Place them in a Manipulate with a single
control variable that determines the Opacity of the second disk (with values that range from 0
to 1).
e. Repeat the previous part, but make the second disk (the one with varying opacity) orange. If
you are not a University of Virginia fan, feel free to use other colors.
3. Make a smiley face as follows:
a. Create a Manipulate using Circle#0, 0, 1, r, S, 2 S' for 0.01 r 1 and note the behav-
ior.
b. Using Graphics primitives such as Disk, create a yellow smiley face that can be manipulated.
c. Add eyebrows that can be manipulated.
and with k assuming integer values from 1 to 20. Note: A forthcoming paper by undergraduate
tive of f +z/ +z r/n ,z2 10 will either be real or will lie on the kth ellipse in this family (where
Liza Lawson and Bruce Torrence shows that for any real number r , the roots of the kth deriva-
0 149.5
2 141.7
4 134.7
6 128.3
8 122.6
10 117.4
12 112.7
14 108.5
In[1]:= data ;
16 104.7
18 101.3
20 98.2
22 95.4
24 92.9
26 90.5
28 88.5
30 86.6
When recording data a spreadsheet-type interface is desirable. To enter these data into Mathematica,
first type data = (any name will do, but “data” seems convenient), then select Table/Matrix # New...
from the Insert menu. A dialog box will appear. In the top left portion select Table. To the right
specify the number of rows (in this case 16) and columns (in this case 2). It is possible to add and
delete more rows and columns later, so these numbers need not be exact. Ignore the remaining
settings and hit the OK button. A rectangular array, a sort of mini-spreadsheet of the dimensions
you specified, will appear in your notebook, and a very long, vertical, blinking cursor will appear to
its right. Type ; so that when you eventually enter this cell the output will be suppressed. Now click
on the placeholder in the upper left corner of your table (or hit the Í key to jump there) and enter
the first data value. When you are finished use the Í key to move to the next placeholder. Con-
tinue to enter your data in this fashion. Additional rows or columns can be added at any time; just
look in the Insert # Table/Matrix menu for Add Row or Add Column. When all the data have been
typed in, enter the cell.
When you enter data in this way, Mathematica stores it as a list of ordered pairs (one pair for each
row in the table). Technically, it’s a list of lists. If you don’t put a semicolon after your data table,
3.10 Working with Data 121
you will see it displayed in this form upon entering it. Or you will see it in this form if you ask for it
by name:
data
0, 149.5, 2, 141.7, 4, 134.7, 6, 128.3, 8, 122.6, 10, 117.4, 12, 112.7, 14, 108.5,
In[2]:=
16, 104.7, 18, 101.3, 20, 98.2, 22, 95.4, 24, 92.9, 26, 90.5, 28, 88.5, 30, 86.6
Out[2]=
You won’t need to work with the data in this form, but it’s good to see it once so you know how
Mathematica interprets it.
In[3]:= ListPlot#data'
150
140
130
120
Out[3]=
110
100
5 10 15 20 25 30
ListPlot takes a single argument: a list of two-tuples. Each two-tuple is interpreted as a point in the
coordinate plane, and these points are then plotted. If your list of points is very short, you may find
it easiest to type it directly into ListPlot rather than first making a data table:
2.5
2.0
Out[4]=
1.5
coordinate axes. In the plot above, the point +1, 1/ is at the intersection of the two axes! One way to
It is an annoying fact of life that ListPlot will often hide one or more of your points behind the
alleviate this masking effect is to “connect the dots.” The option Joined True accomplishes this.
Also, one may specify another symbol to indicate the data points using the PlotMarkers option.
Here we used the ¼ symbol (from the Shapes and Icons portion of the SpecialCharacters palette).
122 Functions and Their Graphs
ListPlot$1, 1, 2, 3, 3, 2, 4, 3, Joined True,
PlotMarkers ¼, AxesOrigin 0, 0, PlotRange 0, 3.5(
In[5]:=
3.5
3.0 ¼ ¼
2.5
2.0 ¼
Out[5]= 1.5
1.0 ¼
0.5
0 1 2 3 4
But ListPlot also accepts most of the options that the Plot command does, so it is a simple matter to
produce as elaborate a graph as you desire. Here we assign the name scatterplot to our plot so that we
can refer to it later. The x and y values in AxesOrigin were chosen just a bit smaller than the small-
est x and y values appearing in the data; this pulls the axes off any data points. Finally, wrapping
Tooltip around the data itself has the convenient effect of producing a tooltip showing a data
point’s exact coordinates when you mouseover that point (you’ll have to try this to experience it).
Out[6]=
You can have Mathematica find the best-fitting polynomial for your data (according to the criteria of
least squares) using the Fit command. Here is the best fitting linear function for the coffee cooling
data. We assign it the name fitLine:
The Fit command takes three arguments. The first is the data (a list of two-tuples). The second is a
list of the terms requiring coefficient values in the polynomial. The last is the name of the variable,
3.10 Working with Data 123
in this case x.
Once we have named these best-fit functions, we can view them against our data. A quick and dirty
way to display the Plot of a function together with data points is as follows (the Epilog option is
discussed in Section 3.9 on page 117):
140
130
120
Out[9]=
110
100
5 10 15 20 25 30
A more nuanced image can be had by using Plot and ListPlot to generate separate graphics, and
then using Show to display them together:
Out[10]=
Out[11]=
124 Functions and Their Graphs
Out[12]=
A more efficient means of entering the input above, where we have a list of nearly identical Show
items, is to use Table to generate the list:
GraphicsRow$
Table$Show$scatterplot, Plot#f, x, 0, 30', PlotLabel f(,
In[13]:=
Out[13]=
The FindFit command may be used in place of Fit when a more complex form of approximating
function is sought than a polynomial (or sum of basis functions). For instance, while the quadratic
above seems to fit the coffee cooling data rather well, a moment’s thought tells us that this function
will fare poorly if we use it to predict the coffee’s temperature, say, when the time x is equal to 60
minutes. For quadratics with a positive coefficient on the x2 term open upward, and so will eventu-
ally (for sufficiently large values of x) turn from decreasing to increasing functions. The coffee, on
the other hand, is not going to get warmer as time progresses. It is a well known principle of physics
the form of the cooling function ought to be f +x/ a b
cx , where a, b, and c are positive constants
that bodies cool exponentially toward the ambient temperature of the surrounding medium. Hence
FindFit requires four arguments. The first is the data. The second specifies the form of the fitting
expression (in this case a, b, c), and the last is the independent variable (in this case x). Any or all
function (in this case a b
cx ), the third is a list of the parameters whose values we seek in this
of the parameters in the third argument may be given as an ordered pair of the form {parameter,
guess}, where guess is a rough estimate of the correct value of that parameter. Below we use .5 as an
3.10 Working with Data 125
initial guess for the decay parameter c, since we know that c is between 0 and 1. This helps Mathemat-
ica refine its search for optimal values of the parameters in question.
The output of FindFit is a list of replacement rules giving the values of the parameters. Replacement
rules are discussed in Section 4.2 on page 153; for now we simply read off the values of the parame-
ters, and note that the fit is excellent:
Out[14]=
Moreover, this approach allows us to use the coffee data to determine that the ambient temperature
of the room is approximately a 69.35 Fahrenheit.
Exercises 3.10
1. For the data given below, find the best-fitting line (according to the criteria of least-squares), and
plot this line together with the data.
x 1 2 3 4 5
y 1.2 2.3 3.6 4.9 5.9
the best-fitting function of the form f +x/ p
xn for real parameters p and n.
2. For the same data used in the previous exercise, find the best-fitting power function. That is, find
3. For the functions in each of the previous two exercises, find the residuals. That is, for each x-
coordinate in the data, find the difference between the actual y value in the data and the value
predicted by the fit function. Geometrically, these residuals indicate the vertical distance
between a data point and the graph of the fit function. A residual is positive if and only if the
data point lies above the graph of the fit function.
4. Enter the following input to create a command that will plot a collection of data, a fit-function,
and the residuals for the data and the given function. Test the command on the data from the
126 Functions and Their Graphs
5. When a Manipulate has a Locator controller, it’s possible (and often desirable) to modify the
Locator’s default appearance in the image pane. A simple way to do this is to display a Graphics
object of your choosing at the position of the Locator, and add the option setting
Appearance None to the iterator for the Locator control. Enter the following input, explore
the output, and then change things so that the Locator appears as a Thick, Blue, Circle.
Manipulate$
Graphics$Directive#PointSize#.03', Red', Point$pt( , Axes True, PlotRange 1(,
pt, 0, 0 , Locator, Appearance None (
We mentioned in Section 3.5 that a list in Mathematica is a collection of items separated by commas
and enclosed in curly brackets, such as {2, 5, 9, 7, 4}. Our first task will be to master the art of extract-
ing one or more items from a list.
In[2]:= myList##5''
Out[2]= 32
If you type the name of a list followed by [[5]], you will extract the fifth part of the list. You can also
use the e3f7 button on the BasicMathInput palette (in the lower right portion of the top half of the
palette) to produce myList 357 , which has the same meaning. One may also type from the keyboard
È [[ È and È ]] È to produce the double square bracket symbols 3 and 7, which also have the
same meaning.
3.11 Managing Data — An Introduction to Lists 127
Out[3]= 32
In[4]:= myList357
Out[4]= 32
We’ll use this last notation throughout this section as it is easy to read, but remember that you may
simply use double square brackets (which are easier to type). A negative number inside the double
square brackets indicates an item’s position relative to the end of the list. For instance, here is the
second to last item:
In[5]:= myList327
Out[5]= 512
To extract a sequential portion of a longer list, one may indicate a Span of positions as follows:
In[6]:= myList31 ;; 47
The most commonly specified items in a list are the first and last. There are, for convenience, special
commands to extract these items (although myList317 and myList3-17 work just as well):
In[7]:= First$myList(
Out[7]= 2
In[8]:= Last$myList(
Out[8]= 1024
Most of Mathematica’s arithmetic operations have the Listable attribute. This means they will be
“mapped over lists.” In other words, each item in the list will be operated upon individually by these
commands, and the list of results will be displayed. This is extremely handy. For example:
1, 2, 3, 4 1
2, 3, 4, 5
In[9]:=
Out[9]=
In[10]:= 2 myList
Out[10]= 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048
¿ To find out if a command has the Listable attribute, type ?? followed by the command name,
128 Functions and Their Graphs
and evaluate the cell. All the attributes of the command will appear (along with a brief
description of the command and a list of its default option settings).
Recall that Mathematica stores a two-dimensional data table as a list of lists. That is, the data table is
stored as one long list, the members of which are the rows of the table. Each row of the table is in
turn stored as a list:
1 214
11 378
21 680
In[12]:= data
31 1215
41 2178
51 3907
Out[12]= 1, 214, 11, 378, 21, 680, 31, 1215, 41, 2178, 51, 3907
In[13]:= data337
In[14]:= data33, 27
Out[14]= 680
To extract an entire column of a two-dimensional table, use All in the first position within the
double bracket:
In[15]:= data3All, 27
The importance of these extraction commands manifests itself in situations that call for a transforma-
tion of the data. In most cases this will amount to performing some arithmetic operation on every
item in a column of your data table. For instance, one column of a table may comprise the x coordi-
nates of your data points, while another contains the corresponding y coordinates. You may want to
subtract 70 from all the x coordinates, or take the logarithm of all the y coordinates. How can this be
accomplished?
The simplest situation is one in which the same operation is to be applied to every member of a data
table. The listable attribute of most operations makes this a one-step process. For instance:
3.11 Managing Data — An Introduction to Lists 129
If you wish to operate on just one of the columns, things are almost as simple. Suppose, for
instance, that you want to take the logarithm of only the second column. One might proceed as
follows (where we make a duplicate copy of the original data, then overwrite the second column in
this copy):
Another method of accomplishing the same task invokes the useful Transpose command, which
switches rows and columns in a two-dimensional table.
1 Log#214'
11 Log#378'
21 Log#680'
Out[20]=
31 Log#1215'
41 Log#2178'
51 Log#3907'
This latter approach suggests a useful means of extracting a few columns from a larger table of data
and applying transformations to them selectively. Here, for example, is a somewhat random collec-
tion of data:
130 Functions and Their Graphs
And here is a new data table comprised only of the first column and the natural logarithm of the
third column:
1 0.677946
2 2.18079
3 0.584791
Out[24]=
4 2.19701
5 1.775
6 0.537204
So, for instance, one may now apply ListPlot or Fit to the newData, as discussed in the previous
section.
Exercises 3.11
1. Suppose that data is input as a Table with 120 rows and 6 columns.
a. What command could you use to extract only columns 2 and 6?
b. What command could you issue to extract only the last 119 rows of columns 2 and 6 (for
instance, imagine that the first row contains headings for the columns and not actual data)?
c. What command could you issue to extract only the last 119 rows of columns 2 and 6, and
then replace column 6 with the natural logarithm of its values?
3.12 Importing Data 131
An example is in order. The command CountryData is used to access data about countries, conti-
nents, and so forth. Like the other data commands, CountryData may be called with empty argu-
ment to produce a list of basic data objects. You will notice a slight delay before the output appears,
but this will only happen the first time a data command is evaluated in a session; this is when the
data is transferred from the central server to your computer.
In[1]:= Short$CountryData#', 3(
Out[2]= 237
Many of the data commands allow the single argument "Properties", which will list the properties
available for each of the countries (or for the primary data objects of the data command you are
using). At the time of this writing, there are 225 properties available for the country data:
In[3]:= Short$CountryData$"Properties"(, 3(
The typical usage of CountryData takes the form CountryData$"tag", "property"(, where "tag" is a
string (i.e., it is enclosed in double quotation marks) representing a country or group of countries
(such as "UnitedStates" or "G8"), and "property" is a string representing the desired property for that
country. A similar syntax applies to the other data commands. For instance:
One may specify a date or a range of dates for the property as follows. In the latter case the output is
suitable for inclusion in the DateListPlot command:
2.5 106
2. 106
Out[6]=
1.5 106
1. 106
Here is the gross domestic product of Germany, in US dollars, at the official exchange rate in place
at the time of this writing:
Out[8]= 3850.
And here we generate a list giving the name, gross domestic product, and oil consumption for every
save space, we use 31;;67 to take only the first six rows of data:
country. To accomplish this we use Table, where c ranges over the list of all possible countries. To
a nicely formatted table. To save space, we again use 31 ;; 67 to take only the first six rows of data:
Note the syntax used for missing data. With a bit of effort one can tweak the input above to produce
3.12 Importing Data 133
Country Gross Domestic Product +US dollars/ Oil Consumption +Barrels per day/
9
Afghanistan 6.50383 10 5000.
Albania 8.53753 109 25 200.
11
Out[10]= Algeria 1.02257 10 246 000.
8
AmericanSamoa 3.338 10 4000.
9
Andorra 3.0909 10 Missing#NotAvailable'
10
Angola 2.88526 10 46 000.
In the exercises we illustrate how to Sort the rows of such a table, for instance by oil consumption,
how to throw out rows containing missing data, and how to Select only rows, for instance, in which
gross domestic product exceeds a certain value. In short, the commands Sort and Select are needed
for such manipulations.
Here we make a ListPlot of the full data table above, showing each country’s annual gross domestic
product in U.S. dollars in the x coordinate, and that country’s oil consumption in barrels per day in
the y coordinate. A logarithmic scale is used on each axis. Missing data are simply not shown.
ListLogLogPlot$Table$CountryData#c, "GDP"',
CountryData$c, "OilConsumption"( , c, CountryData#' ((
In[11]:=
107
106
105
Out[11]= 104
1000
100
A slight modification allows us to add a Tooltip showing the name of each country as you mou-
seover its dot on the graphic. You’ll have to experience this in a live session to appreciate it. Essen-
tially, a tooltip such as this adds another dimension of content to your information graphic.
134 Functions and Their Graphs
In[12]:= ListLogLogPlot$
Table$Tooltip$CountryData#c, "GDP"', CountryData$c, "OilConsumption"( ,
CountryData#c, "Name"'(, c, CountryData#' ((
Out[12]=
Many of the data commands can produce graphical content. One can easily produce a map of each
country, for example:
Out[13]=
Out[14]=
Many of the data commands load gigantic collections of data. AstronomicalData, for instance,
which has information on over 100,000 celestial bodies, is astronomical in size. ChemicalData has
information on over 18,000 chemicals. FinancialData has up-to-date information on over 186,000
securities. Each data command has its own unique syntax conventions, so the Documentation
Center page for each such command is a must read. But there are also many similarities between
commands; if you become familiar with one command, others will be easy to learn. For instance,
3.12 Importing Data 135
after reading this section the input and output below should be self-explanatory, with only the units
in need of explanation (in this case the units are seconds):
5 1012
2 1012
1 1012
Out[17]= 5 1011
2 1011
1 1011
6. 1012
5. 1012
4. 1012
Out[19]=
3. 1012
2. 1012
1. 1012
In addition to built-in data commands, it is common practice to import data from other sources,
such as a spreadsheet or text file, or directly from a web page. Suppose, for instance, you find a
collection of raw data on a web page. For example, if you were to visit the URL
http://www.census.gov/genealogy/names/dist.male.first you would find a collection of curated data
136 Functions and Their Graphs
from the 1990 United States census in which male first names are ranked by frequency. The web
page is simply a plain text file containing four columns of data, with one or more spaces separating
data values on each row, and with a return character at the end of each row. Use the Import com-
mand with a single argument, a string containing the URL for the web site, to bring the data into
Mathematica.
There are over 1200 rows of data here. To save space we display only the top-ten list of male first
names:
In[21]:= Text
Grid$Join$"Most Popular Male First Names from the 1990 Census", SpanFromLeft ,
"Name", "Frequency +/", "Cumulative Frequency +/", "Rank" ,
data31 ;; 107(, Dividers Gray(
The ListPlot below shows the cumulative frequency distribution for the entire data set. Note that
when a single list of numerical values is given as the argument to ListPlot, the x-coordinate values
1, 2, 3, … are used. This allows us to easily see that there are slightly more than 1200 data points. It
also reveals that the 200 most popular male names account for over 70% of all males in the U.S.
80
Out[22]= 70
60
This same import technique works for many types of raw files that are found online, even graphic
files:
Out[23]=
The InputForm of this image reveals it to be a Raster of a matrix of pixel values. Once imported,
one could apply a transformation to the matrix of numerical values to alter the image.
In[24]:= Short$InputForm$pic(, 3(
Out[24]//Short=
Graphics#Raster#255, 679!!, 519!!, 3!!', 3!!'
When the data you’re after is found in a formatted table on a web page, add "Data" as a second
argument to Import, like this: Import$"URLstring" , "Data"'. For instance, here we import a web
page showing U.S. News and World Report’s list of top liberal arts colleges.
Import$
"http:sscolleges.usnews.rankingsandreviews.comsusnewsseduscollegesrankingss
briefst1libartcoBbrief .php", "Data"(
The output is rather large, so we don’t show it here. One simply copies and pastes (and if necessary,
edits) the list of data values from what is imported, and uses it as desired. Here, for instance, are the
top few colleges from this page at the time of this writing; we copied the relevant data from the
Import output, and pasted it into the Grid command below (evidently Carleton and Middlebury are
tied, as are Pomona and Bowdoin):
138 Functions and Their Graphs
You may also Import data from a file on your local hard drive. Suppose you have a spreadsheet
containing data that you want to analyze using Mathematica. The first step when importing a file is
to tell Mathematica where to look for it. This is accomplished with the SetDirectory command.
There are many ways to use this command. Its argument is a string representing the complete path of
the directory (i.e., the folder) containing the file. Of course this can be tedious to type if the file is
many levels from the top, and if the file is later moved, then its new path will be needed. Instead we
advocate the following approach: save your notebook if you have not already done so, and then
place the file you wish to import into the same directory containing your Mathematica notebook.
Then type and enter the following into this notebook:
In[26]:= SetDirectory$NotebookDirectory#'(;
This will set the current directory to be that of the notebook in which you are working. Even if you
later move this directory (containing the Mathematica notebook and your data file) to another
location, even to a different computer running a different operating system, the command above
will still set the directory correctly.
Now you are ready to Import your file. Here we use an Excel spreadsheet that we downloaded from
the data pages at the Math Forum maintained by Drexel University:
http://mathforum.org/workshops/sum96/data.collections/datalibrary/index.html.
We placed this spreadsheet into our notebook directory, as described above. The spreadsheet shows
the 2005 National League baseball salaries. Note that Import recognizes the file-type by the suffix
.xls, so no additional input is needed.
Excel spreadsheets typically have multiple “sheets.” Mathematica will import spreadsheets in the
form {sheet1, sheet2, sheet3,…}, where each sheet is imported as a standard list of lists, suitable for
display by Grid. In particular, if all of the data resides on the first sheet (a very typical scenario),
there will be an extra set of curly brackets around your data. That is the case with this file, so we use
First to access the first (and only) sheet, and display the top 20 rows of data:
A careful look at the data indicates that the actual data values begin on row 5, and that the last 3
rows are empty. Noting this, we can now make a histogram of all of the 2005 National League
baseball salaries:
In[29]:= Needs$"Histograms`"(;
140 Functions and Their Graphs
200
150
Out[30]=
100
50
We easily calculate that the mean salary exceeds a quarter million dollars, while the median is a
paltry $800,000:
Import will work with over 120 file formats. File types are recognized by the suffix on the filename,
so simply try Import$"filename"(, (where your filename includes the appropriate suffix) and chances
are good that you will have success. Here is a quick list of formats that are recognized at the time of
this writing:
In[32]:= $ImportFormats
Out[32]= 3DS, ACO, AIFF, ApacheLog, AU, AVI, Base64, Binary, Bit, BMP, Byte, BYU, BZIP2,
CDED, CDF, Character16, Character8, Complex128, Complex256, Complex64, CSV,
CUR, DBF, DICOM, DIF, Directory, DXF, EDF, ExpressionML, FASTA, FITS, FLAC,
GIF, Graph6, GTOPO30, GZIP, HarwellBoeing, HDF, HDF5, HTML, ICO, Integer128,
Integer16, Integer24, Integer32, Integer64, Integer8, JPEG, JPEG2000, JVX, LaTeX,
List, LWO, MAT, MathML, MBOX, MDB, MGF, MOL, MPS, MTP, MTX, MX, NB,
NetCDF, NOFF, OBJ, ODS, OFF, Package, PBM, PCX, PDB, PDF, PGM, PLY, PNG,
PNM, PPM, PXR, QuickTime, RawBitmap, Real128, Real32, Real64, RIB, RSS, RTF,
SCT, SDTS, SND, Sparse6, STL, String, SXC, Table, TAR, TerminatedString, Text,
TGA, TIFF, TSV, UnsignedInteger128 , UnsignedInteger16, UnsignedInteger24,
UnsignedInteger32, UnsignedInteger64, UnsignedInteger8, USGSDEM, UUE, VCF,
WAV, Wave64, WDX, XBM, XHTML, XHTMLMathML, XLS, XML, XPORT, XYZ, ZIP
3.12 Importing Data 141
Exercises 3.12
1. This exercise makes use of the ElementData command.
a. Construct a table with 118 rows and 3 columns. Each row should contain the name of an
element, its atomic weight (in atomic mass units), and its molar volume (in moles, obviously).
Use the first 118 elements listed in ElementData.
b. Make a ListPlot of molar volume versus atomic weight for your data.
c. Add a Tooltip to your ListPlot so that the name of each element is displayed as you mou-
seover it.
2. Visit the web site http://www.census.gov/genealogy/names/names_files.html.
a. Find the file giving the distribution of female first names, and make a table of female first
names, ranked by frequency.
b. With male first names, we showed in the text that roughly 70% of all males had one of the
top 200 names. What proportion of females have one of the top 200 names?
3. Visit the web site http://research.stlouisfed.org/fred2/data/FEDFUNDS.txt. It shows the effective
federal funds rate each month from 1954 to the present. Like the census site in the previous
exercise, this page contains raw data suitable for display in a Grid. Unlike the census site, how-
ever, the first 13 lines of text on this page describe the data that follows. That is, the file contains
more than just the straight data.
a. Use Import with the URL above as the first argument, and "Table" as the second argument.
b. Extract the data (starting on line 12, so that the column headers are included), and name it
data.
c. Use DateListPlot and Rest to view the data.
4. The Select command will apply a function to each member of a list. The syntax is:
Select[list,function]. It will return all items in the list for which the function returns the value
True. Typically the function is given as a pure function (these are discussed in Section 8.4). For our
purposes, just remember that the Slot character # represents the variable for the function, that is,
the items in the list. Enter the input below to find all of the properties available to CountryData
which contain the substring "Product":
5. Use Select to find all chemicals listed in ChemicalData that contain the substring "ButylEther".
6. How many cities in the U.S. have a population exceeding 100,000? Hint: Use the CityData
command together with Select to produce a list of such cities, then use Length to get the answer.
7. There are two standard ways of removing Missing data values from a list. One is to use Select,
and another is to use Cases together with Except.
a. Enter the two inputs below to see an example of each.
142 Functions and Their Graphs
It is often possible to define a sequence by specifying the value of the first term (say s#1' 3), and
giving a difference equation (also called a recurrence relation) that expresses every subsequent term as a
function of the previous term. For instance, suppose the first term in a sequence takes the value 3,
and each term that follows has a value twice that of its predecessor. We could express this via the
difference equation s#n' 2 s#n 1' for each n ! 1. A sequence defined this way is said to be defined
recursively. Computers make it easy to calculate many terms of recursively defined sequences. Here’s
a means for harnessing Mathematica for such a purpose:
3.13 Working with Difference Equations 143
Let’s walk through this carefully, as it makes nontrivial use of all three types of equal signs. First, the
left hand side is of the form s[n_Integer]. This indicates that the variable n must be an integer in
order for the definition that follows to be applied. This is a safety feature, as it will prevent the
inadvertent use of the function s being applied, for instance, to n .5 or to any other non-integer
input. Next we see the SetDelayed operator :=. This indicates that the expression to its right will
only be evaluated when the function s is called. We also see that the expression to its right is a
Piecewise defined function, as described in Section 3.6 on page 94. There are two distinct defini-
tions of s according to whether the input n is equal to 1 or greater than 1. These two conditions (on
the far right) will, for any value of n, evaluate to either True or False. Note the double equal sign
here; it is a condition to be tested, not an assignment being made to the symbol n, so the double
equal sign is needed. Finally, what happens when s#n' is called with a specific value of n? Let’s see:
In[3]:= s#4'
Out[3]= 24
Here is what is now stored in memory for the symbol s:
In[4]:= ?s
Global`s
s#1' 3
s#2' 6
s#3' 12
s#4' 24
Here’s what happened: s#4' was evaluated as 2 s#3', which was in turn evaluated as 2 +2 s#2'/,
which in turn was evaluated as 2 +2 +2 s#1'/, which was finally evaluated as 2 +2 +2 3/ 24,
with the assignment s[1]=3 being made. At this point, the intermediate assignments s[2]=6, s[3]=12,
s[4]=24 were made in turn. If you were to ask for s#4' again, it would be a one-step process and the
value 24 would be returned immediately, for at this point the assignment s[4]=24 has already been
made. This is very important, for if you were to then ask for, say, s#5', it would be a quick calcula-
tion: 2 s#4' 2 24 48. This is the reason for the Set operator = in each of the two lines of the
Piecewise definition of s; it prevents long chains of calculations being repeated.
144 Functions and Their Graphs
The only down side to this approach is that after, for instance, s#20' is evaluated, the assignments
s[1]=3, s[2]=6, … , s[20]=1572864 are all stored in memory. If s#200' is called, you literally have
hundreds of assignments stored in memory. Fortunately, they are all associated with the symbol s,
and so can be Cleared in one line:
In[5]:= Clear#s'
In[6]:= ?s
Global`s
Here are three things you can do with a sequence: compute an individual value (such as s#4' above),
make a table of values, or make a plot. These are easy, and will be discussed below. More subtle is
3,2n1 0. The
the task of trying to find a solution to a difference equation—an explicit representation of s#n' as a
function of n. For instance, the function in our example above has the solution s#n'
task of solving difference equations is addressed in Section 4.8 on page 189.
Let’s use a different example to illustrate the remaining topics. It is vitally important that we Clear
the symbol s when moving from one function to the next, as the myriad of intermediate assign-
ments from an earlier definition could easily pollute calculations to be made with a newer defini-
tion. For this reason it’s always a good idea to include a Clear statement when defining a function
recursively.
In[7]:= Clear#s';
s#1' 2 n 1
s$nBInteger( : 2
s#n' 3s#n 1' .05s#n 1' n!1
In[9]:= s#20'
Out[9]= 37.0838
However, there is one subtlety. Mathematica has a safety mechanism in place to prevent calculations
from falling into an infinite loop. It will not allow, by default, any recursively defined command
from calling itself more than 256 times. In practice, this means that you cannot ask for s#n' when n
is more than 256 units greater than the largest n with which s was previously called. For example, if
you want to know s#1000', you can’t just ask for it and receive an answer. But you can work up to it
by evaluating s#250', then s#500', then s#750', and then s#1000':
¿ The system parameter $RecursionLimit is by default set to 256. Another means of making
more than this number of recursive calculations is to assign a new value to this parameter. For
3.13 Working with Difference Equations 145
instance, you may simply type $RecursionLimit 1024, or whatever value you need, prior to
evaluating your sequence term.
Making a table of sequence values is accomplished exactly as it is for any other type of function:
In[11]:= Text Grid$Table#n, s#n', n, 10', Alignment Right, Left , Spacings 2(
1 2
2 5.8
3 15.718
4 34.8012
5 43.8474
Out[11]=
6 35.4125
7 43.5353
8 35.8398
9 43.2948
10 36.1624
ListPlot may be used as discussed in Section 3.10 for plotting the values of a sequence.
sn
40
30
Out[12]=
20
10
n
10 20 30 40 50
The option setting Joined True will connect the dots. In this case, it helps to clarify the oscillatory
nature of this sequence.
sn
40
30
Out[13]=
20
10
n
10 20 30 40 50
¿ To generate several terms of a sequence defined by a first-order difference equation it is quite
efficient to use NestList with a pure function as its first argument, rather than using Piecewise
to define the sequence and Table to generate values. NestList is discussed in Section 8.7 and
pure functions are discussed in Section 8.4. Here’s an example showing how to generate the
first ten terms of the sequence in the last example with only a few keystrokes. The first
argument is a pure function that will generate a member of the sequence from the previous
term. The second argument is the initial value in the sequence. The final argument is how
many iterations you desire.
Out[14]= 2, 5.8, 15.718, 34.8012, 43.8474, 35.4125, 43.5353, 35.8398, 43.2948, 36.1624
Exercises 3.13
1. Consider the sequence s#n' with s#1' 100, and with the remaining terms defined by the differ-
ence equation s#n' 1.05s#n 1'.
a. Enter this into Mathematica.
b. Find s#20'.
c. Make a ListPlot of the first 30 terms of the sequence.
d. Assuming that the solution to this difference equation is of the form s#n' p
bn for real
parameters p and b, use FindFit and the data used in the ListPlot above to find a solution.
4
Algebra
300
200
Out[3]=
100
2 1 1 2 3 4 5
Here we see the graph of a polynomial that appears to have roots at x 1 and x 4 (that is, the
function appears to assume the value 0 when x 1 and x 4/. We can confirm this by factoring the
polynomial:
In[4]:= Factor#f#x''
Observe that when x assumes the value 4, the linear factor +4 x/ is zero, making the entire product
equal to zero. Similarly, if x 1, the linear factor +1 x/ is zero, and again the product is zero. Roots
of a polynomial are often easily identified by determining the linear factors in the factored form of
the polynomial.
148 Algebra
The task of finding the roots of a given function f is a vitally important one. Suppose, for instance,
that you need to solve an equation in one variable, say 12x3 3x4 3x 12. Equations such as
this arise in a wide variety of applied contexts, and their solution is often of great importance. But
solving such an equation is equivalent to finding the roots of a function—just subtract from each
side of the given equation everything on the right hand side. In this case we get
12 3x 12x3 3x4
f +x/
0, so the solutions of this equation are the roots of the function
12 3x 12x3 3x4 , which we have just found (via factoring) to be 4 and 1. Solving equa-
tions and finding roots are essentially the same task.
You can expand a factored polynomial with the Expand command. This will essentially “undo” the
factoring. One way to use this command is to open the AlgebraicManipulation palette (look for
palettes in the Palettes menu). Use your mouse to highlight the factored output above, then push
the Expand#e' button. Another way is to type:
In[5]:= Expand#'
Out[5]= 12 3 x 12 x3 3 x4
The expanded form gives us different information about the function. The constant term (in this
case 12) represents the y intercept of the polynomial’s graph. It’s simply the value of the function
when x 0. The leading coefficient (in this case 3, the coefficient of x4 ) is positive. Since the 3x4
summand will dominate the others for large values of x, a positive leading coefficient tells us that
the function values will get large as x gets large.
It is important to note that some polynomials have real roots that will not be revealed by the Factor
command:
15
10
Out[6]=
5
4 2 2
Out[7]= 1 3 x x2
The graph clearly indicates two real roots, the x intercepts, yet there are no linear factors present in
the factored form of the polynomial. Why? The Factor command will not extract factors that
involve irrational or complex numbers unless such numbers appear as coefficients in the polynomial
being factored. Since the coefficients in the above polynomial are all integers, only factors with
integer coefficients will be extracted. To get approximations to the real roots, simply replace one of
4.2 Finding Roots of Polynomials with Solve and NSolve 149
the integer coefficients in the original polynomial by its decimal equivalent by placing a decimal
point after it. In doing this you are telling Mathematica that decimals are acceptable in the output:
The real roots are approximately .302776 and 3.30278. You can easily check that this is consistent
with the graph (and of course, you should).
Lastly, note that as always Mathematica makes a distinction between decimals and fractions when
factoring:
)
1
In[10]:= Factor%x2
4
+1 2 x/ +1 2 x/
1
Out[10]=
4
Exercises 4.1
1. Let f +x/ 1 5x 2 x3 10x4 .
2. Factor the following expressions and explain the differences in the resulting factorizations.
a. 1 xn xm xnm and 1 x x3 x4
b. 1 x3 and 1 xn
c. 1 x4 and 1 x2n
Let’s take another look at the polynomial 1 3x x2 from the previous section:
NSolve provides approximate numerical solutions to equations. It takes two arguments, separated as
always by a comma. The first argument is an equation. Note that the double equal sign m is used for
equations; this is because the single equal sign is used to assign values to expressions, an essen-
tially different operation. You may also use the m button on the BasicMathInput palette. The second
argument in the NSolve command (x in the example above) specifies the variable for which we
want to solve. It may be obvious to you that you wish to solve for x, but it’s not to the computer.
For instance, there may be occasions when the equation you are solving involves more than one
variable (we’ll see an example later in this section). Lastly, the NSolve command can take an
optional third argument which specifies the number of digits of precision that you desire:
Now what about the output? First notice that it is in the form a list (a sequence of items separated
by commas with a set of curly brackets around the whole thing). This is because there are typically
the items in this list. Each is of the form x solution. This looks strange at first, but it is easy
numerous solutions to a given equation, so it is sensible to present them in a list. Now let’s focus on
enough to interpret. It is an example of a structure called a replacement rule, which will be explored
later in this section.
You can smarten the appearance of the list of solutions by making a Grid of the results. As discussed
in the last chapter (Section 3.5, see page 87) when Grid is applied to a such a list it will produce a
neatly formatted column:
x 3.3027756377319946465596106337352480
Out[3]=
x 0.3027756377319946465596106337352480
Can Mathematica produce exact solutions to polynomial equations? The answer is sometimes. It is a
mathematical fact that some polynomial equations involving powers of x that exceed 4 cannot be
solved algebraically, period. However, if an equation can be solved algebraically, the Solve com-
mand is the ticket. Here are the precise roots of the polynomial above:
.3 13 2
1
x
2
.3 13 2
Out[4]=
1
x
2
4.2 Finding Roots of Polynomials with Solve and NSolve 151
Remember the quadratic formula? That’s all that’s happening here. In fact, if you ever forget the
quadratic formula, you can have Mathematica derive it for you:
Clear#a, b, c, x';
Solve$a x2 b x c m 0, x( ss Grid
In[5]:=
b b2 4 a c
x
2a
Out[6]=
b b2 4 a c
x
2a
Note the space between a and x2 , and between b and x in the last input line; they are needed to
indicate multiplication. This example also makes it clear why the second argument to the Solve
command is so important; this is an equation that could be solved for a, for b, for c, or for x. You
have to specify the variable for which you wish to solve.
Let’s look at a few more examples of these commands in action. We’ll start with the NSolve command
and later address some special considerations for using the Solve command:
2
Out[7]=
3 2 1 1
2
Out[8]= x ,1 3 x x2 0
Note that the factor x corresponds to the root x 0, but that the other roots are not revealed by the
Factor command (although they would have been found had we replaced the x by 1.*x in the
polynomial). The NSolve command reveals all roots, always.
Now let’s tweak things a little. We can shift the graph of this function up by one unit by adding 1 to
its expression, and the resulting function should have only one real root (the dip on the right of the
graph will be entirely above the x-axis):
152 Algebra
Out[10]= 2
3 2 1 1
2
Out[11]= 1 x 3 x2 x3
This didn’t do a thing; the new function has no rational roots. What happens if we replace one of
the integer coefficients with its decimal equivalent?
This reveals a real root near x 2.76929. But what about the quadratic factor?
Mathematica is reporting three roots. The first root reported is the x-intercept that we see in the plot.
The second two are complex numbers; they are each expressions of the form a bÇ, where Ç is the
imaginary number whose square is 1. They are purely algebraic solutions to the polynomial
equation, bearing no obvious geometric relationship to its graph. Although you may not care to
contemplate complex roots of equations, the Solve and NSolve commands will always display them
if they exist. It is a fact that every polynomial whose highest power of x is n will have exactly n
roots, some of which may be complex numbers (see the fundamental theorem of algebra in Section 4.4
on page 169). It is also true that any complex roots of a polynomial (whose coefficients are all real)
come in conjugate pairs; one will be of the form a bÇ, the other a bÇ, as in the output above.
How can you extract one solution from a list of solutions? For instance, you may only need a real
solution, or the context of the problem may dictate that only positive solutions be considered. You
can extract a single solution from the list of solutions using double square brackets. This was dis-
cussed in Section 3.11 on page 126. Here’s an example to illustrate:
We have given the list of solutions the name sols (note the assignment operator = assigns the name
sols to the output, while the equation operator m is used to produce equations). Here’s how to
4.2 Finding Roots of Polynomials with Solve and NSolve 153
extract the first element from a list (type È [[ È to get 3, or you may also just use two square
brackets back to back):
sols317
x 15
In[15]:=
Out[15]=
sols327
x 15
In[16]:=
Out[16]=
Out[17]= b
You may also use the e3f7 button on the BasicMathInput palette to extract an item from a list.
To use one of the solutions provided by the NSolve or Solve command in a subsequent calculation,
you need to understand the syntax of replacement rules. The symbol /. tells Mathematica to make a
replacement. It is shorthand for a command called ReplaceAll. You first write an expression involv-
ing x, then write /. and then write a replacement rule of the form x solution. The arrow is found
on the BasicMathInput palette. You may type -> (the “minus” sign followed by the “greater than”
sign) in place of the arrow if you wish:
In[18]:= x2 s. x 3
Out[18]= 9
This last input line can be read as “Evaluate the expression x2 , replacing x by 3.”
Here’s how you can use replacement rules to extract solutions generated by the Solve command:
In[19]:= x s. sols317
Out[19]= 15
In[20]:= x s. sols327
Out[20]= 15
In[21]:= x2 s. sols327
Out[21]= 225
If you don’t specify which solution you want, you will get a list where x is replaced by each solution
in turn:
154 Algebra
x s. sols
15, 15
In[22]:=
Out[22]=
You can do all of this in one step, generating output that is a list of solutions rather than a list of
replacement rules:
You may also use replacement rules to test whether an equation holds for a particular value of x:
In[25]:= x2 225 m 0 s. x 10
Out[25]= False
Replacement rules take some getting used to, but they are enormously convenient. Here, for
instance, we plot a polynomial, and include an Epilog to place a Point at each of the roots (this will
work when all roots are real):
In[26]:= f#xB' : 12 4 x 15 x2 5 x3 3 x4 x5
In[27]:= Plot$f#x', x, 4, 3, Epilog PointSize#.02', Point#x, 0' s. NSolve#f#x' m 0, x'(
40
20
Out[27]=
4 3 2 1 1 2 3
20
40
Now let’s look at the Solve command in greater detail. Note that you can find the exact roots
(without any decimal approximations) for any polynomial whose degree is four or less:
x 1
. .47 3 249 22
1s3 1s3
1 2 1
x 12
3 473 249 2
.1 Ç 32 .1 Ç 3 2 . .47 3 249 22
1s3 1s3
Out[28]= 1 1 2 1 1
x
3 3 473 249 6 2
.1 Ç 32 .1 Ç 3 2 . .47 3 249 22
1s3 1s3
1 1 2 1 1
x
3 3 473 249 6 2
Wow, this is powerful stuff! But be careful when using the Solve command. If you just need an
approximate decimal solution to an equation you will be better served using NSolve. In particular, if
you want a numerical approximation to a solution generated by the Solve command, as you might
with the output generated above, it is not a good idea to apply the N command to the result. In
some cases, for instance, you may end up with complex numbers approximating real roots (try
solving x3 15x 2 0; it has three real roots, yet applying N to the output of the Solve command
produces complex numbers—see Exercise 2). The moral of the story: Use Solve to generate exact
answers; use NSolve to generate numerical solutions to any required degree of accuracy.
Another consideration to be aware of when using the Solve command is that equations involving
polynomials of degree 5 or more (i.e., where the highest power of x is 5 or more) may not have
explicit algebraic solutions. This is a mathematical fact; there are equations of degree five with roots
that cannot be represented in radicals. Here is what the output will look like in these situations:
In[29]:= Solve$x5 x 1 m 0, x(
x Root$1 Ó1 Ó15 &, 1( , x Root$1 Ó1 Ó15 &, 2( , x Root$1 Ó1 Ó15 &, 3( ,
x Root$1 Ó1 Ó15 &, 4( , x Root$1 Ó1 Ó15 &, 5(
Out[29]=
The output here is comprised of Root objects, which are a means of cataloging the (in this case five)
roots that cannot be expressed in algebraic form using radicals. When Root objects arise, it is always
possible to apply N to the output to get numerical approximations. Alternately, NSolve can be used
to get the same result:
After all this you may wonder why the Solve command is ever used, since NSolve seems to be more
versatile and robust. The answer is that it depends on your purposes. For numerical solutions to
specific problems, NSolve is probably all you need. But for exact algebraic solutions or the deriva-
tion of general formulae, Solve is indispensable. Here are two more examples to illustrate the power
156 Algebra
of this command. The first provides the general formula for the roots of a cubic equation of the
form x3 b x c, where b and c may be any numbers:
- 1
1s3
2 1s3 9 c 3 4 b3 27 c2
b
x 3
1s3
21s3 32s3
9 c 3 4 b3 27 c2
1s3
1Ç 3 b 1Ç 3 9 c 3 4 b3 27 c2
Out[32]= x 1s3
2 21s3 32s3
22s3 31s3 9 c 3 4 b3 27 c2
1s3
1Ç 3 b 1Ç 3 9 c 3 4 b3 27 c2
x 1s3
2 21s3 32s3
22s3 31s3 9 c 3 4 b3 27 c2
The next example illustrates how Solve may be used to put a given formula into a different form. It
is a rather trite computation, but it illustrates the versatility of the Solve command:
m !!
e
Out[34]=
c2
One last comment about the Solve command is in order. As you may expect, it will distinguish
between decimals and fractions in the input, and adjust its output to match:
1
In[36]:= Solve%x2 m 0, x)
4
x !, x !!
1 1
Out[36]=
2 2
Exercises 4.2
1. In Exercise 1 of Section 4.1 we used Factor to find the real roots of f +x/ 1 5x 2 x3 10x4 .
a. Use Solve to find the real roots of f +x/ and compare your solutions with the values you found
in Exercise 1 of Section 4.1.
4.3 Solving Equations and Inequalities with Reduce 157
b. Use NSolve to approximate the real roots of f +x/ and compare your solutions with the values
you found in Exercise 1 of Section 4.1.
2. Use Solve followed by N to approximate the roots of the polynomial x3 15x 2. Then find the
roots using NSolve. Which gives the better approximation?
3. Fix two real numbers p and q, and consider the following quadratic equation in the variable z:
1 3
z2 q z p 0.
27
a. Solve this equation in terms of p and q (use Mathematica, or work by hand using the quadratic
formula).
1
z2 q z p3
b. Consider the expression 27
, which has the same roots as the quadratic above. Use
.3 y 4 p 3 y2 2, and then p by
z
1
replacement rules to replace z by w3 , then w by 3
Out[1]= x m 10 «« x m 10
The values of x are given as equations rather than as replacement rules. The double vertical bar ||
stands for the word “or.” So the output here reads, “Either x is equal to 10, or x is equal to 10.”
The reason for the different output format is that Reduce is designed to consider special conditions
on all parameters appearing in an equation, whereas Solve will ignore conditions on any parameter
whose solution is not explicitly being sought. For example, the solution below only makes sense
when the parameter a 0:
x !!
b
Out[2]=
a
158 Algebra
+b m 0 && a m 0/ «« a 0 && x m
b
Out[3]=
a
Note that the double ampersand && stands for the word “and,” so this reads, “Either a and b are
b
both equal to 0, or a is nonzero and x .” The output syntax is designed to handle subtle
a
expressions like this.
Another point where Reduce and Solve differ arises when an equation has an infinite number of
discrete solutions, such as cos+x/ 0. We all know that the cosine function is equal to zero when its
S
argument is of the form 2k S , where k is any integer. Solve will not attempt to display them all.
2
Rather it will send a warning message suggesting that you try Reduce instead:
x !, x !!
S S
Out[4]=
2 2
Okay, let’s try it:
2 S C#1' «« x m
S S
Out[5]= C#1' ± Integers && x m 2 S C#1'
2 2
The output says precisely what it should, but we first need to understand the syntax. We said earlier
S
that the solution set is comprised of all numbers 2k S , where k is an integer. Reduce uses the
2
generic name C[1] (rather than k) when it has to introduce a constant. It’s a rather unsightly name,
but something like this is necessary. For instance, in an output where fifty such constants are
needed, they will be called C[1], C[2], and so on; the numerical piece guarantees that the notation is
capable of handling arbitrarily many such constants. Next, we see the symbol ±. This is standard
mathematical notation; it reads “is an element of.” Hence the output above says: “ C#1' is an element
S
of the set of integers, and x 2S C#1' .” The output concisely describes all of the roots of the
2
cosine function.
In[6]:= Reduce$x2 1 ! 0, x(
Out[6]= x 1 «« x ! 1
If you were asked to describe the natural domain (among all real numbers) for the function
4.3 Solving Equations and Inequalities with Reduce 159
f +x/
x2 7x3
, you would want to know for which values of x the polynomial
x3 4x2 2x1
The solution is given in terms of a Root object, which at first glance may seem both intimidating
and unhelpful. A careful inspection shows that Root simply writes the polynomial in the form of a
pure function (see Section 8.4 page 403 for a discussion of these) for its first argument. The second
argument gives an index number for the root in question. Essentially, Root objects are a means of
cataloging roots of polynomials. What is important for us here is the fact that using N, we can
numerically approximate any Root object with ease:
Out[8]= x ! 3.51155
If an exact answer is preferred, one can specify for cubic or quartic polynomials that an explicit
expression in radicals be given (instead of Root objects); just set the option Cubics (or Quartics in
the case of a polynomial of degree 4) to True:
.83 3 321 2
1s3
4 1 83 3 321 1 1
Out[9]= x!
3 3 2 2 3 2
, x, 0, 5)
x2 7x 3
In[10]:= Plot%
3 2
x 4x 2x 1
2
4
Out[10]= 6
8
1 2 3 4 5
from the field of mathematical logic. In this notation, the symbol  means “and” and the symbol Á
Note that Mathematica can also express the output generated by Reduce using standard notation
means “or.” If you have experience with this notation, you may find it easier to read. A constant is
also represented as c1 rather than C#1'. In all, it reads a bit more nicely. To get your output in this
form, simply apply TraditionalForm to the output of Reduce.
160 Algebra
c1 ± = Ð x n 2 S c1 Î x n 2 S c1
S S
2 2
Here is a more complicated example in which there are two constants. The symbol = denotes the set
of integers, as is traditional in mathematical notation.
While the output for Reduce reads very nicely, it is not obvious how one could work programmati-
cally with it to perform some follow-up work. It is certainly possible to copy and paste portions of
the output, but there are other ways. First note that just as you can extract parts from a List, you can
extract parts from a logical expression like the one above:
Out[13]=
In[15]:= ToRules#ans'
In[16]:= rules Flatten#Table#ToRules#ans' s. C#1' 0, C#2' k, k, 1, 1', 1'
Here, for example, is a plot of the function f +x/ sin+1 cos+x// with the solutions to the equation
f +x/ 1 shown as large points via the Epilog:
In[17]:= Plot$Sin#1 Cos#x'', x, 10, 10, Epilog PointSize#.02', Point#x, 1' s. rules(
1.0
0.8
0.6
Out[17]=
0.4
0.2
10 5 5 10
As with Solve and NSolve, it may be the case that complex numbers are generated by Reduce.
.9 78 2
1s3
««
1
xm
.3 .9 78 22
Out[18]=
2s3 1s3
3
.1 Ç 3 2 .9 78 2
1s3
««
1Ç 3
xm
2 .3 .9 78 22
2s3 1s3
23
.1 Ç 3 2 .9 78 2
1s3
1Ç 3
xm
2 .3 .9 78 22
2 32s3 1s3
In the next section we discuss a means for dispensing with non-real output.
162 Algebra
Exercises 4.3
1. Find the largest possible domain in the real numbers for the function f +x/
x3 5x2 4x5
. Make a
x2 2x3
plot of f over a valid domain in the positive real numbers, along with the function
2. Reduce can be used to solve systems of equations. A system of equations is a collection of several
equations, each in several variables. A solution to such a system is a set of values for the variables
for which all the equations are satisfied.
a. A system of equations can be entered in several ways. One can either type a list of equations,
or an equation of lists like the input below. Type and enter this input.
Reduce$Cos$y( Cos$x Cos$y((, x Cos$x Cos$y(( Sin$y( m 0, 0, x, y , Reals(
solution set happens to be rather complex. If we regard a particular solution ,x0 , y0 0 as a point
b. The output is a bit intimidating at first. This is unavoidable for the simple reason that the
in the x y plane, then this output is comprised of both discrete points and entire curves in the
plane. Read the output carefully and identify the discrete points. ListPlot them in the plane.
c. Use ContourPlot to sketch the solutions of both equations.
3. We can also use Reduce to solve a single equation with more than one variable. Use Reduce to
8x y2 4x
,1y2 0
find the set of points in the plane that are solutions of 2
and then use ContourPlot to
1y2
In[1]:= +8/1s3 ss N
Out[1]= 1. 1.73205 Ç
In high school and early college courses it is more common to work with the real-valued cube root
function, which would return the value 2 (instead of the complex number above) as the cube root
of 8. Here are a few other instances where complex numbers can pop up in the context of a real
computation:
In[2]:= Log#2'
Out[2]= Ç S Log#2'
4.4 Understanding Complex Output 163
In[3]:= ArcSin#2.'
.9 57 2
1s3
««
2
x m 1
.3 .9 57 22
Out[5]=
32s3 1s3
.1 Ç 3 2 .9 57 2
1s3
««
1Ç 3
x m 1
.3 .9 57 22
2s3 1s3
23
.1 Ç 3 2 .9 57 2
1s3
1Ç 3
x m 1
.3 .9 57 22
2 32s3 1s3
Now this sort of output ought to make you hungry to learn about the complex numbers. If you are
even a bit curious, ask your instructor about them. And even if you are not particularly interested in
such matters be mindful that you are now using a grown-up software package, so you are going to
have to deal with them from time to time. Let’s deal with the inputs above individually. We’ll
discuss the cube root function at length later in this section. The next two inputs simply extend the
domains of the logarithm and inverse sine functions beyond what we ordinarily allow. Doing so
results in complex output. In a precalculus or calculus course, the likelihood of encountering com-
plex numbers in this way is small. The next outputs, however, will be more difficult to avoid. We
have already seen instances in which Solve and NSolve will find complex solutions to equations,
even when every quantity appearing in those equations is real. Whereas Solve and NSolve are
designed to always report complex solutions when they exist, the command Reduce allows one to
specify that only real values are to be considered. Simply set the optional third argument of Reduce
to Reals. If there are no real solutions, it will return False.
Out[6]= False
Out[7]= False
164 Algebra
Out[8]= False
Out[9]= x m 2
.9 57 2
1s3
2
x m 1
.3 .9 57 22
Out[10]=
2s3 1s3
3
The lesson here is that if you are interested only in real solutions to equations, using Reduce with
Reals as its third argument is a good strategy.
Another subtle issue that can arise from time to time is illustrated below:
.1 2 Ç 31 2 ««
5 1s3
xm
.1 2 Ç 31 2
Out[11]=
1s3
5 .1 Ç 32
.1 Ç 3 2 .1 2 Ç 31 2 ««
1 1s3
xm
2 .1 2 Ç 31 2
1s3 2
5 .1 Ç 32
.1 Ç 3 2 .1 2 Ç 31 2
1 1s3
xm
2 .1 2 Ç 31 2
1s3 2
Here we have asked only for real roots, and yet Reduce has returned three expressions involving Ç.
The output is correct; each of these numbers is indeed a real number (one could verify this with a
plot of this cubic—its graph crosses the x-axis three times). But just as one can write the real number
1 as Ç2 , it is possible to express other real numbers in a manner that makes use of the complex
number Ç. That’s what has happened here. In some cases it is possible to algebraically manipulate
such numbers so that all the Ç’s go away. The command that can accomplish this is called Complexg
Expand. It will attempt to break a knotty complex number into its real and imaginary components.
In this case, the imaginary part of each of the numbers above should be exactly zero. One could
apply simply append //ComplexExpand to the previous input, but we will apply it just to the first
root reported above to make the output easier to read:
4.4 Understanding Complex Output 165
)
5 1s3
In[12]:= ComplexExpand% 1 2 Ç 31
1s3
1 2 Ç 31
Notice the structure of the output; it is of the form a Çb. If this is indeed a real number, it had
better be the case that b 0. Now your first impression upon seeing intricate output like this may be
to both marvel at what Mathematica can do, and simultaneously to glaze over and fail to examine
the output critically. Pause for a moment to take a good look at it, and focus your attention on the
imaginary component b. You will find that b is indeed zero. It is of the form 5 sin+c/ 5 sin+c/,
and since sin+c/ sin+c/, the entire quantity is zero. In the next section the Simplify command
will be discussed. It can be invoked to carry out this simplification as well:
31 ) ) 31 ) ))
1 1
In[13]:= Simplify% 5 Sin% S ArcTan%2 5 Sin% S ArcTan%2
3 3
Out[13]= 0
Note also that the opposite issue can arise—certain non-real complex numbers may be expressed
using only real numbers. At first glance they don’t betray their complexity:
In[14]:= Reduce$x3 m 1, x(
In[15]:= ss ComplexExpand
x m 1 «« x m «« x m
1 Ç 3 1 Ç 3
Out[15]=
2 2 2 2
any real number to be its real cube root. So, for instance, +8/1s3
and calculus courses, where the complex number system is not utilized, one defines the cube root of
2. Mathematica uses a different
definition, which we will discuss in this section. Under this definition +8/1s3 1 Ç 3.
166 Algebra
In[16]:= ComplexExpand$+8/1s3 (
Out[16]= 1Ç 3
The reality is that when complex numbers are taken into account, there are three numbers whose
cube is 8:
Out[17]= x m 2 «« x m 1 Ç 3 «« x m 1 Ç 3
So in defining the cube root, one of these three must be chosen. Mathematica chooses the last of
these. Note that the underlying command used to raise a number to a power is called Power:
In[18]:= xp ss FullForm
Out[18]//FullForm=
Power$x, p(
It is the definition of this basic arithmetic operation that is at issue. When a negative number is
the rational power 1 s 3), you might like to have a power expression evaluate to a real number, as
raised to a rational power, and the denominator of that rational power is an odd number (e.g., 3, for
would be expected in a a precalculus or calculus course. In Section 3.2 on page 58 an alternate power
command called realPower was defined that can be used to emulate the real-valued power func-
tions commonly encountered in such a course.
We now discuss a topic that falls outside of the standard precalculus and calculus curricula: why on
earth does Mathematica report that +8/1s3 1 3 Ç when it would be so much simpler to say that
the cube root of 8 is just 2? What possible reason could there be for such insanity? This will take
a bit of careful thought, and a page or two of explanation, so make yourself comfortable before
reading on. We’ll see that there is indeed a compelling reason.
Well, raising any negative real number to the power 1 s 3 would, in a similar manner, produce a
Let’s suppose, for the sake of argument, that the cube root of 8 is 2. What consequences follow?
negative real number. In fact, raising any negative real number to the power 1 s n, where n is an odd
positive integer, would produce a negative real number. Now suppose we raise this result to the mth
power m s n. If m is odd the result is another negative number, while if m is even the result is a posi-
power, where m is a positive integer; that is, our original negative number is raised to the rational
tive number (since squaring a negative number results in a positive number). To summarize: raising
a negative number to a positive rational power with odd denominator produces a real number. This
number is negative or positive according to the parity (odd or even) of the numerator. So far so
good, but there’s a problem.
Just as the exponential function g+x/ 2x is continuous, in a just world we would also expect the
function f +x/ +2/ to be continuous. What happens when x is a rational number with odd denomi-
x
nator? Are we to accept a state of affairs in which +2/311s99 is a negative real number, but +2/312s99 is
4.4 Understanding Complex Output 167
a positive real number? The two exponents are very close to each other, yet they are producing
311 312
values that are not close to each other (you can check this). Furthermore, noting that S ,
how would we define +2/S ? It simply cannot be done when we operate under this convention that
99 99
rational power with even denominator. For instance, using the power 1 s 2, the square root of a
And of course, under such a convention it does not even make sense to raise a negative number to a
negative number is … what? It is certainly not a real number. That’s a strong indication that one
may need to consult the complex number system.
The complex numbers are in fact rather simple. They include the imaginary number Ç, which has
the property that Ç2 1. There are many other complex numbers whose squares are real. For
instance, +2Ç/2 2 2
2 Ç 4. Using multiples of Ç like this, one can find square roots of any negative
real number. In general, a complex number has the form a b Ç, where a and b are real. a is referred
every complex number a b Ç can be represented as an ordered pair +a, b/, and so can be geometri-
to as the real part of this complex number, and b Ç is its imaginary part. At this point, let’s note that
numbers. The real number 1 has coordinates +1, 0/, and the complex number Ç has coordinates +0, 1/.
cally identified with a point in the plane. The complex plane refers to this model of the complex
The correct definition of the powers of a negative real number necessarily entails the complex
number system. Just as every positive real number has two square roots (one positive and one
negative), every negative number also has two square roots. But neither of them are real numbers,
both are complex. And just as we choose, for any positive real number a, one of its two square roots
to be a1s2 (we define a1s2 to be the positive square root of a), we must also choose one of the two
complex square roots of a to be +a/1s2 . Which is the square root of a? We choose the complex
root whose argument is least, the so-called principal square root. That is, if one were to draw rays
from the origin, one to each of the square roots of a in the complex plane, and for each measure
the angle counterclockwise from the positive real axis to each ray (this angle is the argument of the
complex number), the ray with the smallest angle corresponds to the principal root. Under this
convention, for instance, +1/1s2 1 Ç.
Higher roots are even more subtle. In the complex number system, every nonzero number has three
cube roots. One is real, and the other two are complex. The correct definition of the cube root of any
real number a (that is, the definition of a1s3 ) is the principal cube root, the one whose argument is
smallest. When a ! 0, this is the real cube root that we know and love. For instance, 81s3 2. The
argument of 2 is zero radians after all, so it must be the principal cube root. But when a 0, the real
has argument S s 3. This is the principal cube root of a. This is the one that we designate to be a1s3 .
cube root is negative, and so its argument is S radians. It so happens that one of the complex roots
So, in particular, working in the complex numbers as Mathematica does, +8/1s3 is not 2. Here is a
graphic showing all three roots of 8 in the complex plane. Each is shown at the end of a ray
Graphics$
Thick, Blue, Line#0, 0, Re#x', Im#x'' s. NSolve$x3 m 8, x( ,
In[19]:=
1.5
1.0
0.5
Out[19]=
real
2.0 1.5 1.0 0.5 0.5 1.0
0.5
1.0
1.5
This notion of using the principal nth root as the proper definition of a1sn has long been accepted by
tions, such as f +x/ +2/x . Note that for values of x that are close to each other, the values of this
the mathematical community. The most immediate benefit is the continuity of exponential func-
7.96732 3.79246 Ç
Out[20]=
7.89809 4.07175 Ç
This means that it is possible to define powers with irrational exponents to be limits of powers with
7.96732 3.79246 Ç
Out[21]= 7.96618 3.7974 Ç
7.89809 4.07175 Ç
f +x/ +2/x on the domain 4 x 0, by making a table of values for this function, and then
It is easy, in fact, to witness the continuity (and the beauty) of the complex-valued function
produce a graphic of these numbers in the complex plane, joining adjacent values with line
segments:
4.4 Understanding Complex Output 169
0.2
real
Out[23]= 0.4 0.2 0.2 0.4 0.6 0.8 1.0
0.2
0.4
0.6
Note that the function assumes real values at precisely those points where this graph crosses the x-
axis. You would be correct to speculate that it does so on this domain when the input variable
assumes the integer values 4 through 0. Can you calculate the values of this function at those
points?
One last word regarding the complex numbers is in order. If you’ve made it this far, you deserve to
know one final fact. This fact is so important that it is commonly known as the fundamental theorem
of algebra. We won’t prove it, but we will tell you what it says. It states simply that every polynomial
of degree n (with real or complex coefficients) can be factored completely over the complex numbers
into n linear factors. It follows that for any positive integer n, and any real number r , the polynomial
xn r has n linear factors. It so happens that the factors will all be distinct in this case. In other
words, every real number r has precisely n nth roots. This is why all real numbers have three cube
roots. Letting r 8, we reconstruct the polynomial x3 r x3 8 from the three cube roots of 8:
Out[24]= 8 x3
The fundamental theorem also explains why Solve and NSolve will always report n roots for a
polynomial of degree n. Here is an example in which we display the eight roots of an eighth-degree
polynomial as eight points in the complex plane (of which two happen to be real):
170 Algebra
Out[25]=
2 1 1 2
1
2
Exercises 4.4
1. Use the following Manipulate to guess the value of c for which the polynomial c 6x 8x3 has
precisely two real roots. Test your guess. Note that by default, TraditionalForm is applied to the
PlotLabel in any Plot.
x n 0.866025 Í x n 0 Í x n 0.866025
8
4
Out[26]=
2
4
valued function f +x/ x2s5 on the domain 32 x 32 shown below. What is f +32/?
2. Use the command realPower defined in Section 3.2 on page 58 to produce the graph of the real-
4.5 Working with Rational Functions 171
4. Consider the following input and output, and use the fundamental theorem of algebra to formu-
late a plausible explanation for the apparent redundancy:
6. Make a Manipulate that displays the roots of the nth degree polynomial xn 1 in the complex
plane, where there is a SetterBar displaying values of n from 1 to 10. These will be graphical
depictions of all the nth roots of 1.
7. Make a Manipulate that displays a Line joining a Table of values for the function +k/x on the
domain 4 x 0 in the complex plane. Make a slider for k which assumes values from 0.1 to 4.
Set the PlotRange to 2.
+x 3/+x 1/
+x 1/
In[1]:= Solve% m 0, x)
Thus all the remarks in Section 4.2 apply to equations involving rational functions as well to those
involving only polynomials.
)
1 x5
In[2]:= Simplify%
1x
Out[2]= 1 x x2 x3 x4
The Simplify command, like Expand and Factor, takes an expression as input and returns an
equivalent expression as output. Simplify attempts a number of transformations and returns what it
believes is the most simple form. In the case of rational functions, Simplify will cancel the common
factors appearing in the numerator and denominator. In the example above, the linear expression
1 x can be factored out of the numerator. You can easily check the result:
Out[3]= 1 x5
You can also guide Mathematica through such a simplification step by step. The best way to do this
is by opening the AlgebraicManipulation palette (in the Palettes menu). Use your mouse to highlight a
certain portion of an algebraic expression, and then feed that portion of the expression to one of the
algebraic manipulation commands. This essentially allows you to drive Mathematica step by step
through an algebraic manipulation. Here, for instance, is a rational function:
x4 5 x3 8 x2 7 x 3
3 x4 14 x3 18 x2 10 x 3
Rather than simplify it in one go with the Simplify command, let’s drive through it step by step.
First, use the mouse to highlight the numerator, then push the Factor#e' button on the
AlgebraicManipulation palette. The cell will then look like this:
+x 1/ +x 3/ ,x2 x 10
3 x4 14 x3 18 x2 10 x 3
Now repeat the process to factor the denominator:
+x 1/ +x 3/ ,x2 x 10
+x 1/ +x 3/ ,3 x2 2 x 10
There is clearly some cancellation that can be done. Highlight the entire expression and push the
Cancel#e' button:
4.5 Working with Rational Functions 173
x2 x 1
3 x2 2 x 1
The results are the same as if you had simplified the original expression using the Simplify com-
mand. The difference is that you know exactly how the simplification took place:
)
3 7x 8x2 5x3 x4
In[4]:= Simplify%
3 10x 18x2 14x3 3x4
1 x x2
Out[4]=
1 2 x 3 x2
This sort of interactive manipulation puts you in the driver’s seat. You will sharpen your algebraic
skills without falling into the abyss of tedium and silly mistakes (such as dropped minus signs) that
can occur when performing algebraic manipulations by hand.
A rational function and the function that results from its simplification are identical, except that the
original rational function will not be defined at those values of x that are roots of both the numera-
tor and denominator. In the example above, the original function is not defined at x 1 and
x 3, while the simplified function is defined at those points. For all other values of x the two
functions are identical.
from left to right. It can be an annoyance reading +3 x/ rather than +x 3/, but the former adheres
By default, Mathematica will always write a polynomial with ascending powers of x as you read it
to the ascending powers of x convention, and so that’s what you will get.
In[5]:= x3
Out[5]= 3 x
However, any expression produced by Mathematica can be displayed in several ways. Append //Tradig
tionalForm to any input, and it will be displayed using traditional notation (which, for a
polynomial, means descending powers of x):
In[6]:= x 3 ss TraditionalForm
Out[6]//TraditionalForm=
x3
You can also convert an output into TraditionalForm by selecting its cell bracket with your mouse,
and then choosing Cell # Convert To # TraditionalForm in the menus.
174 Algebra
Vertical Asymptotes
Roots of the denominator that are not also roots of the numerator will yield vertical asymptotes in
the graph of a rational function. Here, for example, is a function with vertical asymptotes at x 3
and x 3:
x4 3x3 x2 5x 4
In[7]:= k#xB' :
x2 9
200
150
100
Out[8]= 50
10 5 5 10
50
100
rational function and the polynomial q+x/ are asymptotic to each other as x gets large. Here’s
h+x/
how to get Mathematica to calculate the quotient and remainder:
x4 3x3 x2 5x 4
In[9]:= k#xB' :
x2 9
The commands Numerator and Denominator can be used to isolate the numerator and denomina-
tor of any fraction. You can then use these to find the quotient q+x/ and the remainder r+x/ with the
commands PolynomialQuotient and PolynomialRemainder:
Out[10]= 4 5 x x2 3 x3 x4
Out[11]= 9 x2
4.5 Working with Rational Functions 175
Out[12]= 8 3 x x2
Out[13]= 68 32 x
The commands PolynomialQuotient and PolynomialRemainder each take three arguments. The
first and second are polynomials representing the numerator and denominator of a rational func-
tion, respectively. The third is the name of the independent variable. In this example we have
computed that:
,8 3x x2 0
x4 3 x3 x2 5 x 4 6832x
x2 9 x2 9
You can check that Mathematica has done things correctly. The following computation accomplishes
this. Can you see why?
Out[14]= 4 5 x x2 3 x3 x4
Here is a plot of k together with the quotient polynomial, which in this case is a parabola. We see
that the graph of k is asymptotic to the parabola as x approaches :
300
200
Out[15]= 100
15 10 5 5 10 15
100
200
Partial Fractions
One final manipulation that is sometimes useful when working with rational functions is known as
partial fraction decomposition. It is a fact that every rational function can be expressed as a sum of
simpler rational functions, each of which has a denominator whose degree is minimal. The
Mathematica command that can accomplish this decomposition is called Apart:
176 Algebra
)
x4 3x3 x2 5x 4
In[16]:= Apart%
x2 9
82 14
3 +3 x/ 3 +3 x/
Out[16]= 8 3 x x2
The command that puts sums of rational expressions over a common denominator (i.e., the com-
mand that does what Apart undoes) is called Together. Both can be found in the AlgebraicManipula–
tion palette. If you take your mouse and highlight the output cell above, and then push the
Together#e' button, an input cell will be created that will look like this:
4 5 x x2 3 x3 x4
+3 x/+3 x/
Exercises 4.5
67 xx2 x3 x4
1. The rational function has no vertical asymptotes in its graph. Explain why.
2xx2
Simplifying Things
The Simplify command can handle all types of expressions as input. Any time you have a messy
expression, it won’t hurt to attempt a simplification. The worst that can happen is nothing; in such
cases the output will simply match the input:
Out[2]= 1 Tan#x'4
The Simplify command can also accept a second argument specifying the domain of any variable in
the expression to be simplified. For instance, consider the following example:
In[3]:= Simplify$Log$Æx ((
Out[3]= Log$Æx (
This seems odd; you may recall having been taught that the natural logarithm function and the
exponential function are inverses of one another—their composition should simply yield x. The
problem is that this is not necessarily true if x is a complex number, and Mathematica does not
preclude this possibility. To restrict the domain of x to the set of real numbers, do this:
Out[4]= x
The ± character can be read “is an element of. ” It can be found on the BasicMathInput palette (in
the same row with and ). This paradigm in which Simplify is called with a second argument
restricting the domain of one or more parameters is extremely useful. The second argument may
also be an inequality, such as x ! 0. In this case it is implied that x is a positive real number. That is,
including a variable in an inequality means it is not necessary to state that the variable is real.
In[5]:= Simplify% x2 )
Out[5]= x2
In[6]:= Simplify% x2 , x ! 0)
Out[6]= x
In[7]:= Simplify% x2 , x 0)
Out[7]= x
It is also possible to restrict variables to the set of integers. To learn about other choices for this
second argument, look up Assumptions in the Documentation Center.
Here is another example of how the Simplify command might be used. Note carefully the distinct
uses of := (for defining functions), m (for writing equations), and = (for assigning names):
. .81 6465 22
1s3 1s3
x 2 !,
1
2 2
3 .81 6465 2
Out[10]=
32s3
.1 Ç 3 2 . .81 6465 22
1s3 1s3
x .1 Ç 32 !,
1
2 2
3 .81 6465 2
2 32s3
.1 Ç 3 2 . .81 6465 22
1s3 1s3
x .1 Ç 32 !!
1
2 2
3 .81 6465 2
2 32s3
. .81 6465 22
1s3 1s3
1
2 2
3 .81 6465 2
Out[11]= 2
32s3
When you plug a root of a function into the function, you had better get zero:
In[12]:= f#realroot'
. .81 6465 22
1s3 1s3
1
2 2
3 .81 6465 2
Out[12]= 9 2 2
32s3
. .81 6465 22
1s3 1s3 3
1
2 2
3 .81 6465 2
2
32s3
Really?
In[13]:= Simplify#'
Out[13]= 0
That’s better.
4.6 Working with Other Expressions 179
The command FullSimplify works like Simplify, but it applies more transformations to the expres-
sion (and consequently it may take longer to execute). In certain instances, it will be able to reduce
an expression that Simplify cannot.
Of course we all know that identity. But what about this one?
Out[16]= Cos#D' Cos#E' Cos#J' Cos#J' Sin#D' Sin#E' Cos#E' Sin#D' Sin#J' Cos#D' Sin#E' Sin#J'
) Cos% )
D E D E
Out[17]= 2 Cos%
2 2 2 2
TrigExpand and TrigFactor are analogous to Expand and Factor, but they are designed to deal
with trigonometric expressions. TrigReduce will rewrite products and powers of trigonometric
functions in terms of trigonometric functions with more complicated arguments.
Any of the commands on the AlgebraicManipulation palette can be used in an interactive manner as
explained in the previous section, where a method for individually factoring the numerator and
denominator in a rational expression was discussed. Here’s another example:
Cos#x'2 2Cos#2x' 3
Highlight the first summand, cos2 +x/, with your mouse and push the TrigReduce#e' button on the
AlgebraicManipulation palette. The cell will then look like this:
180 Algebra
1
+1 Cos#2x'/ 2Cos#2x' 3
2
There is now clearly some combining of like terms that can occur. Do it in your head, or else high-
light the entire expression and push the Simplify#e' button. The cell will then look like this:
1
+7 5Cos#2x'/
2
You can keep manipulating an expression as much as you like. For instance, if you highlight the
entire expression and push the TrigExpand#e' button you will have this:
Here is an example where we will demonstrate why cos+S s 9/ is a root of the polynomial
f +x/8x3 6x 1. First we state the (rarely seen) triple angle formula for the cosine function:
Clear#a';
Cos#3a' ss TrigExpand
In[19]:=
Next, use a replacement rule to manually replace sin2 +a/ with 1 cos2 +a/:
cos- 1
S S 1 1
If a , then cos+3a/ . Hence we have 4 cos+a/3 3 cos+a/ . Multiply each side by 2,
and we see that indeed, cos- 1 is a root of the cubic 8x3 6x 1. Reduce confirms this:
9 3 2 2
S
9
x m Cos% )
S
Out[23]=
9
4.6 Working with Other Expressions 181
As useful as these commands are, it is important to realize that they are not a panacea. Most alge-
braic identities are at best difficult to uncover through blind application of the suite of commands
provided in the AlgebraicManipulation palette. Rather, as in the previous example, they are best used
when guided by a clear purpose. Here is another example. It is true that
arctan- 1 arctan- 1
S 1 1
4 2 3
yet no amount of manipulation of the right hand side using only the tools in the
S
AlgebraicManipulation palette will produce the value . How can these tools be used to explore, or to
4
uncover, such an identity? The answer is subtle. First, recognize that they are only tools. They must
be used carefully, with due deliberation and forethought. Owning a hammer doesn’t make one a
carpenter. That’s the bad news. The process is much like traditional pencil-and-paper mathematics
in that you pursue an idea and see if it bears fruit. The good news is that the pursuit is made less
tedious with Mathematica working for you.
Let’s explore the identity above. First, for sanity’s sake, let’s see if it can possibly be true:
ArcTan% ) ArcTan% ) ss N
1 1
In[24]:=
2 3
Out[24]= 0.785398
ss N
S
In[25]:=
4
Out[25]= 0.785398
Okay, it’s believable. Now can we derive a general formula, for which the above identity is but a
special case? As a first attempt, we might try commands such as TrigExpand, TrigFactor, and
TrigReduce on the expression ArcTan#a' ArcTan#b'. We find that none has any effect. For
instance:
Clear#a, b';
ArcTan#a' ArcTan#b' ss TrigExpand
In[26]:=
Now we are at a critical juncture in our investigation. We have made no progress, except to learn
that Mathematica does not appear to have the magic command that will provide us with the type of
formula we seek. It is at this point that we need to stop and think. What else might we try? Well,
what if we took the tangent of the expression ArcTan#a' ArcTan#b', then tried to expand that?
Believe it or not, this gets us somewhere:
182 Algebra
Clear#a, b';
Tan#ArcTan#a' ArcTan#b'' ss TrigExpand
In[28]:=
a
Out[29]=
2 2 1 ab
1a 1b
1a2 1b2 1a2 1b2
b
1 ab
1 a2 1 b2
1a2 1b2 1a2 1b2
In[30]:= ss Simplify
ab
Out[30]=
1ab
This tells us that
ab
tan+arctan+a/ arctan+b//
1a b
1
ab
arctan- arctan+a/ arctan+b/
1a b
1 1
It is a simple matter to see that when a and b , the left-hand side is equal to arctan+1/, which
2 3
S
is , and so this reduces to the identity mentioned previously. This formula is a generalization of
4
that identity. The final task is determining for which values of a and b the formula is valid. We leave
this task to the reader.
Exercises 4.6
1. Use TrigExpand to examine patterns in the nth angle formulas for the sine function, i.e., identi-
ties for sin+n x/.
3. Derive a quadruple angle formula for the cosine function, and use it to show that cos+S s 12/ is a
root of 16x4 16x2 1.
4.7 Solving General Equations 183
Often Solve and NSolve can be effectively used to solve equations involving powers that are rational
1
numbers. For instance, since raising a quantity to the power is the same as taking its square root,
2
equations involving square roots fall into this category:
.1 13 2
1
x
2
.1 13 2
Out[1]=
1
x
2
x0
Out[2]= 1
x
8
Solve and NSolve may be able to find solutions to simple equations with the variable appearing
inside a logarithm or as an exponent:
Out[3]= 101s200
Log% )
!
3
2
Out[4]=
Log#1 r'
Here we are warned that although one solution was found, there may be more. In fact, for this
example no other real solution exists, but we have no way of knowing that on the basis of this
output. Fortunately Reduce is effective here, working either over the complex numbers (which it
does by default), or over the reals where we see that the solution above is indeed unique:
184 Algebra
2 Ç S C#1' Log% )
3
2
Out[5]= C#1' ± Integers && 1 r 0 && Log#1 r' 0 && x m
Log#1 r'
Log% )
3
2
Out[6]= Log#1 r' 0 && r ! 1 && x m
Log#1 r'
On occasion these solving commands may come up empty, even when a unique solution exists:
In[7]:= Solve$Sin#x' m 2 x2 , x(
Solve::tdep :
The equations appear to involve the variables to be solved for in an essentially nonalgebraic way. j
Out[7]= Solve$Sin#x' m 2 x2 , x(
In[8]:= NSolve$Sin#x' m 2 x2 , x(
Solve::tdep :
The equations appear to involve the variables to be solved for in an essentially nonalgebraic way. j
Out[8]= NSolve$Sin#x' m 2 x2 , x(
In[9]:= Reduce$Sin#x' m 2 x2 , x(
Reduce::nsmet : This system cannot be solved with the methods available to Reduce. j
Out[9]= Reduce$Sin#x' m 2 x2 , x(
There are powerful numerical techniques for approximating solutions to equations such as this.
FindRoot is the final equation-solving command that we introduce. It is your last line of defense. It
is very robust, and adapts its methodology according to the problem it is fed. It’s tenacious, but it’s
also old-school. To use it you must give it a numerical value as a starting point. Like a hound dog it
will hunt down a single solution from this starting point, iteratively using its current position to
zero in on it. It is likely to hone in to the solution nearest the starting point, so choosing a good
starting point is key. A plot is helpful in this endeavor. A simple approach is to Plot two functions,
the left side and right side of the equation you wish to solve. The solutions are the x coordinates of
the points where the curves intersect. For instance, here’s a view of the functions in the equation
above:
4.7 Solving General Equations 185
Out[10]=
2 1 1 2
1
2
We expect solutions near x 1.6 and x 1. To zero in on a solution once you know roughly where
it is, use the FindRoot command like this:
Out[11]= x 1.72847
Out[12]= x 1.06155
The first argument of the FindRoot command is an equation, the second is a list whose first mem-
ber is the variable to be solved for, and whose second member is a rough guess at the true root. To
have all internal calculations performed with n-digit precision you can use the optional argument
WorkingPrecision:
Out[13]= 1.06154977463138382560203340351989934205887417838924148608498893580932536g
58078013681605147722169795200205523517584438182489915752386795185105198g
01898497141789694624781317887368590739943328390244768652889979635131820g
54066331171612084604692146632416602626438286949734162187208102212531109g
55046026055069360793013098705252533458512558323397412062383035427145357g
98284624484729386618537019854165883676711994
This technique of first estimating a solution with a plot and then using FindRoot to zero in on it is
very robust in that it will work on almost any equation you wish to solve (provided that a solution
exists). It does have several drawbacks, however. First, it is a strictly numerical command; it cannot
be used when there are more variables than there are equations. For instance, it won’t be able to
solve x y 1 for x; the solution must a number (or a list of numbers if there are several equations).
Second, it may be tedious to find an appropriate domain for a plot, one in which a point of intersec-
tion resides. Third, it is often difficult to discern whether or not other intersection points might be
present to the left or right of those you have already found. And finally, for some equations the
algorithm will fail altogether. For instance, FindRoot relies at times on a well-known algorithm, the
Newton-Raphson method, to produce its solutions. It is also well known that this method doesn’t
work for all combinations of equations and initial guesses, even when a unique solution exists:
186 Algebra
Out[14]= x 650.407
The output here is not correct (so the warning message is welcome). You can usually avoid this sort
of thing if you make a reasonable initial guess. This sort of problem is unlikely if you follow our
advice and make a few plots first, using the plots to generate reasonable initial guesses for FindRoot.
400
350
Out[15]= 300
250
2 4 6 8 10 12 14
Out[16]= x 8.31039
In[17]:= Solve$Log#x' m x3 x, x(
Solve::tdep :
The equations appear to involve the variables to be solved for in an essentially nonalgebraic way. j
Out[17]= Solve$Log#x' m x x3 , x(
In[18]:= NSolve$Log#x' m x3 x, x(
Solve::tdep :
The equations appear to involve the variables to be solved for in an essentially nonalgebraic way. j
Out[18]= NSolve$Log#x' m x x3 , x(
In[19]:= Reduce$Log#x' m x3 x, x(
Reduce::nsmet : This system cannot be solved with the methods available to Reduce. j
Out[19]= Reduce$Log#x' m x x3 , x(
4.7 Solving General Equations 187
You shouldn’t allow these outputs to let you to give up your hunt for a solution. Solve, NSolve, and
Reduce may be unable to find a solution but that doesn’t mean one does not exist. In your dealings
with computers, you should live by the maxim made famous by Ronald Reagan: “Trust, but verify.”
In that spirit, we endeavor to make a plot and see what is going on:
600
500
400
Out[20]= 300
200
100
2 4 6 8 10
One of the functions is barely visible. It is not uncommon for one function to dwarf another when
viewed over certain domains. To make things clearer we render the Log function thick, dashed, and
blue, and the cubic red.
600
500
400
Out[21]= 300
200
100
2 4 6 8 10
Suspicion confirmed: the Log function is quite flat compared to the cubic on this domain. Perhaps
they intersect over on the left, somewhere between 0 and 2. Try again: edit the iterator above,
specifying the new plot domain, and reenter the cell:
1
Out[22]=
0.5 1.0 1.5 2.0
1
2
Out[23]= x 0.699661
Out[24]= x 1.
Is x 1 an exact solution? Yes: plug it in by hand to the original equation, or do this:
In[25]:= Log#x' m x3 x s. x 1
Out[25]= True
It is worth noting that you can manipulate any equation into the form expressionm0 simply by
subtracting the original quantity on the right from each side of the equation. Solving the resulting
equation is then a matter of finding the roots of expression. The obvious advantages to this approach
is that the roots are easy to read off of a plot, since they fall directly on the labeled x axis. Here is the
graph for the last example when presented this way. Note that we still plot two functions, one of
which is the x-axis. This guarantees that the x-axis will be included in the output graphic, even if
you are unlucky enough to choose a domain on which there are no solutions.
1
Out[26]= 2
3
The roots, of course, are the same as the solutions we found earlier. FindRoot will report the same
output regardless of whether you input the equation Log#x' m x x3 or the equation
x x3 Log#x' m 0. In fact you can simply use x x3 Log#x' (and forgo the m 0) when using
FindRoot. The name “FindRoot” makes good sense in this light. You can use whichever approach
seems easier to you.
Exercises 4.7
1. Approximate the solutions to the equation 1 x2 sin x.
2
2. Approximate three of the solutions to the equation Æx sin x.
2
3. Is it true that ÆS sin S?
4.8 Solving Difference Equations 189
If no initial conditions are given (so the first argument is a difference equation and nothing else),
one or more constant terms may be generated, as seen above. C[1] represents the constant in the
output above. Any specific real or complex numerical value for this constant value will give, accord-
ing to the previous output, a solution to the difference equation. The first argument may also be a
value a#0' 1 s 3.
list of equations. For instance, here we solve the same difference equation, but also specify the initial
a#n' !!
2n
Out[3]=
3
RSolve is most useful when dealing with somewhat more complicated difference equations. Here we
ask it to solve the difference equation that defines the Fibonacci numbers:
In[4]:= RSolve#a#n' m a#n 1' a#n 2', a#1' m 1, a#2' m 1, a#n', n'
And here’s an example of a logistic growth equation that RSolve can handle:
190 Algebra
1
In[7]:= RSolve%a#n 1' m 2a#n' a#n'2 , a#n', n)
3
a#n' 3 3 Æ2
n
C#1'
Out[7]=
Adding an initial condition forces it to use Solve internally, and in this case it is not able to
determine if it has found a unique solution. We look at the first few values as a check:
1
In[8]:= RSolve%a#n 1' m 2a#n' a#n'2 , a#0' m 1!, a#n', n)
3
Solve::ifun : Inverse functions are being used by Solve, so some
solutions may not be found; use Reduce for complete solution information. j
1, !
5 65 6305
Out[9]= , ,
3 27 2187
We can also look at the solution to the difference equation without the initial condition (three
outputs ago), and do the solving ourselves:
c m Log% )
3
Out[10]=
2
3.0
2.5
Out[11]= 2.0
1.5
1 2 3 4 5
Moreover, using NestList to generate the first few terms of the sequence generated by the original
difference equation, we see that it agrees with our solution. The use of NestList to generate terms of
a sequence defined by a difference equation is discussed at the end of Section 3.13 on page 146.
4.9 Solving Systems of Equations 191
1
In[12]:= NestList%2Ó Ó2 &, 1, 3)
3
1, !
5 65 6305
Out[12]= , ,
3 27 2187
1, !
5 65 6305
Out[13]= , ,
3 27 2187
¿ At times it may be helpful to note that the second argument to RSolve can be simply the
symbol a (rather than a[n]), and the output will be a pure function expression for a. Pure
functions are discussed in Section 8.4.
Exercises 4.8
1. Suppose a $30,000 car was purchased with no money down, using a five-year loan with an
annual interest rate of 7%, compounded monthly. This means that each month interest is
.07
compounded at the monthly rate of , while the principle is reduced by the amount p of
12
the monthly payment.
a. Calculate the monthly payment.
b. Make a table breaking down each payment as principle and interest for the 60 month loan
period.
2. Suppose that the value of a new automobile is $30,000, and that it loses 10% of its value each
year. That is, at the end of each year it is worth only 90% of what it was worth at the beginning
of that year. When will it be worth $8,000?
x !!
31 595 1047
Out[1]= ,y
283 283
You can leave out the second argument entirely if you want to solve for all the variables appearing
in the equations:
x !!
31 595 1047
Out[2]= ,y
283 283
You can easily use generic coefficients to generate a general formula for solving similar systems:
Clear$a, b, c, d, e, f, x, y(;
Solve$a
x b
y m c, d
x e
y m f , x, y (
In[3]:=
x !!
c e b f c d a f
Out[4]= ,y
b d a e bdae
The Solve command works very well for linear equations (like those above). It also does a good job
with systems of polynomials. Here is an example showing the points of intersection of a circle and a
parabola:
0
Out[5]=
1
2
2 1 0 1 2
It turns out that there are two real solutions (you can see them on the plot) and two complex ones.
One of the real ones is the point (2, 0). The other is:
4.9 Solving Systems of Equations 193
.28 3 87 2 .28 3 87 2
1s3 2s3
2 1 2 2 1
y 2s3
1s3
3 9 9
9 283 87 9 283 87
.28 3 87 2
Out[6]=
1s3
1 1
x 2 1s3
3
283 87
We won’t list the complex solutions, as they’re even nastier. Here’s another example:
Out[7]= 1
1
2 1 0 1 2
This output, while potentially useful, may make you cringe. In such situations, applying N to the
result will often provide a good numerical approximation:
Similarly, NSolve can be used, but it will report both real and complex solutions.
194 Algebra
Just as there are single equations that can foil the Reduce, Solve and NSolve commands, there are
systems of equations that can as well. In such situations one can use FindRoot to approximate a
solution. Give as the first argument the list of equations. Follow that with an additional argument
for each variable. Each of these arguments is of the form {variable,guess}, where the guess is your best
estimate of the actual value for that variable. Use a plot to help you make your guess:
Exercises 4.9
1. Use a graph to estimate the solutions to the system of equations y x2 and y 4sin+x/. Then find
a command that will find or approximate the real valued solutions.
5
Calculus
0.8
0.6
Out[4]= 0.4
0.2
10 5 5 10
0.2
10
Out[5]=
1.0 0.5 0.5 1.0
5
10
196 Calculus
investigations. The function f +x/ assumes values that approach 1 as x approaches 0. The function g
The two outcomes are strikingly different, and they illustrate the likely possibilities for similar
has a vertical asymptote at x 0; as x approaches 0 from the right, g assumes values that approach
, while as x approaches 0 from the left, g assumes values that approach .
We can check this numerically by making a table of values. Here is a table of values for f as x
approaches 0 from the right:
x f +x/
0.1 0.998334166468282
0.01 0.999983333416666
Out[6]=
0.001 0.999999833333342
0.0001 0.999999998333333
0.00001 0.999999999983333
The Limit command provides an easy way to investigate the behavior of functions as the indepen-
dent variable approaches some particular value (such as 0):
Out[7]= 1
In[8]:= Limit$g#x', x 0(
Out[8]=
The first argument to the Limit command is the expression for which you wish to find a limiting
value. The second argument (x 0 in these examples) specifies the independent variable and the
value which it will approach. You may use the symbol from the BasicMathInput palette or the
keyboard equivalent -> by hitting the “minus” sign followed by the “greater than” sign.
It is important to note that the Limit command by default computes one-sided limits, and these are
limits from the right. That is, the expression is examined with x values chosen slightly to the right of
the value that x approaches. In the limit for g as x 0, for instance, the output was . You can take
limits from the left by adding the option Direction 1. You can think of this as the direction in
which you need to move on a number line to get to the number 1 from the origin.
Out[9]=
In a strictly mathematical sense, a limit exists if and only if the limits from the left and right agree.
So the limit of the function g as x approaches 0 does not exist since the limit from the right is
while the limit from the left is . In Mathematica, the Limit command defaults to the limit from
the right to increase the likelihood of being able to find a limiting value. It is crucial to check that
5.1 Computing Limits 197
the limit from the left matches the limit from the right before concluding that a limit exists. A plot
is usually helpful in this regard. The only exception to this convention is a limit as x approaches
infinity (where the Limit command will by default compute limits from the left).
In[10]:= Limit$g#x', x (
Out[10]= 0
Taking another glance at the graph of g , you can see that as the value of x gets large, the value of
g+x/ approaches 0. A table of values is also useful in this regard:
x g+x/
10. 0.1
100. 0.01
Out[11]=
1000. 0.001
10 000. 0.0001
100 000. 0.00001
Note that you may use the symbol from the BasicMathInput palette, or type Infinity. Note also
that some functions will not have one-sided limits:
The output here indicates that the sine function assumes values in the interval from 1 to 1 without
approaching a single limiting value as x approaches infinity. This is consistent with our knowledge
of the sine function; a plot provides additional confirmation:
0.5
Out[13]=
5 10 15 20 25 30
0.5
1.0
Piecewise functions provide the standard examples of functions for which the left and right direc-
tional limits differ.
198 Calculus
0.8
0.6
Out[14]=
0.4
0.2
, x 0, Direction 1 )
x2 x 1 x 0
In[15]:= Limit%
Sin#x' x!0
Out[15]= 1
, x 0, Direction 1 )
x2 x 1 x 0
In[16]:= Limit%
Sin#x' x!0
Out[16]= 0
In some cases there may be one or more parameters appearing in an expression other than the one
whose limiting value we wish to determine. For instance, what happens to an expression of the form
1xn
as x 0?
n
1 xn
In[21]:= Limit% , x 0, Assumptions n 0)
n
Out[21]=
Exercises 5.1
6x 12 6x 12
1. Use the Limit command to find limx2 and limx2 . Then use a graph to explain
x2 4 x2 4
these answers.
use your graph to predict limx x sin- 1. Use the Limit command to confirm your answer.
x
b
x
3. The function f +x/ x2 2x 3 x has very different behavior as x approaches and x
approaches . Use the Table command to examine this function for large positive and nega-
tive values of x.
In[3]:= h#xB' : x3 ;
diffquot#h'
x3 +x 'x/3
Out[4]=
'x
200 Calculus
You can now simplify it by typing and entering Simplify#', but it’s much more fun (and informa-
tive) to “drive” Mathematica through it step by step. First use your mouse to highlight +x 'x/3 in
the last output, and then hit the Expand#e' button on the AlgebraicManipulation palette. You will then
have this:
3 x2 3 x 'x 'x2
That’s it! You’ve just simplified an algebraic expression painlessly, with no dropped minus signs,
and without skipping a step. We encourage you to do this for five or six functions of your choosing;
you might even find it fun.
In[10]:= diffquot#f'
Sin#S x' Sin#S +x'x/'
x x'x
Out[10]=
'x
You can find the average rate of change of f from x 2 to x 2.5 as follows:
Out[11]= 0.8
Recall from the last chapter (Section 4.2, page 153) that the replacement rule /.{x 2, 'x 0.5}
instructs Mathematica to replace x by 2 and 'x by 0.5. The average rate of change of f from x 2 to
x 2.1 is:
5.2 Working with Difference Quotients 201
Out[12]= 1.47151
Here is a table of values for the difference quotient of f at x 2 for various small values of 'x:
diffquot#f' s. x 2
Sin#S +2 'x/'
In[15]:=
'x +2 'x/
Out[15]=
Out[17]= 1.570796327
Note that this result is consistent with the table that we computed above.
Exercises 5.2
1. Find the difference quotient for f +x/
1
and use the AlgebraicManipulation palette to simplify
x2
this expression.
202 Calculus
3. Find the difference quotient for f +x/ x and use the AlgebraicManipulation palette to simplify
this expression.
In[1]:= f '#x'
S Cos#S x' Sin#S x'
Out[1]=
x x2
If you check this using the quotient rule, your answer may look slightly different. You can simplify
the output above by highlighting it and pushing the Simplify#e' button on the AlgebraicManipulation
palette, or by typing:
In[2]:= Simplify#'
S x Cos#S x' Sin#S x'
Out[2]=
x2
This is exactly what you would obtain if you worked by hand using the quotient rule. We can
evaluate the derivative at any value of x:
In[3]:= f '#2'
S
Out[3]=
2
A plot of a function and the tangent line to the function at a point (at x 2, for example) can be
for a line, where the point on the line is ,2, f +2/0, and the slope of the line is f '+2/. You can zoom in
produced as follows. The expression representing the line is obtained from the point-slope formula
(or out) by changing the bounds on the iterator. Try x, 1.9, 2.1 to zoom in on the two graphs near
x 2.
5.3 The Derivative 203
0.5
Out[4]=
1.8 2.0 2.2 2.4
0.5
You may also find it instructive to study the graph of a function and its derivative on the same set
of axes. Here the graph of f is black, while its derivative is gray. Of course you can use your favorite
colors.
3
2
1
There is another way to take derivatives of expressions with Mathematica that is useful in many
situations. The command is called D, and it takes two arguments; the first is an expression to be
differentiated, and the second is name of the variable with respect to which the differentiation is to
be performed:
Sin#S x'
In[6]:= D% , x)
x
S Cos#S x' Sin#S x'
Out[6]=
x x2
A palette version of the D command exists and is sometimes useful. Go to the BasicMathInput
palette, and find the f e button. Type and highlight the expression you wish to differentiate, then
push this button. Now type x (as the subscript) to indicate that you wish to differentiate with
respect to x:
In[7]:= x Sin#x'
Out[7]= Cos#x'
The palette approach is most useful when the expression you wish to differentiate already exists on
your screen (as the output of some former computation, for instance). You can then highlight it and
204 Calculus
A word of warning regarding the palette button is in order. If you first hit the palette button and
then enter an expression to be differentiated in the position of the placeholder, you should put
grouping parentheses around the expression. Here’s an example of what can happen if you don’t:
In[8]:= x x2 x3
Out[8]= 2 x x3
You certainly don’t want to report that the derivative of x2 x3 is 2x x3 ! With parentheses things
are fine:
In[9]:= x ,x2 x3 0
Out[9]= 2 x 3 x2
When you first highlight the expression to be differentiated, and then push the palette button,
Mathematica will add the grouping parentheses automatically.
D can be used to easily derive just about any differentiation rule. You just need to ask it to derive an
expression involving “dummy” functions (functions which have been given no specific definition).
Here is the product rule, for instance:
There are two points to remember about the D command. First, it is imperative that the variable (x
in the example above) be cleared of any value before it is used in the D command. Second, if you
plan to plot a derivative generated by the D command, you need to wrap it in the Evaluate com-
mand before plotting:
Out[12]=
1.0 0.5 0.5 1.0
1
2
As a general rule of thumb, D is useful for differentiating unnamed expressions and for deriving
general formulae. For functions to which you have already given names (such as f ), the “prime”
command f'[x] is generally easier to use than D.
5.4 Visualizing Derivatives 205
Exercises 5.3
1. Make a Manipulate that shows the tangent line to f +x/ cos+x/ at the point x a as a assumes
values from 4S to 4 S.
2. Graph the derivatives of f +x/ sin+xn / for n 2, 3, 4 and look for patterns in the graphs that are
reflected in the expressions for their derivatives.
'x
1.0
0.5
Out[1]=
6 4 2 2 4 6
0.5
1.0
ized by the following Manipulate. Here we see the graph of f +x/ Æx and its tangent line at x 1.
The equivalence of the instantaneous rate of change and the slope of the tangent line can be visual-
As we move the slider we zoom down to the microscopic level where the curve and the tangent line
become indistinguishable.
206 Calculus
zoom
Out[2]=
Exercises 5.4
1. Make a Manipulate like the first one in this section, showing the difference quotient for the
1
natural logarithm function ln+x/ converging to as 'x 0.
x
2. Modify the zooming Manipulate at the end of this section so that it includes a Checkbox
control. The tangent line should display if the checkbox is checked, but not otherwise.
In[3]:= f ''#x'
Third derivatives?
In[4]:= f '''#x'
The D command is useful for producing general formulae as in the last section. For example, here is
the (seldom seen) second-derivative product rule:
In[6]:= Clear$f, g, x(
derivative of f[x] is f +3/ #x'. A similar notation is employed for all derivatives beyond the second.
And here is a product rule for third derivatives. Note that the StandardForm notation for the third
Out[8]= 3 g #x' f #x' 3 f #x' g #x' g#x' f +3/ #x' f#x' g+3/ #x'
Exercises 5.5
f +x/
1. Use the D command to find a general rule for the second derivative of y .
g+x/
2. Use a Table to look for patterns in the higher order derivatives of Sec+x/.
208 Calculus
Out[3]= xm 3 «« x m 3
Out[4]= x 3 !, x 3 !!
Recall from Section 4.2 on page 149 that the Solve command returns a list of replacement rules. Here
is how to use that output to get a list of the two critical points, each of the form x, f#x'. These are
the points in the plane where the graph of f assumes its extreme values:
And here is a plot of f , with the extreme points superimposed as large dots. They will appear as large
red dots on a color monitor:
30
20
10
Out[6]=
4 2 2 4
10
20
The Epilog option can be used with any command that produces graphics, such as Plot. It allows
you to overlay “graphics primitives,” such as points, on the graphic after it has been rendered. In
this case, the directive PointSize[.02] makes the points big (they are each 2% of the width of the
5.6 Maxima and Minima 209
graphic), the directive Red makes them red, and the Point[extrema] transforms the list of coordi-
nate pairs into a Graphics primitive Point object.
In any event, that little bit of technical typing produces a satisfying plot, and allows you to verify
visually that the points you found using the Solve command are really the extrema you sought.
We can confirm that an extreme point is a maximum or a minimum by using the second derivative:
Out[7]= True
Out[8]= True
Returning to the task at hand, the strategy that we followed above will fail precisely when the Solve
(or NSolve) command is unable to solve the equation f '+x/ 0, typically when f is something other
than a polynomial of low degree:
Sin#S x'
In[9]:= f#xB' :
x
This is a clue that you need to follow an alternate strategy. One approach is to stare hard at the
equation f '+x/ 0 and see if you can find a solution by hand. There are rare occasions in which
there is an obvious solution that Mathematica will miss (there’s an example at the end of this sec-
tion). Try a few values of x, such as 0 or 1, and see if they work. If the Solve (or NSolve) command
does produce a solution, but warns you that inverse functions were used, work by hand to see if you
210 Calculus
can find other solutions. Bear in mind that this process of finding extrema cannot be reduced to a
single, simple, automated procedure; you have to remain fully engaged at every step. If your efforts
in solving f '+x/ 0 bear no fruit (as will probably be the case with the example above), don’t
despair. In such cases we resort to attacking the extreme points one at a time, using Plot and Findg
Root, and settle for approximations to the actual extreme points.
The first step in this strategy is to produce a graph of f . In this example, we’ll look at the graph of f
between x 0 and x 3. If you are working on an applied problem, there is probably some specified
domain. That would be a good choice for your plot.
There appears to be a relative minimum near x 1.5. Use that as an initial guess, and let FindRoot
do the rest:
Out[13]= x 1.4303
The coordinates of the relative minimum can be easily recovered using replacement rules:
Note that this is an approximate, rather than an exact solution. This is the best that Mathematica can
do in such situations. Note also that when you use FindRoot, you can almost always get an answer,
but you have to settle for one solution at a time. If you need to find six extreme points, you need to
run FindRoot six times, each with a different initial guess (suggested by a plot).
Here’s how to produce a plot with the relative minimum shown. It will appear as a red dot on a
color monitor:
Be mindful that a plot of some sort is important. For although relative extrema for a function f must
occur at values of x that satisfy f '+x/ 0, satisfying this equation is no guarantee that the point in
question is in fact a relative maximum or minimum. This will often happen when the equation
f '+x/ 0 has repeated roots:
16.5
16.0
Out[18]=
15.5
ately to the left of 2 to values smaller than f +2/, and f takes points immediately to the right of 2 to
The plot suggests that even though f has a horizontal tangent when x 2, f takes points immedi-
values greater than f +2/. In other words, f has no relative maximum or minimum at x 2. Without a
plot (or some careful mathematical reasoning) it is unclear whether a function f has extrema at
those values of x satisfying f '+x/ 0. Note that the second derivative confirms that the function is
neither concave up nor concave down at x 2:
In[19]:= f ''#2'
Out[19]= 0
We next give an example of a function f for which Mathematica’s Solve command cannot produce a
real solution for the equation f '+x/ 0, but for which Reduce can. An exact solution can also be
found by hand.
In[21]:= f '#x'
Solve is likely to fail when the function in question is a transcendental function without an inverse.
We know from the expression for f that it will oscillate, and hence will have infinitely many local
extrema. In fact, a moment’s thought reveals that since the cosine function attains its extreme
values when its argument is an integer multiple of S, this function will attain its extreme values
when x is the natural logarithm of an integer. This is precisely what Reduce tells us, provided we
instruct it to restrict x to the field of real numbers. It distinguishes between the cases when the
integer is odd and even. This is a remnant of the more general case when x is permitted to assume
complex values, where this distinction is necessary; it also neatly divides the solutions between
maxima and minima:
Out[23]= C#1' ± Integers && ,,C#1' 1 && x m Log#2 C#1''0 «« ,C#1' 0 && x m Log#1 2 C#1''00
Recall from Chapter 4 that C[1] ± Integers means that C[1] is an element of the set of integers. The
symbol && means “and” and the symbol || means “or.” Note that Reduce finds every solution. The
output may seem a bit difficult to read, but this is necessary. After all, there are infinitely many
solutions; a careful description is necessary. In this case, however, a more concise description is
possible: every solution to the equation f '+x/ 0 is of the form x ln+c/ where c is a positive integer.
A plot confirms this:
1.0
0.5
Out[24]=
3 2 1 1 2 3
0.5
1.0
The moral of this example is that the process of finding extrema is not one that can be completely
automated. Rather, you must have a clear grasp of the underlying mathematical ideas, and the
flexibility to combine abstract mathematical thinking with the tools that Mathematica provides.
That said, however, Mathematica does have two built-in commands that go a long way toward
automating the process of finding extrema. The commands are called Maximize and Minimize. But
5.6 Maxima and Minima 213
be careful, these commands will return only global (a.k.a. absolute) minima and maxima, not local
extrema. For example, recall the first function discussed in this section.
We saw in the graph that this function does not attain a global maximum on its full domain. But we
can add constraints on the values to be considered by Maximize as follows:
Out[28]= 5 6 3 , x 3 !!
The output is a list of two items. The first gives the maximum value of the function, while the
second gives a replacement rule that indicates the point in the domain where the maximum occurs.
On the same interval the minimum occurs at the left endpoint.
Constraints can be inequalities or equations. When entering equations be sure you use double
equals (m). These commands can be very useful when you want to find global extrema. Be warned
tions (this is the case for the previous example where f +x/ cos+S Æx /; see Exercise 1). But for polyno-
that in the case of finding extreme values for transcendental functions, they may not find all solu-
mial equations, they are bulletproof. For instance, here is an optimization word problem of the type
frequently encountered in a calculus course:
A rectangular field is to be enclosed by a fence on three sides and by a straight stream on the fourth
side. Find the dimensions of the field with maximum area that can be enclosed with 1000 ft of fence.
It is always a good idea to check any solution found with Maximize or Minimize by looking at a
graph. As always, don’t trust—verify! Here is a somewhat intricate Manipulate that provides a
graphical confirmation by showing the rectangular field on the left and its area on the right:
214 Calculus
In[31]:= Manipulate$Module$y ,
y : 1000 2 x;
GraphicsRow$
Plot$y, x, 0, 500, AspectRatio .5,
Epilog Gray, EdgeForm#Black', Polygon$0, 0, x, 0, x, y , 0, y ( ,
AxesLabel "x", "y" , Ticks 0, 250, 500, Automatic(,
Plot$x y, x, 0, 500, AspectRatio .5,
Epilog PointSize#.04', Red, Point$x, x y ( ,
AxesLabel "x", "Area of Rectangle" ( , ImageSize 400((,
x, 250, 0, 500(
y Area of Rectangle
1000
120 000
60 000
400
40 000
200 20 000
x x
250 500 100 200 300 400 500
¿ A Module was used in the Manipulate above. While not strictly necessary, it is convenient. If
the line beginning with Module were eliminated (along with its closing square bracket on the
second-to-last input line), one would simply have to replace all occurrences of y with
1000 2 x. In fact, that was how we first made the Manipulate. But it was annoying having to
type 1000 2 x in five different places, and it made the code more difficult to read. Module is a
“scoping construct”; it allows one to define local variables. It is much like the command With,
(first seen in Section 3.2) but it is more flexible in that delayed assignments can be made. The
first argument is a list of all such variables (here there is only one, namely y). Then y is defined
using the SetDelayed operator :=. That’s it. The rest is simply a GraphicsRow containing the
two plots. Outside of this Module, the symbol y has no assigned value. Note also that switch-
ing to a different example, such as y 1500 3x, would mean only having to change one line
of code. Module is discussed in Section 8.6.
Exercises 5.6
1. Read the example concerning the function f +x/ cos+S Æx / in this section. Use Maximize to find
extreme values of f on the domain 3 x 3. Compare the output to the graph in the text, and
comment on what you find.
2. Use Minimize to find the x-coordinate of the third occurrence of the y-value 1 for the function
5.7 Inflection Points 215
3. Find the dimensions of the right circular cylinder of largest volume that can be inscribed in a
right circular cone with radius r and height h.
Out[3]=
0.4
0.2
0.4
0.6
You may find it instructive to study the graph of a function and its derivatives on the same set of
axes. Here is the graph of f , and the graph of its derivative with filling added. Note that f is decreas-
ing on those intervals where f ' is negative, and increasing when f ' is positive. The zeros of f ' corre-
spond to the relative extrema of f .
216 Calculus
Out[6]=
And below we see f and f '' plotted together, where f '' has filling added. Note that f is concave down
on those intervals where f '' is negative, and concave up where f '' is positive. The zeros of f '' corre-
spond to the inflection points of f .
Out[7]=
Exercises 5.7
1. Plot the function f +x/
sin+Sx/
on the domain 3 x 3, then make a ContourPlot of the curve
x
f '+x/ m 0, and superimpose the two graphics using Show. Describe what you see.
3. Repeat the first two exercises, but modify the ContourPlot input so that the first argument is not
an equation but instead simply f'[x] or f''[x]. Add the option settings Contours {0} and Colorg
Function "LightTerrain". This will have the effect of adding color to the vertical bands.
Within the Show command, list the ContourPlot first (otherwise the colored bands will be on
top of, and hence obscure, the Plot of f ).
5.8 Implicit Differentiation 217
for the implicit equation cos,x2 0 sin,y2 0. First, rewrite the equation with
dy
Here’s how to find
dx
cos,x2 0 sin,y2 0
every nonzero term on the left-hand side, so that it is of the form expression 0. In this case we get
0. The key to implicit differentiation is to tell Mathematica that y is to be regarded
as a function of x. This is accomplished by typing y[x] in place of y. We can now differentiate the
expression on the left-hand side with respect to x. For convenience, we name it lhs:
It is important to remember that this derivative is equal to the derivative of 0 (the right-hand side of
dy
our implicit equation), which is also 0. We can get an expression for by solving this equation:
dx
x Sec$y#x'2 ( Sin$x2 (
Out[3]= y
#x' !!
y#x'
dy x sec,y2 0 sin,x2 0
In traditional notation we replace y#x' by y, yielding .
dx y
In some instances, you may be asked to differentiate an equation such as cos,x2 0 sin,y2 0 with
respect to a third variable, such as t . In this case we assume that each of x and y are functions of t ,
and type x[t] and y[t] in place of x and y, respectively. The differentiation is carried out with respect
to t :
Exercises 5.8
for the implicitly defined function cos,x2 0 sin,y2 0.
dx
1. Find
dy
d2 y
2. Find for the implicitly defined function x y y2 3 as a function of x and y.
d x2
3. A ten foot ladder is leaning against a wall. The base of the ladder is sliding away from the wall at
3 feet per second when it is one foot from the wall. How fast is the top of the ladder sliding
down the wall?
4. Use Manipulate to make a movie of a tangent line rolling around a unit circle.
The DSolve command takes three arguments. The first is a differential equation, an equation that
includes a derivative. The second is the function whose algebraic expression you wish to find, and
the third is the name of the independent variable. The second and third arguments appear redun-
dant in an example like this one, but in more complex situations they are needed to avoid ambigu-
ity. In any event, you need to use them, always.
The output to DSolve is a list of replacement rules, exactly like those produced by the Solve com-
mand (see Section 4.2 in the previous chapter for a detailed description of the Solve command and
replacement rules). The C[1] in the output represents a constant. It can be replaced by any number
to produce an explicit solution. In applied settings, some other information is usually given that will
enable you to find the value of such a constant. For instance, if we use our population growth
model, we might have been told that initially, at time t 0, the population was 400. Then we see
that 400 y+0/ Æ0 C#1' C#1'. Thus we conclude that the algebraic expression for y+t/ is
y+t/ 400Æts3 .
You can also use DSolve by giving a list of equations as the first argument. You can, for instance,
put the differential equation and an initial condition in the list. This makes life very easy indeed:
10 000
8000
6000
Out[6]=
4000
2000
2 4 6 8 10
Here we have a family of solutions, with individual solutions determined by the values of the
220 Calculus
constant C[1]. For instance, here are several solutions for values of C[1] ranging from 500 to 500 in
increments of 50:
Out[9]= 500 500 Æts5 , 500 450 Æts5 , 500 400 Æts5 , 500 350 Æts5 , 500 300 Æts5 ,
500 250 Æts5 , 500 200 Æts5 , 500 150 Æts5 , 500 100 Æts5 , 500 50 Æts5 , 500,
500 50 Æts5 , 500 100 Æts5 , 500 150 Æts5 , 500 200 Æts5 , 500 250 Æts5 ,
500 300 Æts5 , 500 350 Æts5 , 500 400 Æts5 , 500 450 Æts5 , 500 500 Æts5
We now have a list consisting of twenty-one functions, each a solution of our differential equation,
and each corresponding to a different numerical value of C[1]. Let’s plot these functions on the
same set of axes.
600
500
Out[10]=
400
300
2 4 6 8 10 12 14
Just as there is the NSolve command to complement the Solve command, there is the NDSolve
command to complement the DSolve command. Use NDSolve in situations where the DSolve
command is unable to provide an exact algebraic solution (or if DSolve seems to be taking all day).
Choose Abort Evaluation in the Evaluation menu to get Mathematica to stop a computation.
To use NDSolve, you need to specify both a differential equation and an initial condition in a list for
the first argument. The second argument is the function to be solved for, as with DSolve. The third
argument is an iterator, specifying the name of the independent variable and the range of values it is
to assume. As for the output, you will not get an explicit algebraic formula—only DSolve can
provide that. Rather, you get a nebulous object known as an interpolating function. It is a numerical
approximation to the true solution of the differential equation on the specified domain. It behaves
like an ordinary function in that it can be plotted, and can be used in calculations:
In[11]:= sol NDSolve$y '#t' m 0.05y#t' 0.0001y#t'2 , y#0' m 10 , y#t', t, 0, 200(
400
300
Out[12]=
200
100
t y+t/
0 10.
20 26.2797
40 65.5185
60 145.367
80 263.509
Out[14]=
100 375.895
120 445.848
140 478.614
160 491.914
180 496.995
200 498.89
Exercises 5.9
dy
1. Find the general solution to the differential equation 4x y2 and then plot several solutions
dx
by choosing specific values of the constant.
d2 y dy
2. Find the general solution for the second order differential equation cos+x/ and then
dx2 dx
plot solutions for several values of the constants.
d2 y dy
3. Use NDSolve to find the solution of cos+x/ subject to the initial conditions y+0/ 1
dx2 dx
and y '+0/ .4 and then plot this solution.
222 Calculus
5.10 Integration
If you haven’t been impressed thus far, this is where Mathematica really pays for itself. Unlike
differentiation, which with perseverance can always be completed by hand, integration can be
exceedingly difficult. In most cases, however, if a function has a known antiderivative, Mathematica
can find it:
Ã
1
In[1]:= Å x
1 x3
)
12 x
Log$1 x x2 (
ArcTan%
3 1 1
Out[1]= Log#1 x'
3 6
3
1
The integration button can be found on the BasicMathInput palette. First type a function ( in the
1x3
example above), then highlight it with your mouse. Now press the à e Å f button on the BasicMathIn–
put palette. Your function will be pasted inside the integral at the position of the black square, and
the cursor will be at the second placeholder. Here you type the variable with respect to which the
integration will be performed (x in the example above). Now enter the cell.
If the function you wish to integrate is already on your screen (in an output cell for instance),
highlight it using the mouse, then push the integration button. It will be pasted inside the integral
in a new input cell. You then enter the variable with respect to which the integration is to be
performed and enter the cell.
Some people find it more natural to use the palette in a slightly different way, first pushing the
integral button, and then typing the function in the position delimited by the first placeholder. This
is okay, but be careful. If the expression you want to integrate is a sum, you need to put grouping
parentheses around the whole thing. Here’s what happens if you don’t:
à 1 x Šx
2
In[2]:=
Integrate::nodiffd :
à 1 cannot be interpreted. Integrals are entered in the form à f Å x, where Å is entered as ÈddÈ . j
You can probably make sense of this message: Mathematica sees the incomplete expression ¼ 1 from
which it is supposed to subtract x2 Å x. That’s nonsense. With the parentheses things work fine:
à ,1 x 0Šx
2
In[2]:=
x3
Out[2]= x
3
Note also that you must be sure to Clear any previous assignment made to the integration variable.
5.10 Integration 223
In[3]:= x 3
Out[3]= 3
In[4]:= Ã 2xÅ x
Out[4]= Ã 6Å 3
In[5]:= Clear#x'
In[6]:= Ã 2xÅ x
Out[6]= x2
You can produce the ¼ symbol without the palette by typing È int È, and you can produce the Å
symbol by typing È dd È. This will allow you to type an integral entirely from the keyboard.
Alternatively, you can use the Integrate command. It does the same thing as the palette button
described above; in fact the palette button provides a means of utilizing the standard syntax for
integrals, but when evaluated it simply calls the Integrate command. The standard Mathematica
syntax leaves no ambiguity as to the necessity of grouping parentheses; the integrand is simply the
first argument. The integration variable is the second.
Log$1 x2 (
1
Out[7]= x ArcTan#x'
2
It is important to remember that if a function has one antiderivative, it has infinitely many others.
But given one, any other can be obtained by adding a constant to it. Mathematica always gives the
most simple antiderivative, the one whose constant term is zero.
One may also use Integrate to produce a general formula by using an integrand that includes one or
more symbolic parameters. Here, for instance, is the first integration formula one typically learns in
a calculus course:
à x Šx
n
x1n
Out[9]=
1n
224 Calculus
Note that this formula holds for almost all n (it fails if n 1). It is an intentional design feature to
not specify such special cases, like this one where there is a single exception to the general formula
provided. This means it’s left to you, the user, to critically contemplate the output, cognizant that
there may be exceptions. However it is also possible to get a piecewise function as the value of an
integral. This will occur when there are two or more measurable regions on which the integral
assumes different values. A typical example of this behavior is when the integrand is itself a piece-
wise function.
Ã
nx 1 x 0
In[10]:= Å x
Æ n x x0
Æn x
x0
n
Out[10]= 1 2
x x nx True
n 3
All of the familiar integration formulae are at your fingertips. Here we see the chain rule, which is
the basis for the technique of substitution:
à F '#u#x''u '#x'Šx
Out[12]= F#u#x''
A more subtle feature of the Integrate command is the manner in which real variables are handled.
By default, it is assumed that the integrand is a function that may assume complex values, and that
may accept complex input. In most cases this creates no issue whatever. But there are exceptions, so
it is important to know how to restrict the values of parameters to the field of real numbers. To do
so, follow the syntax of this example:
t3
Out[13]= t
3
This input reads pretty much as one would say it out loud: assuming that t is an element of the
reals, integrate the function that follows with respect to t . Equivalently, you may add an Assumpg
tions option to the Integrate command:
t3
Out[14]= t
3
The outputs above are simpler than that produced by a straight integration:
5.10 Integration 225
In[15]:= Ã t4 2t2 1 Å t
,1 t2 0 ,3 t2 0
2
t
3 ,1 t2 0
Out[15]=
,1 t 2 0
2
This output is different since does not necessarily simplify to 1 t 2 when t is a complex
number. Every complex number has two square roots, and the radical indicates the principal square
root (see Exercise 2). More examples that make use of Assumptions are provided in the next section.
It is also worth noting that there are numerous “special” functions that cannot be defined in terms
of such elementary functions as polynomials, trigonometric functions, inverse trigonometric func-
tions, logarithms, or exponential functions, but that can be described in terms of antiderivatives of
such functions. If you use Mathematica to integrate a function and see in the output something
you’ve never heard of, chances are that Mathematica is expressing the integral in terms of one of
these special functions. Here’s an example:
à Cos$x (Šx
2
In[16]:=
S 2
Out[16]= FresnelC% x)
2 S
In[17]:= ? FresnelC
Don’t be intimidated by such output. It simply says the integral you asked for cannot be expressed
in terms of elementary functions. It expresses the answer in terms of another such integral, one so
famous that it has its own name (like FresnelC). Augustin Fresnel (1788–1827) was a French mathe-
matical physicist who studied this and similar integrals extensively. There is a FresnelS integral as
well, it uses sine in place of cosine.
There is also the possibility that Mathematica will evaluate an integral producing an expression that
involves complex numbers. Such numbers can be recognized by the presence of the character Ç in
the output, which denotes 1 . In cases such as this, the Assumptions option that we mentioned
earlier will not eliminate the appearance of complex numbers. Rather, they are necessary (even in
the real case) to express the antiderivative.
226 Calculus
In[18]:= Ã x x3 Å x
In this example we also have an appearance by the special function EllipticE. What’s that?
In[19]:= ? EllipticE
If that’s not helpful, don’t worry about it. Suffice it to say that there is a whole universe of special
functions out there, and you’ve just caught a glimpse of a small piece of it. The bottom line is that
integration is difficult by nature. Mathematica doesn’t know whether or not you hold a Ph.D. in
mathematics. It does the best it can. You shouldn’t be surprised or discouraged if you occasionally
get a bit more back than you expected.
Another possibility when integrating is that Mathematica simply won’t be able to arrive at an
answer. Alas, some integrals are just that way. In such situations, the output will match the input
exactly:
In[20]:= Ã ArcTan#t' Å t
Out[20]= Ã ArcTan#t' Å t
Exercises 5.10
1. Evaluate the following integrals. Note that in many cases a constant a appears in the integrand,
and that in all cases the integration is with respect to the variable u. The results mimic many
standard integral tables (such as those found on the inside jackets of calculus textbooks).
a. Ã a2 u2 Å u
b. Ã a2 u2 Å u
c. Ã ,a u 0
2 2 3s2
Å u
d. Ã u 2a u u2 Å u
5.11 Definite and Improper Integrals 227
e. Ã sec+u/Å u
2. Show that for c +1 Ç/ it is not the case that c2 c, then find another complex number c for
which the equation does not hold.
3. Consider the family of functions ln x, +ln x/2 , +ln x/3 , +ln x/4 , etc.
a. Integrate +ln x/n for integers n 1 through 5, identify the pattern, and propose a general
formula for ¼ +ln x/n Å x for any positive integer n.
formula reduces to +ln x/n . You will then have proved that your formula is correct. Congratula-
b. Using pencil and paper, prove that the derivative (with respect to x) of the expression in your
x1 1 1 1 , x1 1 1 1 1
à ,1 x 0Šx
2
2
In[1]:=
2
4
Out[1]=
3
The same comments made in the last section with regard to grouping parentheses apply here as
well; in particular, if you push the palette button before typing the expression you wish to integrate,
it may be necessary to put grouping parentheses around that expression when you type it. If you
prefer typing to palettes, the command you need is Integrate. It works as it did in the last section,
but the second argument is now an iterator giving the name of the variable and the bounds of
integration.
228 Calculus
4
Out[2]=
3
Here is a plot where the value of the definite integral corresponds to the signed area of the shaded
region—the area of the portion above the x axis minus the area of the portion below it:
Out[3]=
As in the previous section, the option setting Assumptions may be used, or equivalently an integral
created using the palette may be placed as the second argument to the Assuming command. Here’s
an example:
Ã
n
Abs#x'Å x
n
Out[5]= n Abs#n'
Assuming%n ! 0, Ã
n
In[6]:= Abs#x'Å x)
n
Out[6]= n2
A little thought will reveal that the first output above is exactly right; if n is a negative number the
absolute value is an absolute necessity.
There is a special class of function that needs discussion. We saw in Sections 3.2 and 4.4 that
Mathematica’s cube root function x1s3 (internally this is Power[x, 1/3]) differs from the elementary
If you need to integrate a power function where the power is a rational number with odd denomina-
tor, and the bounds of integration include negative numbers, you need to know which power
function to use. If you wish to use the real-valued power function (the one typically used in calculus
courses), you will want to use this realPower command rather than Mathematica’s default Power
command. The following example illustrates the difference (note that the antiderivative of x2s3 is
3x1s3 ):
3 3Ç 3
Out[8]=
1s3 1s3
2 2
Out[9]= 3 22s3
Note also that with complex numbers lingering just below the surface, it should be a comfort to
know that in many cases they simply cannot arise. For instance, if one has real numbers for both
upper and lower bounds (so that one is integrating over a real interval), and in addition the inte-
grand is a real-valued function on this interval, then the definite integral, if it converges, must
evaluate to a real number. Even if Mathematica produces complex output (the symbol Ç can be seen),
you can be assured that the expression is using complex numbers to represent a real number. For
of a positive real number throughout the interval #0, 1'. In the next section we discuss how to
example, the following output must be a real number, as the integrand is real—it’s the square root
Ã
1
In[10]:= t4 2t2 2 Å t
0
), Ç)
1 1 Ç
Out[10]= 5 2 1 Ç EllipticE%Ç ArcSinh%
3 2 2
), Ç)
1 Ç
2 1 Ç EllipticF%Ç ArcSinh%
2 2
230 Calculus
Riemann Sums
Mathematica makes the computation of Riemann sums easy with the Sum command. Sum works
very much like Table, but rather than producing a list of the specified items, it adds them:
Out[11]= 30
This is the same as adding 1 4 9 16. The advantage of using the Sum command for such addi-
tions can be seen when you want to add lots of numbers:
There is a palette version of the Sum command on the BasicMathInput palette that allows you to
employ the traditional summation notation. Use the Å e button, and then use the Í key to move
f
f f
Åi
100
2
In[13]:=
i 1
The cells below provide an example of a Riemann sum computation for a function f over the
interval from a to b, with n rectangles and f evaluated at the left endpoint of each subinterval. The
first cell sets the values of f , a, and b. It needs to be edited every time you move from one example
to the next:
The following cell makes use of the values above and defines the appropriate Riemann sum as a
function of n. It does not need to be edited as you move from one example to the next:
ba
In[18]:= 'x#nB' : ;
n
x#iB, nB' : a i
'x#n';
i 0
5.11 Definite and Improper Integrals 231
The function 'x returns the width of an individual rectangle—it is a function of n because its value
depends on the number of subintervals between a and b. The function x returns the right endpoint
of the i th subinterval. It is a function of both i and n. Lastly, leftRsum returns the Riemann sum for
your function between a and b, with the function evaluated at the left endpoint of each subinterval.
In[21]:= leftRsum#50'
Out[21]= 0.937499
Note that the values of i in the summation (from 0 to n 1) dictate that f be evaluated at the left
endpoint of each subinterval. To compute a Riemann sum with f evaluated at the right endpoint of
each subinterval you can change the bounds of the summation to 1 and n:
In[23]:= rightRsum#50'
Out[23]= 0.880853
Either sum can be viewed as an approximation to the definite integral of f over the interval from a
to b. It is not hard to modify the process to generate other approximations such as those employing
the trapezoidal rule or Simpson’s rule. The approximations tend to get better as the value of n
increases, as the following table shows:
n Riemann Sum
50 0.880853
100 0.895106
150 0.899843
Out[25]= 200 0.902209
250 0.903628
300 0.904574
350 0.905249
400 0.905755
à f#x'Šx ss N
2
In[26]:=
0
Out[26]= 0.909297
Here is a plot where the value of the definite integral corresponds to the signed area of the shaded
region:
Out[27]=
Ã
2
In[28]:= Æx Å x
Out[28]= S
, x, 1, )
1
In[29]:= Integrate%
x3
1
Out[29]=
2
Of course there is the possibility that an improper integral will fail to converge. Mathematica will
warn you in such circumstances:
Ã
1
In[30]:= Å x
1 x
Ã
1
In[31]:= Å x
1 x
Ã
1
Out[31]= Å x
1 x
Other improper integrals occur when a function has a vertical asymptote between the upper and
lower bounds. It is dangerous to evaluate these integrals by hand using the Fundamental Theorem of
Calculus without carefully considering the behavior of the function at the asymptotes. Luckily,
Mathematica is very careful and will tell you when these integrals do and do not converge.
Ã
2 1
In[32]:= Å x
2 x2
Ã
2 1
Out[32]= Å x
2 x2
Out[33]=
Mathematica is rightly reporting that the shaded area (were it not cut off at y 500) is infinite. Here
is another example:
234 Calculus
Ã
2
In[34]:= S
Csc#x'Å x
2
Ã
S
2
Out[34]= S
Csc#x'Å x
2
Out[35]=
Students frequently argue that this integral should be zero due to the symmetry apparent in the
graph. But Mathematica is returning the correct answer. Since the area to the left of x 0 is not
convergent the entire integral is divergent.
In[35]:= Clear#b';
Limit%Ã
b
S
Csc#x'Å x, b 0, Direction 1)
2
Out[36]=
In the previous examples the functions had asymptotes at x 0. In the next example the interval
also contains a vertical asymptote but the integral converges.
Ã
5 1
In[37]:= Å x
3
x3
Out[37]= 2 2
5.11 Definite and Improper Integrals 235
Out[38]=
v#tB' : Ã a#u'Å u 20
t
In[41]:=
0
s#tB' : Ã v#u'Å u 4
t
In[42]:=
0
In[43]:= s#t'
Out[43]= 4 20 t 16 t2
You simply need to remember that Integrate always returns the antiderivative whose constant term
is equal to zero, so constants need to be included in such definitions. The function v+t/ above
satisfies the condition that v+0/ 20, while the function s+t/ satisfies s+0/ 4. In the example above,
s+t/ represents the height in feet above ground level of an object after t seconds if it is thrown
vertically upward at an initial velocity of 20 feet per second and from an initial height of 4 feet; v+t/
is the velocity of the object at time t , and a+t/ is the object’s acceleration. Air resistance is ignored.
Ã
1
In[44]:= ArcTan#t' Å t
0
Ã
1
Out[44]= ArcTan#t' Å t
0
236 Calculus
Even in cases like this, you will often be able to get numerical approximations:
In[45]:= N#'
Out[45]= 0.629823
The next section explores a better way to get numerical approximations of definite integrals.
Exercises 5.11
1. Make the following sketch of the graph of f +x/
1
, and evaluate the definite integral of f from
x2
x 1 to x 3.
a. Ã
1
t 4 2t 2 1 Å t
0
b. Ã
1
t 4 2t 2 2 Å t
0
c. Ã cos+t/ Å t
S
10
0
3. Use a Riemann sum to approximate the second of the three integrals in the previous exercise.
a. Use n 100 subintervals and left endpoints.
b. Use n 100 subintervals and right endpoints.
c. Use n 100 subintervals and midpoints.
d. Make a Plot of f +x/ x4 2x2 2 and use it to decide which of the three approximations is
best.
5.12 Numerical Integration 237
4. Your opponent chooses a number p strictly between 0 and 1. Your opponent then chooses a
function on the domain #0, 1' passing through +0, 0/ and +1, 1/ whose arc length exceeds q, and
second number q strictly between 2 and 2. To defeat your opponent, find a strictly increasing
Manipulate%
1a
6. Assuming that a and b are real numbers, under what specific conditions on a and b can the
integral ¼a
b
t Å t be evaluated? What is does it evaluate to in this case?
7. Use Integrate to illustrate the two parts of the fundamental theorem of calculus.
¼ f +t/Å t .
d g+x/
8. Use D and Integrate to calculate a formula for
dx a
Ã
1
Out[1]= ArcTan#t' Å t
0
238 Calculus
Out[2]= 0.629823
Here’s an example where using Integrate followed by N gives a different result than NIntegrate.
That can happen! In almost all cases, NIntegrate will provide a better result. Here, the antiderivative
provided by Integrate involves complex numbers, and when approximated by N a very small
imaginary component (at the threshold of machine precision) persists.
Ã
1
In[3]:= t4 2t2 2 Å t
0
), Ç)
1 1 Ç
Out[3]= 5 2 1 Ç EllipticE%Ç ArcSinh%
3 2 2
), Ç)
1 Ç
2 1 Ç EllipticF%Ç ArcSinh%
2 2
In[4]:= N#'
Out[5]= 1.67571
NIntegrate accepts arguments exactly as Integrate does for handling definite integrals. There is no
palette version of NIntegrate. It is important to understand that NIntegrate works in an entirely
different way from Integrate. Rather than attempt symbolic manipulation, NIntegrate produces a
sequence of numerical values for the integrand over the specified interval, and uses these values to
produce a numerical estimate for the integral. Although the algorithm used is quite sophisticated,
you can think of NIntegrate as producing something analogous to a Riemann sum. The good news
is that you now have at your disposal a means for estimating some very messy integrals. The bad
news is that NIntegrate can occasionally produce poor estimates. Just as the Plot command can
miss features of the graph of a function that are “narrow” relative to the domain over which it is
plotted, NIntegrate can miss such features also. Problems arise if points near the narrow feature are
not sampled (for a discussion of the Plot command in this context, see Section 3.2 on page 55). Here
is an example:
5.12 Numerical Integration 239
Out[6]=
Here is the same function plotted over a much larger domaiO
To much larger that the bump
disappears from view:
1.0
0.8
0.6
Out[7]=
0.4
0.2
Out[8]= 1.77243
When NIntegrate is applied to this function over the larger domain, it also misses the bump:
NIntegrate::slwcon :
Numerical integration converging too slowly; suspect one of the following: singularity, value
of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. j
NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near x
NIntegrate::ncvb :
Out[9]= 1.9195
240 Calculus
The warning messages provide a hint that something might not be right, yet an incorrect output is
generated. This phenomenon can be even worse if the integrand has discontinuities, for in such
situations the actual definite integral may not have a real value, yet NIntegrate may report one. The
following integral, for example, does not converge:
, x, 0, 3)
1
+x 1/+x 2/
In[10]:= NIntegrate%
NIntegrate::slwcon :
Numerical integration converging too slowly; suspect one of the following: singularity, value
of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. j
NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near x
NIntegrate::ncvb :
Out[10]= 0. 101
Taking the right and left limits of the exact integral as the upper and lower bounds, respectively,
approach 1 demonstrates nonconvergence:
Limit%Ã
b 1
+x 1/+x 2/
In[11]:= Å x, b 1, Direction 1)
0
Out[11]=
Limit%Ã
3s2 1
+x 1/+x 2/
In[12]:= Å x, a 1, Direction 1)
a
Out[12]=
If you know that the integrand has discontinuities in the interval over which you are integrating
(vertical asymptotes in the graph are a giveaway), you can instruct NIntegrate to look out for them
by replacing the iterator {x,xmin,xmax} with {x,xmin,x1 ,x2 ,x3 ,…,xmax}, where x1 , x2 , x3 , … are the
points of discontinuity. Sometimes this won’t help, certainly not in those cases when the integral
does not have a real value:
5.12 Numerical Integration 241
, x, 0, 1, 2, 3)
1
+x 1/+x 2/
In[13]:= NIntegrate%
NIntegrate::slwcon :
Numerical integration converging too slowly; suspect one of the following: singularity, value
of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. j
NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near x i 83j.
NIntegrate::ncvb :
NIntegrate obtained 146.633 and 28.765504119209318` for the integral and error estimates. j
Out[13]= 146.633
But if the integral has a value, NIntegrate will usually produce a very good approximation to it:
, x, 0, 1, 3)
1
NIntegrate%
+x 1/
In[14]:=
4
2
Out[14]= 4.82843
Out[15]=
One strategy to help you determine if NIntegrate is providing an accurate answer is to examine
carefully the plot of the integrand. If the numerical value provided by NIntegrate appears consis-
tent with the area in the plot, but you still have your doubts, you might try breaking up your
integral as a sum of integrals over various disjoint intervals whose union is the interval over which
you are integrating. Place short intervals around any discontinuities:
Assuming%b 1, Ã
b 1
Å x)
+x 1/
In[16]:=
0 4
2
Out[17]= 2
Ã
3 1
Å x
+x 1/
In[18]:=
1 4
2
Out[18]= 2 2
Assuming%a ! 1, Ã
3 1
Å x)
+x 1/
In[19]:=
a 4
2
Out[19]= 2. 2 1 a 2
Out[20]= 2 2
, x, 0, 0.9)
1
NIntegrate%
+x 1/2
In[21]:=
4
Out[21]= 4.82843
Good. This is consistent with the previous output. As usual, it is up to you to test and decide on the
efficacy of results produced with the computer.
Exercises 5.12
1. Use NIntegrate to produce a numerical approximation to ¼0
1
t 3 3t 1 Å t .
Out[1]=
Once you have generated a plot you can grab it with your mouse and rotate it to view it from any
angle. With a little work you can use this feature to display a curve as if it has been rotated about the
radius t rotated about the vertical axis. To get a plot of the surface generated by revolving y x2 for
the x values 0 through 2 about the x-axis you must first generate the graph below:
Out[2]=
Now use your mouse to orient the surface so that it appears to be a revolution about the x-axis as
below:
244 Calculus
Sometimes it can be easier to visualize a surface by cutting it open and peering inside. You can do
this by plotting it on less than a full revolution. The plot below shows the revolution of both x2 and
x about the y-axis through only 270 degrees. Note that we need to use radians to indicate our
angle of revolution.
Out[3]=
Out[4]=
5.13 Surfaces of Revolution 245
To display the surface formed when this region is revolved about the y-axis enter:
Out[5]=
To display the surface formed when this region is revolved about the x-axis enter the command
below, and then maneuver the plot appropriately with your mouse.
Out[6]=
Exercises 5.13
1. Use RevolutionPlot3D to plot y sin+x/ on the interval 0 to 4S and revolve the surface through
330 degrees.
n
In[1]:= Limit% , n )
nn
Out[1]= 0
Out[2]= S
There are several powerful commands for dealing with series. The first and most simple is the Sum
command, discussed earlier in this chapter—see the subsection “Riemann Sums” in Section 5.11 on
page 230. If you haven’t yet used this command (to compute a Riemann sum, for instance), it’s not
hard. It works like the Table command, but rather than creating a list of the specified items, it adds
them.
The really amazing thing about this command is that it can accept as a bound, meaning that it
can find the value of an infinite series.
Å
1
In[3]:=
n 1 n2
S2
Out[3]=
6
In[4]:= N#'
Out[4]= 1.64493
Of course some series fail to converge, and others have solutions that Mathematica will not be able to
find. Solutions to the latter type can be approximated by summing a large number of terms. Here is
a series that doesn’t converge:
Å Cos#n'
In[5]:=
n 1
Å Cos#n'
Out[5]=
n 1
Here is an example of a series involving an independent variable x. Note that the nonpalette version
5.14 Sequences and Series 247
of the Sum command is more flexible than its palette counterpart in that the iterator can be
adjusted to skip certain terms. Here is the sum 1 x2 x4 :
Clear#x';
Sum$xn , n, 0, , 2(
In[6]:=
1
Out[7]=
1 x2
In this example Mathematica reported a solution, but did not specify which values of the indepen-
dent variable x are acceptable. In particular, note that if x 1 the denominator will be zero, and
the solution makes no sense. Mathematica reports the solution you most probably need in such
situations, but it is up to you to determine the region of convergence, those values of the independent
variable for which the solution is valid. In the example above, x must fall strictly between 1 and 1
for the solution to be valid.
The Mathematica command that more or less undoes what the Sum command does is the Series
expansion of 1 t ,1 x2 0:
command. Here are the first few terms (those with degree not exceeding five) of the Taylor series
, x, 0, 5)
1
In[8]:= Series%
1 x2
Out[8]= 1 x2 x4 O#x'6
power series expansion. The second is a special iterator, one whose form is variable, x0 , power}, where
The Series command requires two arguments. The first is the function for which you wish to find a
variable names the independent variable, x0 is the point about which the series is produced, and
power specifies the highest power of the variable to be included in the output. The output includes a
big O term, indicating that there are more terms in the series than those being shown. To get rid of
the big O term, use the Normal command:
In[9]:= Normal#'
Out[9]= 1 x2 x4
You can produce a plot of this polynomial and the function. Note that the polynomial provides a
good approximation to the function when x is near x0 (x0 0 in this example):
248 Calculus
4.0
3.5
3.0
Out[10]= 2.5
2.0
1.5
Note that you can get the formula for a Taylor series expansion for an arbitrary function (such as f )
about an arbitrary point (such as a). Here are the first four terms of such a series:
Clear#a, f';
Normal#Series#f#x', x, a, 3''
In[11]:=
f#a' +a x/ f
#a' +a x/2 f
#a' +a x/3 f +3/ #a'
1 1
Out[12]=
2 6
In fact it is a simple matter to design a custom command for generating Taylor polynomials of
degree n for the function f about the point x0 :
For example, we can now easily compute the eleventh-degree Taylor polynomial for the sine func-
tion, expanded around the point x 0:
x3 x5 x7 x9 x11
Out[14]= x
6 120 5040 362 880 39 916 800
Here is another example. The fourth-degree Taylor polynomial for cos+x/, expanded about the point
S
x , is given below:
4
taylor%Cos, x, !, 4)
S
In[15]:=
4
, x0 , x0 , x0
S S 2 S 3 S 4
1 x
4 4 4 4
Out[15]=
2 2 2 2 6 2 24 2
S
And here is a list of the first five Taylor polynomials for cos+x/, again expanded about the point x :
4
5.14 Sequences and Series 249
2 2
- x1
S S 2
1 x
4
4
2 2 2 2
- x1 - x1
S S 2 S 3
1 x
Out[16]= 4
4
4
2 2 2 2 6 2
2 2 2 2 6 2 24 2
2 2 2 2 6 2 24 2 120 2
Finally, let’s produce a sequence of graphics, one for each of the first twelve Taylor polynomials for
S
the cosine function, expanded about the point . Each plot will show the cosine function in light
4
gray, with the Taylor polynomial in black, and with the point , , cos, 00
S S S 1
, , highlighted.
4 4 4 2
The individual frames are displayed in a Grid below, and should be read sequentially from left to
right across the rows to increase the degree of the Taylor polynomial.
Out[19]=
250 Calculus
In a live session the following command is fun. Simply click on a tab to see the Taylor polynomial of
that degree.
1 2 3 4 5 6 7 8 9 10 11 12
Out[23]=
Exercise 3 below shows how to use the Thread command to construct this TabView.
Exercises 5.14
- 1 ,- 1 ,- 1 ,…
2 3 2 4 3 5 4
1. Find the limit of the sequence: ,
1 2 3 4
- 1, - 1, 1, 1, …
1 1 1 2 2 1 3 3 3 1 4 4 4 4
2. Find the limit of the sequence: - -
1 2 2 3 4 3 4 5 6 4 5 6 7 8
3. The Thread command can be used to take two lists such as {1,2,3} and {a,b,c}, and thread some
command f through them to produce the list {f[1,a],f[2,b],f[3,c]}. It is particularly handy when
constructing a TabView object, where the syntax requires a list of the form {label1 item1,label2
item2,label3 item3,...}. In this case, f is the command Rule (the FullForm of 1 a is
Rule[1,a]). Thread is discussed in greater detail in Section 8.4 on page 403.
a. Enter the following inputs to see Thread in action.
4. Create a TabView that displays the first 10 distinct Taylor polynomials for sin+x/ expanded about
x0 0.
6
Multivariable Calculus
6.1 Vectors
A standard notation for a vector in the plane is a coordinate pair, such as ;2, 5?. This represents the
¶ ¶ ¶ ¶
vector that has its tail at the origin and its head at the point with x coordinate 2 and y coordinate 5.
Another standard notation for this vector is 2i 5j . Here i and j denote the unit vectors in the x
and y directions, respectively.
In Mathematica, a vector in the plane is expressed as a list of length two, such as 2, 5. Vector
addition and scalar multiplication work exactly as you would expect:
Out[1]= 19, 1
42, 5
8, 20
In[2]:=
Out[2]=
i 1, 0;
0, 1;
In[3]:=
j
2i 5j
Out[5]= 2, 5
Higher-dimensional vectors are simply given as longer lists. Here is the sum of two vectors in three-
space:
60, 60, 8 !
S
Out[6]=
4
252 Multivariable Calculus
Out[7]= u1 v1 u2 v2
Out[8]= 32
You can calculate the norm (i.e., the length or magnitude) of a vector using the Norm command.
Out[9]= 5
¿ We have made use of subscripts to display general vectors. This looks very nice. However, it
can get you into trouble if you try to give such a vector a name. You should never enter input
such as u u1 , u2 . This will throw Mathematica into an infinite loop. See Exercise 4.
The dot product can also be employed to find the angle between a pair of vectors. You may recall
that the cosine of the angle T between vectors u and v is given by the formula:
uv
uºv
cos T
where u denotes the norm of u. You can find the angle (in radians) between vectors like this:
6.1 Vectors 253
) ss N
u.v
ArcCos%
Norm#u'Norm#v'
Out[14]= 2.0724
180
Conversion to degrees requires multiplying by the conversion factor , or dividing by the built-in
S
constant Degree:
In[15]:= s Degree
Out[15]= 118.74
In[16]:= Graphics#Arrow#0, 0, 1, 1', Arrow#0, 0, 2, 1', Axes True'
1.0
0.8
0.6
Out[16]= 0.4
0.2
Arrow accepts as its argument a list of two ordered-pairs. These represent the coordinates of the tail
and head of the arrow, respectively. Note, however, that you can double click on any arrow in a
graphic, then drag either end of the arrow to a new location. Or you can drag the middle portion of
the arrow and move the entire thing to a new location, preserving its length and direction.
But working programmatically (rather than grabbing and dragging) is advantageous for attaining
precise positioning. Here we illustrate a vector sum:
254 Multivariable Calculus
Graphics$
Arrow#0, 0, 1, 2', Arrow#0, 0, 2, 1',
In[17]:=
2.5
2.0
Out[17]= 1.5
1.0
0.5
0.0
0.0 0.5 1.0 1.5 2.0 2.5 3.0
And why not drop it into a Manipulate? This has a Locator at the head of each of the two compo-
nent vectors in the sum, so that you can drag either of those vectors by the head to move them. It’s
actually easier to make sense of the structure of the input when it’s written in this general form:
In[18]:= Manipulate$Graphics$
Arrow#0, 0, u', Arrow#0, 0, v',
Gray, Arrow#u, u v', Arrow#v, u v' ,
Red, Arrow#0, 0, u v'
, PlotRange 3, Axes True, Ticks None(,
u, 1, 2, Locator, Appearance None ,
v, 2, 1, Locator, Appearance None (
Out[18]=
6.1 Vectors 255
¿ There is no primitive Arrow object available for 3D graphics, although the VectorFieldPlot
package has a command called VectorFieldPlot3D which will draw 3D arrows for vector fields.
This is discussed in Section 6.5 on page 327. The issue with 3D arrows is the arrowheads,
which should be cones, and as of version 6 of Mathematica, there is no primitive for cones.
You can harness Mathematica to take the cross product of a pair of vectors with the command Cross.
Here that command is used to give the general formula for the cross product:
Out[19]=
And here it is used to find the cross product of a specific pair of vectors:
Out[20]=
You can also use the small l button on the BasicMathInput palette to calculate cross products. It’s
the button under the Ç near the middle of the palette. Don’t confuse it with the larger button to
its left; that one is used for ordinary (component-wise) multiplication.
Out[21]=
Exercises 6.1
1. Find an exact expression for the sine of the angle between the vectors ;2, 1, 1? and ;3, 2, 1?.
2. Look up the Sign command in the Documentation Center. Explain how to use it with the dot
product to determine whether the angle between a pair of vectors is acute, right, or obtuse.
3. The dot product, we learned in this section, is implemented in Mathematica in infix form by
placing a period between a pair of vectors. There is (as always) a “square bracket” version of this
command. It is called Dot.
a. Use Dot to take the dot product of u1, u2, u3 with v1, v2, v3.
b. The dot product is the most common example of an inner product. Mathematica has another
command called Inner that can be used to create alternate inner products. Verify that the
input Inner[Times, {u1, u2, u3}, {v1, v2, v3}, Plus] gives the same output as that produced in
part a.
256 Multivariable Calculus
u1 , u2 . The subscripts are obtained from the keyboard by typing, for instance, u ŹÎ_Ï 1. The
4. Near the beginning of this section, we did several computations using general vectors of the form
proof, for instance) write, “Let u ;u1 , u2 ?.” While this is a convenient notation, it cannot be
FullForm of the resulting expression is Subscript[u,1]. Many textbooks will (in the course of a
replicated in Mathematica. If you were to enter the input u u1 , u2 , what would happen? The
same bad thing will happen if you enter the more simple input u u1 . Try it, then explain
what’s going on. As the blues singer Kelly Joe Phelps put it, “It’s not so far to go to find trouble.”
5. Use Mathematica to verify the parallelogram law in 53 : For any pair of vectors u and v ,
u v u v
2 2 2 2
2u 2v .
Clear$f, x, y(;
Sin$x2 y2 (;
In[2]:=
f
A function of three variables is dealt with similarly. Note that it is important to Clear any variables
that have previously been assigned values.
To evaluate a function defined in this fashion, one uses replacement rules. For instance, here is
f .0, S s 4 2:
6.2 Real-Valued Functions of Two or More Variables 257
In[6]:= f s. x 0, y Ss4 !
1
Out[6]=
2
In[7]:= f s. x 1 S, y 1 S
In[8]:= Simplify#'
Out[8]= 0
Out[9]=
Grab such a plot with your mouse and drag. This will rotate the image so that you can see it from
any vantage point you like. Hold down the Ø key while you drag and you can zoom in and out.
It’s a beautiful thing.
Note that it is a simple matter to produce a sketch of any vertical cross-section (sometimes called a
trace) for such a plot in either the x or y direction. Simply set one of the two variables to a numerical
value and make a Plot using the other as the independent variable.
258 Multivariable Calculus
0.5
Out[10]=
x
2 1 1 2
0.5
Out[11]=
0.4
0.6
0.8
We’ll discuss how one can use the Mesh option to superimpose these traces onto the original
Plot3D of f in the subsection “Controlling the Mesh Lines” on page 265. See Exercise 6 for more on
cross-sections.
There are a host of options available that will allow you to tweak the output of these plotting
commands in some incredible ways. Among the options that are essentially the same as the familiar
options for Plot (see Section 3.3, see page 59) are such common settings as AxesLabel, PlotLabel,
PlotPoints, MaxRecursion and PlotRange. Other options, such as Mesh and MeshFunctions, work
in a similar manner as they do in Plot, but now everything is one dimension higher. In short, they
will take some getting used to.
Table$Plot3D$ÆSin#x y' , x, S, S, y, S, S , Mesh m, MaxRecursion 4(,
m, None, All(
In[12]:=
Out[12]= , !
Note that like Plot, Plot3D uses an adaptive algorithm that recursively subdivides the surface into
smaller polygons in areas where the surface bends more sharply. PlotPoints settings control how
many equally spaced points are initially sampled in each direction (so a setting of 50 will force
Mathematica to sample 50 50 2500 points in the domain). MaxRecursion controls the number of
recursive subdivisions permitted to fine-tune the image. Large settings for these options produce
beautiful images, but may result in perceptibly slower rendering times, and will definitely produce
larger file sizes when the notebook is saved. See Exercise 2.
y2 0
, x, 3, 3, y, 3, 3 , ClippingStyle k),
x2 y5 x5 y2 2
In[13]:= Table%Plot3D% Æ,x
100
k, Automatic, None)
Out[13]= , !
The setting PlotRange All will force Mathematica to show the entire graph. Notice, however, that
the bump in the middle of the plot vanishes from view due to the compression of the vertical axis:
260 Multivariable Calculus
y2 0
, x, 3, 3, y, 3, 3 , PlotRange All)
x2 y5 x5 y2 2
In[14]:= Plot3D% Æ,x
100
Out[14]=
The option BoxRatios determines the relative dimensions of the bounding box. The simple setting
BoxRatios 1 will produce a cubical bounding box, while the setting BoxRatios Automatic will
scale the bounding box so that all axes have the same scale; it is analogous to the option AspectRag
tio used in two-dimensional plots. Be careful to not use this setting in cases such as the one above
where one axis would be dramatically longer than the others. A setting such as BoxRatios {1, 1, 2}
will produce a bounding box whose horizontal sides are the same length, but whose vertical dimen-
sion is twice as long.
In[15]:= Plot3D$Sin$x Cos$y((, x, 6, 6, y, 3, 3 , BoxRatios Automatic(
Out[15]=
Plot3D$Sin$x Cos$y((, x, 6, 6, y, 3, 3 , BoxRatios Automatic, Boxed False,
Axes False, False, True, AxesEdge Automatic, Automatic, 1, 1(
In[16]:=
Out[16]=
ViewPoint specifies the position in space (relative to the center of the graphic) from which it is
seen. The setting {0, 0, 4}, for instance, will give a view from above, while the setting {3, 0, 1} will
yield a vantage point that is three units from the origin along the positive x axis, and one unit up.
Out[17]=
The ColorFunction
The option ColorFunction controls the coloring of the graph. Any of the color gradients available in
the ColorData archive may be used to color a plot. Type and enter ColorData["Gradients"] for a
listing of available gradients. For instance, here we color a plot using a color gradient reminiscent of
that used in topographical maps, where low regions are colored dark blue, middle regions are shaded
with greens and browns, and peaks are white.
y2 0
, x, 3, 3, y, 3, 3 , BoxRatios Automatic,
2
In[18]:= Plot3D%Æ,x
Out[18]=
262 Multivariable Calculus
ColorFunction allows you to assume complete control over the manner in which color is applied to
a surface. With a bit of work, the graph of a function may be colored (using any color gradient you
like) according to the values of any other function. See Exercise 1.
PlotStyle is used to set the intrinsic surface color, and to specify the surface’s diffusive and reflective
properties. Several settings may be simultaneously given by wrapping them within the Directive
command. There are three potential specifications. A straight color or opacity setting like Blue
and/or Opacity[.5] can be given to set the intrinsic surface color or transparency. This color will
interact with the lighting. A Glow setting, such as Glow[Red], will emanate from the surface irrespec-
tive of the color of the lighting. Finally, a Specularity setting determines the diffusive and reflective
properties of the surface. Specularity can accept two arguments. The first determines the color and
amount of diffusion added to reflected light. A numerical value of 1 is equivalent to a color setting
of White; in this case 100% of the light is reflected back, with no alteration to its color other than
that determined by the surface’s color. The second argument controls the shininess of the surface.
Typical values range from 1 (dull) to 50 (shiny). The setting Specularity[White, 20] is good for
creating the appearance of an anodized metallic surface:
In[19]:= Plot3D$x Cos$x y(, x, 3, 3, y, 2, 2 , Mesh None, MaxRecursion 4,
PlotStyle Directive$Lighter#Red', Specularity#White, 20'((
Out[19]=
Lighting can be adjusted in numerous ways. The default setting includes both ambient light and
four colored light sources (although if an explicit ColorFunction is specified, white light from these
same sources will be used instead). The simple setting Lighting "Neutral" will force the use of
white rather than colored lights.
6.2 Real-Valued Functions of Two or More Variables 263
In[20]:= Plot3D$x Cos$x y(, x, 3, 3, y, 2, 2 , Mesh None, MaxRecursion 4,
PlotStyle Directive$Lighter#Red', Specularity#White, 20'(, Lighting "Neutral"(
Out[20]=
The Documentation Center page for Lighting gives information on setting ambient, spot, and
directional light sources. We note here that the setting Lighting {{"Ambient", White}} is similar to
the setting Lighting "Neutral", but the latter includes point light sources and the shadows they
create, and so is better at giving the illusion of depth. The former may be appropriate when a special
ColorFunction is used and shadows would interfere with the information that the color provides.
In[21]:= GraphicsRow%
Plot3D% x2 y2 , x, 1, 1, y, 1, 1 ), Plot3D% x2 y2 , x, 1, 1, y, 1, 1 ,
Out[21]=
The setting Function$x, y , x2 y2 1( is a pure function. It takes a coordinate pair ,x, y0 as input,
and returns True precisely if that coordinate pair lies within the unit circle. In the resulting plot, the
domain is restricted to the region where this region function returns True. The setting in the second
264 Multivariable Calculus
plot above could also have been given in the shorter form RegionFunction ,Ó12 Ó22 1 &0.
Pure functions are discussed in Section 8.4.
RegionFunction also provides an excellent means of plotting over a punctured domain. A classic
example is the function f ,x, y0
x 2 y
, which is not defined at the origin, and which has an essential
x4 y2
discontinuity there. We remove a small disk from the center of the domain, and get a beautiful
image:
RegionFunction Function%x, y , .1 x2 y2 ),
Out[22]=
Another approach to controlling the domain is by defining a function whose value outside a desired
region is 0. This is easily accomplished using a Piecewise function. The Exclusions option is useful
for specifying discontinuities; note how easy it is to specify the locus of discontinuities as an
equation.
x2 y2 x2 y 2 1
In[23]:= f ;
0 x2 y 2 1
Out[24]=
6.2 Real-Valued Functions of Two or More Variables 265
Out[25]=
Out[26]=
The MeshFunctions option gives you even more control over the rendering of the mesh lines on a
plot. The price of this versatility is that it will take a bit of practice to master. Your efforts here will
be well rewarded, so read on.
By default there are two mesh functions, one for all the mesh lines corresponding to fixed x values,
and one for the perpendicular collection of mesh lines corresponding to fixed y values. Together
they form the familiar grid pattern that graces your Plot3D outputs. This default specification, if you
were to manually type it, would read:
266 Multivariable Calculus
It is a list of two pure functions, where each takes three arguments (one for each coordinate position
in three-space). Equivalently, this default setting could be entered using the common shorthand
notation: MeshFunctions Ó1 &, Ó2 &. Pure functions are discussed in Section 8.4. In any event,
mesh lines will be drawn where the mesh functions assume constant values; by default 15 evenly
spaced values will be displayed.
Perhaps the most common non-default setting is the following, which places level curves on your
plot. That is, the third variable (we usually call it z) is set to 15 evenly spaced values within the plot
range, and a mesh curve is added to the surface at each of these values.
In[27]:= Plot3D$x2 y3 +x 1/2 y, x, 2, 2, y, 2, 2 , MeshFunctions Ó3 &(
Out[27]=
But of course you may define any mesh function you like. Here we place mesh lines according to
distance from the origin. In other words, each mesh line lies on the surface of an invisible sphere
centered at the origin.
Out[28]=
6.2 Real-Valued Functions of Two or More Variables 267
MeshShading
The MeshShading option allows the regions between mesh lines to receive specific color directives.
The setting for this option has the same list structure as MeshFunctions; if there is a list of two
mesh functions, you should have a list of two MeshShading settings. Each such setting is itself a list
of directives that will by used cyclically (if this list is shorter than the number of mesh regions).
Setting the Lighting to "Neutral" will replace the default colored lighting with white lights, so that
the colors specified in the MeshShading are accurately rendered. For instance:
Plot3D$x2 y3 +x 1/2 y, x, 3, 3, y, 3, 3 , BoxRatios 1, MeshFunctions Ó3 &,
Mesh 20, MeshShading Red, Green, Lighting "Neutral"(
In[29]:=
Out[29]=
Set it to a list whose length matches the Mesh setting, and you will cycle precisely once through the
list of directives. To utilize an entire color gradient, keep in mind that each color gradient function
(such as ColorData["StarryNightColors"]) is defined on the domain 0 t 1.
Plot3D$x2 y3 +x 1/2 y, x, 3, 3, y, 3, 3 , BoxRatios 1,
MeshFunctions Ó3 &, Mesh 20, MeshShading
In[30]:=
Out[30]=
The output above is very similar to that produced with the setting ColorFunction "StarryNightCol
ors" (and no MeshShading or Lighting specifications). When MeshShading is used, each band
268 Multivariable Calculus
between mesh lines is uniformly shaded. When ColorFunction is used, the shading varies continu-
ously. When there are multiple MeshFunctions, the colors will criss-cross like a woven basket.
Below we use the default MeshFunctions setting {#1&,#2&}:
Plot3D$x2 y3 +x 1/2 y, x, 3, 3, y, 3, 3 , BoxRatios 1, Mesh 20,
MeshShading Yellow, Green, Black, White, Lighting "Neutral"(
In[31]:=
Out[31]=
solution curve can be regarded as a single contour line for the function f ,x, y0
We’ve previously used ContourPlot to plot solutions of equations (such as sin,x cos y0 0). This
sin,x cos y0. To
produce a full contour plot, use ContourPlot in a manner identical to that of Plot3D. Here we see a
ContourPlot and a Plot3D of the same function, showing the same level curves and using similar
shading:
6.2 Real-Valued Functions of Two or More Variables 269
Out[32]= , !
Perhaps the most commonly used option setting is Contours. Set it to a positive integer, say 20, and
you will see 20 contour lines in the resulting plot. Set it to a specific list of values and you will see
the contour lines through precisely those z values. If this list has a single value (as in the second plot
below), you will essentially be viewing the set of solutions to an equation in two variables.
GraphicsRow ContourPlot$,1 x2 0,1 y2 0, x, 2, 2, y, 2, 2 , Contours 20(,
ContourPlot$,1 x2 0,1 y2 0, x, 2, 2, y, 2, 2 , Contours 4(,
In[33]:=
Out[33]=
The ContourShading option works much like the MeshShading option for Plot3D. Note that you
may set this option to None. As was the case with Plot3D, the ColorFunction may be set to any
named color gradient.
270 Multivariable Calculus
GraphicsRow
ContourPlot$,1 x2 0,1 y2 0, x, 2, 2, y, 2, 2 , ContourShading Red, Blue(,
In[34]:=
Out[34]=
Also important are PlotPoints and MaxRecursion, which (as you might expect) can be employed to
x2
improve image quality. The function is not defined along the line y x 1, and ContourPlot
1xy
with its default settings has difficulty in the vicinity of this line. The Exclusions option provides
another means of dealing with such discontinuities.
Out[35]=
ContourPlot accepts many of the same options as Plot3D. Exceptions are those options that are
specific to two-dimensional graphics. For instance, one uses AspectRatio rather than BoxRatios to
6.2 Real-Valued Functions of Two or More Variables 271
adjust the relative dimensions of a graphic produced by ContourPlot. Note that by default, a
ContourPlot will be square.
Out[36]=
ContourPlot will embed tooltips into its output. Position the tip of the cursor along a level curve to
see the value of the z coordinate for all points on that curve. The option setting ContourLabels
Automatic can be used to place these values directly onto the graphic.
In[37]:= ContourPlot$Sin$x Cos$y((, x, 1, 1, y, 1, 1 , ContourLabels Automatic(
Out[37]=
While the placement of these labels on the graphic is handled automatically, the appearance and
value (displaying the z coordinate at the point ,x, y0), but make the text gray in a six point font. We
indeed the function used to calculate each label can be adjusted. Below we use the default function
ColorFunction "LightTerrain"(
Out[38]=
It is not difficult to write a command that will place a key next to a ContourPlot, as in the example
below. See Exercise 5.
If MeshFunctions are specified along with specific Mesh values, the curves where these functions
are equal to the respective Mesh values will be displayed. By default, there are no MeshFunctions
displayed in a ContourPlot (this is different from Plot3D, where the default MeshFunctions are
{#1&, #2&}, which produces the familiar grid pattern). Mesh curves will be superimposed on top of
the level curves. Here, for instance, we display the ellipse x2 2y2 1 in yellow on the plot:
6.2 Real-Valued Functions of Two or More Variables 273
Out[39]=
And here we display in dashed red the curve where the partial derivative with respect to x is zero,
and in yellow the curves (lines in this case) where the partial derivative with respect to y is equal to
zero. The critical points (and hence all maxima and minima for the function being plotted) occur at
the intersection points of these curves.
MeshFunctions Function%x, y, z , 2 x ),
4y
1 y2
,1 y 0
Function%x, y, z ,
2 2 1 y2
Out[40]=
274 Multivariable Calculus
In general, it’s not a bad idea when including multiple MeshFunctions to use MeshStyle to make
the first one Dashed or Dotted. That way, if two curves happen to coincide, the top curve won’t
completely obscure the bottom one.
In[41]:= ContourPlot3D$x2 y2 z2 m 1, x, 1, 1, y, 1, 1 , z, 1, 1(
Out[41]=
And just as ContourPlot may be used to render a collection of level curves for a function of two
variables (as discussed in the previous subsection of this chapter), ContourPlot3D may be used to
render a collection of level surfaces for a function of three variables:
Out[42]=
Note that three iterators are needed, one for each of the three coordinate variables. Virtually all
options are either identical to those of Plot3D, or can be surmised from those of ContourPlot. For
instance, the Contours option works just as it does in ContourPlot. The ContourStyle option can
6.2 Real-Valued Functions of Two or More Variables 275
be used to assign style directives to each level surface. The ColorFunction option can also be used
for this purpose; the plot on the right uses a ColorData gradient specified by ColorFunction.
Out[43]=
Out[44]=
Constructing 3D plots of solids (rather than surfaces) can be accomplished with RegionPlot3D. This
command is discussed in the subsection “Finding Bounds of Integration and Plotting Regions in the
Plane and in Space” on page 294.
276 Multivariable Calculus
Graphics3D Primitives
Just as one can use the Graphics command to “manually” construct a two-dimensional graphic
from primitive Point, Line, Polygon, and Text objects, one can use the Graphics3D command to
build three-dimensional graphics. The Graphics command was discussed in Section 3.9 on page
114. In most cases you will use higher level commands such as Plot3D to generate 3D images. But
there may come a time when you want to create a graphic from scratch, or to add a simple sphere or
cylinder to the output of a command such as Plot3D. This section provides a basic introduction to
such endeavors.
The primitive 3D objects that can be used to build a Graphics3D are many. They include familiar
objects such as Point, Line, Polygon, and Text, and new ones such as Cuboid, Cylinder, and
Sphere. Here are a few simple examples to get you started:
Out[45]=
Out[46]=
6.2 Real-Valued Functions of Two or More Variables 277
In[47]:= Graphics3D$
Sphere#0, 0, 0, 3',
Sphere#0, 0, 4, 2',
Sphere#0, 0, 6.5, 1',
Cylinder#0, 0, 7.5, 0, .5, 9, .8',
Cylinder#0, 0, 7.5, 0, .03, 7.6, 1.5',
Cylinder#2, 0, 4, 4, 0, 5, .2',
Cylinder#2, 0, 4, 4, 0, 5, .2'
, Boxed False(
Out[47]=
The overall situation is completely analogous to that for building Graphics. The single argument
given to Graphics3D is (most simply) a primitive or a list of primitives. Here we’ve used only
Sphere and Cylinder primitives. Sphere takes two arguments: the coordinates of its center, and its
radius. Cylinder also takes two arguments. The first is a list of two points: the endpoints of its
central axis. The second is its radius.
As is the case with Graphics, one can also apply one or more directives to each primitive. Directives
are used to customize the appearance of the individual primitive elements, and may include color,
opacity, and/or specularity settings. To apply a directive to any primitive object, replace the primi-
tive with the list {directive, primitive}. If more than one directive is to be applied, wrap them in the
Directive command, as in Directive[Red, Opacity[.8]].
278 Multivariable Calculus
Graphics3D$
Directive$Red, Opacity#.7'(, Sphere#0, 0, 0, 3' ,
In[48]:=
Out[48]=
One may also include Graphics3D option settings, such as Boxed False. Many of these have been
discussed in Section 6.2. In particular, the setting Lighting "Neutral" can be used to turn off the
colored lights that are used by default when rendering a Graphics3D. This will provide a more
honest rendering of any colors you introduce. Note that by default, surface primitives will be white
(they appear colored because of the colored lights). Here, for instance, we make a white snowman
with black hat and arms:
In[49]:= Graphics3D$
Sphere#0, 0, 0, 3',
Sphere#0, 0, 4, 2',
Sphere#0, 0, 6.5, 1',
Lighter#Black',
Cylinder#0, 0, 7.5, 0, .5, 9, .8',
Cylinder#0, 0, 7.5, 0, .03, 7.6, 1.5',
Cylinder#2, 0, 4, 4, 0, 5, .2',
Cylinder#2, 0, 4, 4, 0, 5, .2'
, Boxed False, Lighting "Neutral"(
6.2 Real-Valued Functions of Two or More Variables 279
Out[49]=
we use Plot3D to render the function f ,x, y0 xy with MeshFunctions set to measure distance on
It is common to combine a Graphics3D with the output from one of the plotting commands. Here
the surface to the point +0, 0, 3/. The plot is colored according to this distance using MeshShading.
We use Graphics3D to place a small green sphere at the point +0, 0, 3/. We put the two images
together with Show:
In[50]:= Show%Plot3D%x y, x, 3, 3, y, 3, 3 , PlotRange 4, 4,
Lighting "Neutral", BoxRatios 1, Mesh 15,
Out[50]=
280 Multivariable Calculus
¿ When using commands such as Plot that produce Graphics (as opposed to Graphics3D)
output, it is common to add an Epilog option to place Graphics primitives directly on the
image. This makes the use of Show unnecessary. While Epilog may also be used with com-
mands such as Plot3D, it cannot be used to place 3D primitives on the plot (such as the
Sphere above). So in a 3D setting, Show is the best way to add primitive Graphics3D objects
to an image.
Clear$f, x, y(;
Sin$x2 y2 (;
In[51]:=
Out[53]= 2 x Cos$x2 y2 (
In[54]:= D$f, y(
Out[54]= 2 y Cos$x2 y2 (
In[55]:= s. x 0, y S!
Out[55]= 2 S
Alternatively, you can calculate partial derivatives with the palette version of the D command by
using the f e button on the BasicMathInput palette. The subscript indicates the variable with
respect to which the derivative should be taken. Move from one placeholder to the next with the Í
key.
In[56]:= x f
Out[56]= 2 x Cos$x2 y2 (
If you use the palette version of D directly on a function such as x2 2xy, it is best to first type the
function expression, then highlight it, and then push the palette button. If you deviate from this
convention by pushing the palette button first, be sure to put grouping parentheses around the
function expression so that you don’t end up only differentiating the first summand:
6.2 Real-Valued Functions of Two or More Variables 281
In[57]:= x x2 2x y
Out[57]= 2x2xy
Out[58]= 2x2y
2 f
To find the second partial derivative , you can use the D command exactly as in Chapter 5:
x2
2 f
To find the mixed second partial derivative , simply do this:
x y
In[60]:= D$f, x, y(
Out[60]= 4 x y Sin$x2 y2 (
Alternatively, you may use the f,f e key on the BasicMathInput palette:
In[61]:= x,x f
In[62]:= x,y f
Out[62]= 4 x y Sin$x2 y2 (
The Gradient
The gradient of a function of two or more variables is a vector whose components are the various
In[64]:= x f, y f
There is no built-in command for producing gradients, but if you need to make extensive use of
them it is not hard to create your own:
This command takes two arguments. The first is the function whose gradient you wish to compute.
The second is the list of variables used in defining the function. For example:
Out[66]= 2 x y3 , 3 x2 y2
, x1 , x2 , x3 , x4 )
x1 2 x2 3 x4 4
In[67]:= grad%
x3 5
!
2 x1 x32 x44 3 x21 x22 x44 5 x21 x32 x44 4 x21 x32 x34
Out[67]= , , ,
x53 x53 x63 x53
You can evaluate the gradient at a specific point in the domain using replacement rules.
In[68]:= x f, y f s. x 0, y S!
Out[68]= 0, 2 S!
The following Manipulate will sketch a gradient vector directly on the ContourPlot for a function.
Initially it shows the gradient evaluated at the point - , 1.
1 1
The tail of the gradient vector is a
2 2
Locator; you can simply click on the graphic to move it to a new position, or you can drag it
around. The geometric properties of the gradient quickly become apparent: it is perpendicular to the
level curve through its tail, and it points uphill.
In[69]:= Manipulate%Module%x, y ,
Epilog Dynamic%Arrow%
Out[69]=
¿ There are several interesting features in this Manipulate. Notice that the first argument (the
thing we are manipulating) is a Module. Module is used to localize variables. In this case, the
variables x and y are insulated from any assignments made to them elsewhere in the session.
Module and other scoping commands are discussed in Section 8.6 on page 424. Secondly, note
that the Epilog to the ContourPlot is wrapped in Dynamic. While not strictly necessary, this
has the beneficial effect of forcing only the arrow to update as the Locator is moved, rather
than having the ContourPlot itself (which does not change) get re-rendered every time the
Locator is moved. This makes the action more “zippy.” For more information, search for
“Advanced Manipulate Functionality” in the Documentation Center and follow the link to the
tutorial of that name. Finally, note that we use the Appearance option to change the appear-
ance of the Locator from its default crosshair icon to a simple red dot. This option allows you
to make any Locator look exactly the way you like.
Given a function of two variables, there is a simple means for simultaneously plotting an array of its
gradient vectors over a rectangular domain, and for superimposing such a plot with a ContourPlot
of the function. See the subsection “Plotting a Two-Dimensional Vector Field” in Section 6.5, page
325, for a discussion of the GradientFieldPlot command.
You can take a directional derivative by taking the dot product of the gradient with a unit vector in
¶ ¶
the indicated direction. Here, for example, is an expression representing the directional derivative of
the function f ,x, y0 x2 y3 in the direction of the vector 3i j :
3, 1
In[70]:= grad$x2 y3 , x, y (.
Norm#3, 1'
3 x2 y2 2
Out[70]= 3 x y3
5
10
To evaluate the directional derivative at a specific point in the domain, use replacement rules:
284 Multivariable Calculus
In[71]:= s. x 2, y 3
2
Out[71]= 108
5
Optimization
There are numerous methods for finding extreme values of multivariate functions. It is certainly
possible to mimic the basic techniques presented in a standard calculus course, with Mathematica
doing the heavy lifting when the algebra gets tough. We outline such an approach in this section.
There are also the built-in commands Maximize, Minimize, NMaximize, NMinimize, FindMaxig
mum, and FindMinimum, which can be extremely useful, but which also have inherent
limitations. We’ll begin with these built-in commands, and then discuss the traditional approach
using critical points and second derivatives.
The commands Maximize, Minimize, NMaximize, and NMinimize (first introduced in Section 5.6
on page 212) all use the same syntax; understand one and you understand them all. In the most
simple setting, where your function is not overly complicated and happens to have a single extre-
mum in its largest natural domain, these commands make light work of optimization:
32, x 2, y , z 5!!
1
Out[72]=
2
If an extreme value does not exist, you can expect to see this sort of thing:
, x , y !!
33 8
Out[73]= ,z
10 5
These commands attempt to find global extrema. They can be adapted to hunt for local extrema by
adding constraints. Simply use a list as the first argument, where the second member of the list is an
equation or inequality (or any logical combination of these):
Here the minimum occurs at a corner of the square region. Below we search in two concentric
circular regions centered at the origin. Since the same answer is reached in two different concentric
regions, we are assured that the minimum occurs in the interior of each region (not on the bound-
ary). We have found, or at least approximated, a local minimum for f :
Unfortunately, Minimize is unable to give us an exact numerical solution. Rather, it presents the
three numbers in the output as roots of sixth degree polynomials.
Out[78]= Root$76 902 125 319 355 960 Ó1 238 172 822 Ó12
46 171 432 Ó13 3 783 476 Ó14 2 441 664 Ó15 236 196 Ó16 &, 1(,
x Root$25 180 Ó1 92 Ó12 1080 Ó13 832 Ó14 1440 Ó15 1296 Ó16 &, 2(,
y Root$25 232 Ó12 288 Ó13 184 Ó14 576 Ó15 1296 Ó16 &, 1(
occurs at - , 1.
We’ll see shortly that traditional methods can be used to determine that the actual local minimum
125 25
Our point here is to state plainly that while commands such as Minimize may
288 72
sound like a panacea for any optimization exercise, they can in fact be subtle to use and produce
output that is difficult to interpret. Worse yet, they can fail completely:
They also return only one extremum even in cases where there are two or more:
, x !!
1 1 1
Out[80]= ,y
2
2 2
x
y s. x !
1 1
In[81]:= ,y
2 2
1
Out[81]=
2
286 Multivariable Calculus
It is for these reasons that you must always be ready with plan B. Now don’t misunderstand us;
Minimize and NMinimize are powerful and versatile tools. In fact, they border on the amazing. But
they simply cannot be expected to work flawlessly in every situation; such is the variety and rich-
ness of the universe of mathematical functions. There are option settings for NMinimize that allow
you to specify the method it uses. Subtle details of this sort are essential if you plan to make exten-
sive use of these commands.
If your goal is to find all extrema for a differentiable function (as it often is in a calculus course),
your first line of defense in constructing plan B comes right out of your calculus textbook. The
critical points for a function are those points where the first partials are both zero (i.e., the gradient of
the function is the zero vector), or where one or both partials do not exist. If a function assumes a
relative minimum or maximum value in the interior of its domain, it does so at a critical point.
It is often possible to find critical points with Solve, NSolve, or Reduce. The setting here is just as it
was in Section 4.9, where we used these commands to solve systems of equations. Recall that Solve
and NSolve are designed primarily to solve polynomial equations, while Reduce can sometimes
solve more general classes of equations. Here’s an example where we use Solve to find the critical
points of a polynomial:
x 0, y 0 , x !!
125 25
Out[83]= ,y
288 72
In any of these solving commands you may use as the first argument either a list of equations, or an
equation of lists. For instance, this input will also work:
x 0, y 0 , x !!
125 25
Out[84]= ,y
288 72
You may be able to determine whether f has a relative minimum or maximum or saddle at a particu-
lar critical point in a purely algebraic fashion by examining the discriminant and the second partials
evaluated at this critical point. Recall that the discriminant of f is the expression
The standard test to determine the status of the critical point ,x, y0 is as follows:
Let’s carry out this test for the two critical points found in our previous example.
The first critical point, +0, 0/, is therefore a saddle point, while the second is either a maximum or
minimum. Which is it?
Out[86]= 8.
The positive value indicates that there is upward concavity in the x direction at this point. Since this
point is an extreme value, it must be a minimum. A ContourPlot shows both critical points clearly,
confirming this analysis:
Out[87]=
In[88]:= f s. crPts
0, !
15 625
Out[88]=
62 208
In this example we used Solve to find the critical points, but Reduce could have been used instead:
x m 0 «« x m
125 4x
Out[89]= && y m
288 5
A word about Reduce is in order. It is very convenient to use Reduce in cases where you are only
interested in real (as opposed to complex) solutions to equations; simply add the third argument
Reals. As this is usually the situation in calculus courses, Reduce may be your first choice as a
288 Multivariable Calculus
solver. On the flip side, Reduce does not (indeed, in general it cannot, as we will see in the next
example) return output in the form of replacement rules. And we like replacement rules. They are
enormously convenient when they are produced programmatically, so you do not have to type
them. In cases where Reduce produces reasonably simple output, a call to ToRules will convert the
4x
output into replacement rules. Just beware that an equation such as y m will be converted to the
5
4x
rule y , which means that replacements should be made with ReplaceRepeated (//.) rather
5
than ReplaceAll (/.):
In[90]:= ToRules#'
x 0, y !, x !!
4x 125 4x
Out[90]= ,y
5 288 5
In[91]:= x, y s.
0, !, !!
4x 125 4 x
Out[91]= ,
5 288 5
0, 0, !!
125 25
Out[92]= ,
288 72
Here is a second example. We identify critical points using Reduce rather than Solve, as Solve
simply will not work in this case. It is important to point out something that is rarely emphasized in
calculus texts: the set of critical points may be far more complex than a few isolated points (like the
two critical points we found in the previous example). For instance:
f Sin$x Cos$y((;
Short$Reduce$x f m 0, y f m 0 , x, y , Reals(, 10(
In[93]:=
Out[94]//Short=
2 S C#1' «« y m ««
S S
C#1' ± Integers && x m 0 && y m 2 S C#1'
2 2
There are five solutions, and only the first and last are shown (since we wrapped the input above in
Short each of the middle three are indicated by <<1>>). Even the abbreviated output is a bit intimi-
dating. Note that there are two constants, C[1] and C[2], that are permitted to assume integer
values. Reduce has found an infinite family of points and curves, parameterized by these two
constants. If you are only concerned with critical points within a bounded domain the output can be
greatly simplified, often with such parameters removed. Below we include in the list of equations
the bounds on both x and y. The constants are no longer needed.
f Sin$x Cos$y((;
Reduce$x f m 0, y f m 0, 6 x 6, 3 y 3 , x, y , Reals(
In[95]:=
) «« y m ArcCos% ) ««
S S S
Out[96]= 6 x && y m ArcCos%
2 2x 2x
) «« y m ArcCos% ) ««
1 S S
6 x S Sec#3' && y m ArcCos%
2 2x 2x
) «« y m ArcCos% ) ««
1 S S
S Sec#3' x 6 && y m ArcCos%
2 2x 2x
) «« y m ArcCos% ) ««
S S S
x 6 && y m ArcCos%
2 2x 2x
) «« y m ArcCos% ) ««
3 3S 3S
6 x S Sec#3' && y m ArcCos%
2 2x 2x
) «« y m ArcCos% ) ««
3S 3S 3S
6 x && y m ArcCos%
2 2x 2x
) «« y m ArcCos% ) ««
3S 3S 3S
x 6 && y m ArcCos%
2 2x 2x
) «« y m ArcCos% ) ««
3 3S 3S
S Sec#3' x 6 && y m ArcCos%
2 2x 2x
«« y m
S S
x m 0 && y m
2 2
&& means “and” and || means “or.” The very last line shows two discrete critical points, at ,0, 0
Okay, this is still rather intimidating. But be patient; the output rewards careful reading. Recall that
S
and ,0, 0. Everything else shows a bounded domain on x, and y as a function of x on this domain.
2
S
2
In other words, the other critical points are comprised of curves.
Let’s embark on a brief visual investigation. In the following graphic, the solid mesh lines are curves
where the partial derivative with respect to x is zero, and dashed mesh lines are curves where the
partial derivative with respect to y is zero. Critical points are points where both partials are simultane-
ously zero. These can be discrete points (where the solid and dashed lines cross), or curves (where
they coincide). This function has both types.
290 Multivariable Calculus
Out[98]=
Do you see the two discrete critical points that we identified earlier? They appear to be saddles. We
confirm this below:
We note that for this function, Maximize does not produce any output, while NMaximize reports,
as always, one maximum (even though there are in fact infinitely many on this domain). Clearly,
the approach we applied here using Reduce is far more comprehensive.
In cases where even Reduce cannot find a critical point, one has the commands FindMaximum
and FindMinimum. Like the command FindRoot (introduced in Section 4.7 on page 184), these are
your weapons of last resort. They require an initial guess for each variable, and using those as
starting values they hone in on a single extremum. Moreover, they are numerical tools; the solution
they provide is only approximate.
Despite these limitations FindMaximum and FindMinimum can be highly effective at refining an
approximate guess. They are also fast and extremely robust. Use them when other methods fail. One
simple means of using these commands is to start with a ContourPlot, and use it to identify a local
extremum. Then, using the approximate coordinates of the extremum as your initial guess, invoke
FindMaximum or FindMinimum. Here’s an example where we use this technique to approximate
some relative extrema for a rather nasty function:
6.2 Real-Valued Functions of Two or More Variables 291
In[100]:= Clear#f';
)Sin% );
5x 3y
f ÆSin#x y' Cos%
y2 1 x2 1
ContourPlot$f, x, 2, 2, y, 2, 2 (
Out[102]=
There appears to be a tiny island near +1, 1/, suggesting that f assumes a relative maximum value
there. Now that we have it in our sights, let’s zero in on it:
Similarly, there appears to be a relative minimum near +1, 1/; let’s zero in on it:
Outstanding!
Constrained Optimization
The technique of Lagrange multipliers is easily implemented in Mathematica. Set things up so that
the function you wish to optimize is called f , while the constraint is of the form g 0. We wish to
solve the system ´ f O ´ g , for some real constant O, together with the constraint equation g 0.
This is easily accomplished using the grad command defined on page 282. Here’s a simple example:
maximize the quantity 4xy under the constraint that 4x2 y2 8.
x 1, y 2, O !, x 1, y 2, O !,
y y
Out[109]=
2x 2x
In[111]:= f s. sols
Note that the Maximize, Minimize, NMaximize, and NMinimize commands will do this all in one
go, but they will only find a single solution. If that’s all you need, there is no easier way to get there:
Regardless which approach is taken, a ContourPlot provides a visual verification. One could make a
ContourPlot of f , and a second ContourPlot with the first argument g m 0, then display them
together with Show. Below we take a different approach, making a single plot with a mesh line to
display the constraint curve. The solutions are shown as red dots:
Out[113]=
28
Out[114]=
9
x3 y 3
Out[115]= 5xy
9
In the examples above we integrated first with respect to x, then with respect to y. That is, the
variables are specified within Integrate in the same order that the integral signs are written in
standard mathematical notation. The palette version of the Integrate command makes the order of
integration more transparent. First type and highlight the function you wish to integrate, then push
the appropriate integration button on the BasicMathInput palette and fill in the placeholders for the
innermost integral, using the Í key to move from one placeholder to the next. Now highlight the
entire expression and push the integration button a second time, fill in the placeholders, and enter:
à à ,5 x y 0Šx Šy
3 2
2 2
In[116]:=
1 0
28
Out[116]=
9
294 Multivariable Calculus
à .à ,5 x y 0Šx2Šy
2 2
In[117]:=
x3 y 3
Out[117]= 5xy
9
à à ,5 x2 y2 0Šx Šy
2 4y2
In[118]:=
2 4y2
52 S
Out[118]=
3
And here we integrate the function f ,x, y, z0 y z2 over the region bounded by a sphere of radius
two centered at the origin:
à à à ,y z2 0Šx Šy Šz
2 4z2 4y2 z2
In[119]:=
2 4z2 4y2 z2
128 S
Out[119]=
15
This last integral may take a minute or so to evaluate. A more sensible approach, even with a tool as
powerful as Mathematica, is to use a spherical coordinate system. See the subsection “Integration in
Other Coordinate Systems” in Section 6.4, page 322, for details.
Finding Bounds of Integration and Plotting Regions in the Plane and in Space
While Mathematica makes the evaluation of most integrals a snap, you still have to set up those
integrals in the first place. And when the region over which the integration takes place is non-
rectangular, this can be a subtle and challenging enterprise in itself. However, if the region in
question is defined by one or more inequalities, the command CylindricalDecomposition will do
this work for you. For instance, to find the bounds used in the last example for integration over a
spherical region of radius 2 centered at the origin, one may simply do this:
Similarly, the circular region in the example preceding this one can be decomposed as follows:
Out[121]= 2 y 2 && 4 y2 x 4 y2
The second argument to CylindricalDecomposition is a list of the coordinate variables, and the
order in which they are listed is very important. It should match the order in which the integral
signs appear. To decompose complex regions, you may wish to experiment with the ordering to find
the one that leads to the most simple decomposition. Also, use strict inequalities when describing
regions of integration, as this will often simplify the output.
You can also make a plot of the region determined by your bounds of integration. This provides a
visual confirmation for your choice of bounds. One produces the plots using RegionPlot for planar
regions, and RegionPlot3D for regions in space. Here, for instance is the circular region used in the
double integral above. Note that the first argument uses the bounds of the inner integral, and is
presented as an inequality (it makes no difference whether strict or non-strict inequalities are used).
The bounds of the outer integral are given implicitly via the iterator for y:
Out[122]=
And here is a view of the volume that this double integral represents. We use Plot3D with both a
RegionFunction setting (to show the plot of the integrand over the circular region) and a Filling
setting (to show a translucent solid region under the graph of the integrand). Note also that Plotg
Range is needed to extend the image all the way down to the x-y plane.
296 Multivariable Calculus
In[123]:= Plot3D%5 x2 y2 , x, 2, 2, y, 2, 2 , Mesh None, PlotRange 0, 5,
Out[123]=
Out[124]=
Here the first argument to RegionPlot3D could be stated more simply as Norm[{x,y,z}] 2. We
stuck here with the inequalities suggested by the bounds of integration, precisely to test that these
bounds produce an appropriate image.
6.2 Real-Valued Functions of Two or More Variables 297
Below we construct multiple cut-away views of this solid ball, with MeshFunction and MeshShadg
ing settings applied to color the ball according the value of the integrand y z2 . If this function
gives the density of the ball at the point ,x, y, z0, for instance, then the triple integral gives the ball’s
mass. Note that the setting BoxRatios Automatic is needed to give all axes the same scale (by
default RegionPlot3D will scale the axes in order to create a cubical bounding box).
GraphicsRow$Table$RegionPlot3D$Norm$x, y, z ( 2,
x, 2, rightSide , y, 2, 2 , z, 2, 2, BoxRatios Automatic,
In[125]:=
Out[125]=
Exercises 6.2
1. In this exercise you will explore the ColorFunction option for Plot3D. ColorFunction may be
set to any pure function with three variables (one for each coordinate position). Pure functions
are discussed in Section 8.4. By default, each of the input values for this function are scaled from
the actual coordinate values to span the range from 0 to 1. In order to use the actual coordinate
values, the additional option setting ColorFunctionScaling False must be added. Moreover
the output of the ColorFunction must be a Hue, RGBColor, or other color directive (such as a
named gradient like ColorData["StarryNightColors"]). The inputs to functions such as Hue
should span the values from 0 to 1. In practice, this means that most “interesting” color func-
tions will have to be Rescaled before being suitable for input to Hue or whichever color render-
ing function you plan to use.
a. Make a Plot3D of the function Æsin+x y/ on the domain 2 x 2, 2 y 2 with the option
setting ColorFunction (#1&). Repeat for (#2&) and (#3&). Note that by setting an explicit
color function, the default Lighting setting switches to using white light only, so as not to
interfere with your choice of color.
2 y 2 with the option setting PlotRange All. Estimate the minimum and maximum
values obtained by the function on this domain.
298 Multivariable Calculus
c. Repeat the previous part, but this time plot the function
,x Cos$x y(0 ,y Cos$x y(0 ,{0,4}]. What do you notice about its minimum
2 2
Rescale[ÆSin#x y'
In[126]:= Plot3D%ÆSin#x y' , x, 2, 2, y, 2, 2 , ColorFunctionScaling False,
Out[126]=
e. The following output wraps the color rendering function in Glow, which means that it will
not react with any Lighting. Can you see any difference from the last input?
In[127]:= Plot3D%ÆSin#x y' , x, 2, 2, y, 2, 2 , ColorFunctionScaling False,
Out[127]=
2. Use TabView to construct a dynamic display of a Plot3D object like the one shown below where
MaxRecursion may be set to any of the values 0 to 3.
6.2 Real-Valued Functions of Two or More Variables 299
0 1 2 3
4. Make two images side by side, one showing a ContourPlot of your favorite function, and the
other a Plot3D of that same function on the same domain. Use MeshFunctions to display level
curves in the Plot3D. Use a specific Range of values for the Contours settings on the Contourg
Plot, and use that same collection of values for Mesh settings on the Plot3D. This way you will
synchronize the level curves being shown in each plot. Use a ColorFunction or MeshShading to
apply "LakeColors" to your Plot3D.
5. Write a command called key that will make a graphical gradient key for a ContourPlot as shown
on page 272. It should have the syntax structure key[{min,max},contours], where min and max
denote the minimum and maximum function values in the plot, and contours is the number of
contour regions in the plot.
x0
y0
z z
0. y
7. Describe the set of critical points for the function f ,x, y0 sin,x2 y2 0 on the domain 2 x 2
and 1 y 1. Use Reduce with the second argument set to {y,x} and again with the second
argument set to {x,y}. Is one output easier to interpret?
a. Show that f has no critical points. Make a ContourPlot with MeshFunctions and Mesh
settings to display the curves where the partial derivative with respect to x is zero, and those
where the partial derivative with respect to y is zero. Confirm visually that these two sets of
curves never cross.
function f has a point ,x0 , y0 0 in its domain where the directional derivative in the direction of
b. Show that for any direction measured by the polar angle T, and any real number r ! 0, the
T exceeds r , and another point ,x1 , y1 0 where the directional derivative in the direction of T is
less than r . Essentially, the steepness of this function is unbounded in every direction, despite
being defined on the entire plane, and having no relative minima, maxima, or saddle points.
ables, such as f ,x, y0 2x y on the domain 1 x 1, 1 y 1. How does this differ from using
9. Explain how to use ContourPlot3D to view the graph of any real-valued function of two vari-
10. Maximize the quantity x1s3 y2s3 under the constraint that 40x 50y 10 000 .
12. Consider the function f ,x, y0 2 1 x2 defined over the unit disk in the x-y plane centered at
the origin.
a. Make a Plot3D of f over this circular region, and use Filling to display the solid under the
graph of f and above the x-y plane.
b. Find the volume of this solid.
6.3 Parametric Curves and Surfaces 301
c. Make the following Manipulate, showing the graph from part a together with a movable
square cross-section. If one first integrates with respect to y, the inner integral is equal to the
area of this square cross-section.
13. Find the approximate value of the double integral of the function f ,x, y0 2x y over the region
R in the x-y plane bounded by y sin x and y 1 x . Make a three-dimensional sketch of the
2
the torus according to distance from the origin, with points closest to the origin appearing red,
and the most distant points appearing blue. (The ColorData["TemperatureMap"] gradient is
perfect for this.) Finally, display that portion of the surface with 2 x 4 and 0 y 4.
each value of a variable t , the function returns a vector <x+t/, y+t/@ in the plane. As t varies
A parametric representation of a curve is a continuous vector-valued function of one variable; for
continuously through an interval, the vectors trace out a curve. Since a vector in Mathematica is
represented as a List, a parametric curve can be defined as a List of two or more real-valued
functions. The first function represents the x coordinate, the second the y coordinate. Since there is
only one variable, we revert to the paradigm for defining functions used in Chapters 3 through 5,
with an underscore after the independent variable on the left side, and using SetDelayed (:=).
You can now find your position in the plane for any value of t :
302 Multivariable Calculus
s% )
S
In[2]:=
4
!
1 S 1
Out[2]= ,
4
2 2
0.5
Out[3]=
5 10 15 20 25 30
0.5
1.0
ParametricPlot takes two arguments. The first is the function you wish to plot, and the second is
an iterator for the independent variable (t in this example). ParametricPlot will tend to give both
axes the same scale unless you explicitly tell it not to. Many curves, circles for instance, look better
that way, so that’s what happens by default. Be aware that in plots like the one above where one
axis is far longer than the other, an AspectRatio setting will be in order.
You need not restrict yourself to simple functions. ParametricPlot works well on Piecewise func-
tions, and even on interesting curves like this:
0.5
Out[5]=
0.5 0.5
0.5
1.0
¿ Note that the definition of c+t/ above uses Set (=) rather than SetDelayed (:=). This causes the
two integrals on the right side of the definition to be evaluated once, when the c[t_]= cell is
6.3 Parametric Curves and Surfaces 303
entered. It is the expressions that are the values of these integrals (they happen to be Fresnel
functions) that are assigned to c+t/, and that are then plotted. If SetDelayed had been used in
defining c+t/, then the integrals would have to be worked out anew for each input value t used
to create the plot (and there are several hundred such values). It would have slowed the
process of plotting by several orders of magnitude.
While a ParametricPlot shows the set of points of the form ,x+t/, y+t/0 as t runs through all values in
an interval, it does not give any indication of which point goes with which input. Manipulate can
be harnessed to trace out a parametric curve, with a slider to control the independent variable. Here,
for instance, is a standard parameterization of the unit circle. Note the PlotRange setting (which
keeps the plot range fixed as t varies), and the small but positive starting value 0.01 for the endpoint
t (one needs distinct starting and ending values for the independent variable in any ParametricPlot).
Manipulate$Show$
ParametricPlot$Cos#t0', Sin#t0', t0, 0, t, PlotRange 1(,
In[6]:=
1.0
0.5
Out[6]=
0.5
1.0
One can also define a custom parametric plotting command that will apply a color gradient to the
curve, so that it will, for instance, start in green and gradually progress through the color gradient to
end in red (green for go, red for stop). Here’s a simple implementation, but you’ll need to try it in
order to see the colors on your monitor.
304 Multivariable Calculus
Out[7]=
¿ The ColorFunction accepts any or all of three arguments. The first two are the x and y
coordinates of the parametric curve. The third (#3 used above) is the independent variable t .
By default, the values of t will be scaled to run from 0 to 1 before being input to the ColorFuncg
tion, regardless of the domain you choose. Hence the option setting above produces a color
gradient that starts at Hue[.3] (green) and ends at Hue[1.] (red). This same setting may be used
to color any ParametricPlot according to this gradient. Exercise 4 shows how to color a
parametric curve according to its curvature.
We note that ParametricPlot accepts most of the options accepted by other two-dimensional
plotting commands such as Plot. The PlotPoints option, for example, can be set to a numerical
value (such as 100) if you see jagged segments where you suspect they should not be. However, the
adaptive algorithm employed by ParametricPlot is both speedy and robust; it is rare to find cases
where it does anything less than an excellent job. The following Manipulate may help to convince
you of this:
In[8]:= Manipulate%
1 1 1 1
ParametricPlot%Cos#t' Cos#7t' Sin#a t', Sin#t' Sin#7t' Cos#b t'!,
2 2 2 2
t, 0, 2S, Axes False, PlotRange 2), a, 17, 5, 25, b, 12, 5, 25)
6.3 Parametric Curves and Surfaces 305
Out[8]=
The derivative of the parametric function <x+t/, y+t/@ is <x '+t/, y '+t/@. You can differentiate a parametric
function just as you did a single-variable function in Chapter 5, or like we did for multivariable
functions in the previous section of this chapter, using :
D#s#t', t'
1 Sin#t', Cos#t'
In[9]:=
Out[9]=
In[10]:= s '#t'
t s#t'
1 Sin#t', Cos#t'
In[11]:=
Out[11]=
¶
Note that while the ParametricPlot of s+t/ has sharp corners (it is the first plot shown at the begin-
¶
shows the derivative vector s '+t/ with its tail at the point s+t/, as t varies. When t is S s 2 or 5S s 2,
ning of this section), its derivative is defined everywhere. This can happen. The Manipulate below
sin t 1 and cos t 0, and so the derivative is the zero vector. This happens at the top of each sharp
corner in the plot.
Module$s,
s#tB' t Cos#t', Sin#t';
In[12]:=
Manipulate$Show$
ParametricPlot$s#t0', t0, 0, 10, PlotRange 0, 10, 2, 2(,
Graphics#Arrow#s#t', s#t' s '#t'''
(,
t, 4.8, 0, 10((
306 Multivariable Calculus
Out[12]=
2 4 6 8 10
1
2
¶ ¶
If s+t/ represents the position of a particle at time t , then its velocity vector is s '+t/, and its speed is the
magnitude of this vector. To compute speed, say at time t 3, you can do this:
Out[13]= 1.31063
You can even get a formula for speed as a function of t . Here we produce and Simplify the formula,
using the optional second argument for Simplify in order to specify that t is permitted to assume
only real values (as opposed to complex values). We could have given the second argument as
Element[t,Reals]. The infix form of the Element command is invoked with the ± symbol, which
can be found on the third row of small buttons on the BasicMathInput palette.
Out[14]= 2 2 Sin#t'
Note that this is consistent with the Manipulate above; speed is zero precisely when t is S s 2, 5S s 2,
etc.
Unit tangent vectors are constructed exactly as you would expect. The use of Simplify as above will
generally serve you well.
D#s, t'
In[15]:= unitTangent#sB, tB' : Simplify% , t ± Reals)
Norm#D#s, t''
!
1 Sin#t' Cos#t'
Out[17]= ,
2 2 2 Sin#t'
The unit tangent vector at a specific value of t can then be obtained via a simple replacement:
6.3 Parametric Curves and Surfaces 307
Unit normal vectors can be formed in a similar way (although FullSimplify does a better job than
Simplify in this case):
D$unitTangent#s, t', t(
In[19]:= unitNormal#sB, tB' : FullSimplify% , t ± Reals)
Norm$D$unitTangent#s, t', t((
!
Cos#t' 1 Sin#t'
Out[20]= ,
2 2 Sin#t' 2
Even though neither the unit tangent nor the unit normal is defined when t S s 2 or t 5S s 2, the
following Manipulate works fine, as it is unlikely to sample these precise values. We define auxil-
iary commands ut and un so that unitTangent and unitNormal only need to be called once (they
are slow, after all, since they use Simplify and FullSimplify, respectively, each time they are called).
The auxiliary commands ut and un are defined using Set (=). So they use the formulas generated by
unitTangent and unitNormal, and simply replace the variable t by whatever argument x is speci-
fied. They are speedy!
t0
Out[21]=
2 4 6 8 10
1
2
308 Multivariable Calculus
Curvature is also a simple calculation. We use the Greek letter N (kappa) to denote this quantity. Find
N on the BasicMathInput palette, or type È k È.
)!),
1 1
Graphics%Gray, Circle%s#t0' un#t0',
curv#t0' curv#t0'
1
Graphics%Red, Arrow%s#t0' un#t0', s#t0'!)!)
curv#t0'
), t0, 6.5, 0, 10))
t0
Out[24]=
2
5 10
2
6.3 Parametric Curves and Surfaces 309
s#tB' :
t2 t2
In[25]:= Sin#t', Cos#t', t!
50 50
To plot a parametric function in space, use the command ParametricPlot3D:
20
Out[26]=
z
10
0
10
5
0
x
5
ParametricPlot3D takes two arguments. The first is the function you wish to plot (a List of three
coordinate functions), and the second is an iterator for the independent variable. We added an
AxesLabel option to make it easy to identify which direction is which after you rotate the figure
with your mouse. Many of the other options for the other 3D plotting commands are applicable
here; see the subsection of Section 6.2 called “Options for 3D Plotting Commands” on page 258.
One can also use MeshShading or ColorFunction settings in exactly the same manner as for 2D
parametric plots. For example, here we color a curve according to the value of the independent
variable:
310 Multivariable Calculus
Out[27]=
Differentiation, integration, unit tangents, unit normals, and curvature work exactly as for 2D
parametric functions (see the previous subsection). In the case of three-space, however, there is a well
known alternate formula for curvature based on the cross-product. It is a simple matter to program a
command based on this formula:
,2500 4 S2 S4 0
Out[29]=
3s2
,2500 4 t2 t4 0
Out[30]= 50
3
6.3 Parametric Curves and Surfaces 311
The image of each coordinate pair +u, v/ is a point in space, a 3-tuple ,x+u, v/, y+u, v/, z+u, v/0, where x,
parameterization of a surface is a continuous function from a rectangle in the plane to three-space.
Mathematica is instrumental in visualizing the amazing spectrum of surfaces that can be constructed
in this manner. Here is an example. It illustrates that just as a parametrically defined curve can
intersect itself (if the coordinate functions assume the same values at two or more distinct values of
t ), so too can a parametrically defined surface intersect itself. This surface is known as the Whitney
umbrella, named after the American mathematician Hassler Whitney (1907–1989).
Clear#), u, v';
u v, u, v2
In[31]:=
)
Out[32]= u v, u, v2
Out[33]=
The rectangular region in the u-v plane with 3 u 3 and 2 v 2 is mapped continuously via )
to the surface in 53 shown above. As with parametric curves, the plot does not show the domain,
but only the image of the domain after the parametric function is applied.
Out[35]=
For parametric surfaces, a mesh function can accept up to five arguments. In order, the arguments
the independent variables. The default setting for the MeshFunctions option is Ó4 &, Ó5 &,
are x, y, z, u, and v. The first three are the coordinates in space of the surface, while the last two are
meaning that the image under ) of uniformly spaced rectangular grid lines on the domain rectangle
are shown. More information can be gained by using Mesh, MeshFunctions, and MeshShading to
apply a color gradient according to increasing values of one of the independent variables. This is
accomplished in exactly the same way as described in the subsection of Section 6.2 called “Options
for 3D Plotting Commands” on page 258. Below we do this for the first independent variable, u,
used in the torus. The “seam” is easily visible (on a color monitor) as the sharp boundary between
red and blue.
ParametricPlot3D$), u, 0, 2S, v, 0, 2S, Mesh 10, MeshFunctions Ó4 &,
MeshShading Table$ColorData$"TemperatureMap"(#k', k, 0, 1, .1(,
In[36]:=
Lighting "Neutral"(
Out[36]=
Differentiation works just as with parameterized curves. Here is the partial derivative with respect to
v:
v )
Cos#u' Sin#v', Sin#u' Sin#v', Cos#v'
In[37]:=
Out[37]=
It is easy to use the concept of a parametric surface to generate a surface of revolution. You may recall
surfaces of revolution from single-variable calculus; indeed, in Section 5.13 on page 242 we dis-
from revolving the function z f +x/ about the z axis. However, this command cannot be used to
cussed the built-in command RevolutionPlot3D, which will plot the surface of revolution obtained
6.3 Parametric Curves and Surfaces 313
rotate the graph of such a function about the x axis. One may easily create a custom command for
this purpose as follows:
f#xB' : 1 x2 ;
GraphicsRow#Plot#f#x', x, 1, 1', xRevolutionPlot3D#f#x', x, 1, 1''
In[39]:=
Out[40]=
Exercises 6.3
unit circle is ;cos t, sin t?, where 0 t 2S. Verify that the parametric function
2. There are many different parameterizations of the same curve. A standard parameterization of the
= A, where 0 t 2S, also parameterizes the unit circle for those values of t
sin+4t/sin+6t/ cos+4t/cos+6t/
,
S s 5).
2sin+5t/ 2sin+5t/
where it is defined (e.g. it is not defined at integer multiples of t
¶
3. Consider the vector-valued function r +t/
;2sin+t/, cos+t/, sin+2t/?.
¶
a. Superimpose a ParametricPlot3D of r +t/ with a ContourPlot3D of the surface z x y. Use the
option Mesh None in your plot of the surface. What do you find?
b. Explain why the curve lies on the surface. Hint: you will need the double-angle formula for
the sine function. If you have forgotten it, type TrigExpand[Sin[2t]].
4. Use ColorFunction to color a ParametricPlot of the function =t, Æt A on the domain 2 t 2
2
according to its curvature with the "TemperatureMap" color gradient. While this method pro-
314 Multivariable Calculus
duces continuous color transitions (as opposed to the discrete color values produced when using
MeshFunctions for this purpose), it requires your knowing the maximum and minimum curva-
ture values for your specific function on your specific domain.
where each independent variable ranges from 0 to 2S, according to values of the independent
variable v. Use the color gradient ColorData["TemperatureMap"] with ten gradations. Use the
resulting graphic to identify the “seam” in the torus created by this variable.
A point in polar coordinates is represented as an ordered pair +r, T/, where r is the distance from the
Conversion to and from Polar Coordinates
point to the origin, and the angle T is measured in radians, counterclockwise, from the positive x
axis to the segment connecting the origin to the point.
To convert between polar and Cartesian coordinates, one makes use of the following triangle and
some basic trigonometry:
r
y
T x
So in converting from polar to Cartesian coordinates, one uses the formulas x r cos T and y r sin T.
x2 y2 , and so long
y t x. This last equation, when solved for T, can be expressed as follows:
In converting from Cartesian to polar coordinates, one uses the formulas r
as x 0, tan T
arctan,y t x0 x!0
arctan,y t x0 S x 0 and y 0
T arctan,y t x0 S x 0 and y 0
Ss2 0 and y ! 0
S s 2
x
x 0 and y 0
6.4 Other Coordinate Systems 315
This expression for T is clearly a bit messy. The formula above will produce a value of T with
ArcTan command usually takes a single number as its argument, and returns a value between S s 2
S T S. Mathematica makes the calculation of T much easier with its ArcTan command. The
and S s 2, the arc tangent of that number. But you can also feed it an x-y pair. ArcTan[x, y] will
return the polar angle T for the point with Cartesian coordinates ,x, y0, with S T S. That is, it
will essentially invoke the complex formula above. So life is easy after all: in converting from
Cartesian to polar coordinates, T ArcTan[x, y]. For instance:
, , !
S S 3S 3S
Out[1]= ,
4 2 4 4
It is now a simple matter to automate the conversion process by creating the following commands.
Note that you can type T from the keyboard via the key sequence È th È. It is also available on the
BasicMathInput palette.
In[4]:= toPolar%1, 3)
2, !
2S
Out[4]=
3
)
S
In[5]:= toCartesian%10,
12
5 .1 3 2 5 .1 3 2
Out[5]= , !
2 2
f +T/. For
Plotting in Polar Coordinates
The built-in command PolarPlot can be used to view the graph of the polar function r
example:
316 Multivariable Calculus
, T, , !)
1 5S S
In[6]:= PolarPlot%
1 Sin#T' 4 4
2.0
1.5
Out[6]= 1.0
0.5
2 1 1 2
0.5
Out[7]=
Like Plot, PolarPlot will accept a list of functions as its first argument (so that multiple functions
can be simultaneously plotted). It accepts most of the same options accepted by Plot, so you will be
pleased to find that you’re already an expert in its usage. For example:
Out[8]=
6.4 Other Coordinate Systems 317
Put two underscores after args on the left side of the definition (two underscores means that args
represents one or more arguments, separated by commas). In this case args stands for the required
iterator for the independent variable and for any options that might be added. Here’s an example:
Out[13]=
In[14]:= Needs$"VectorAnalysis`"(
Now you will have access to the commands CoordinatesToCartesian and CoordinatesFromCarteg
sian. Each of these commands takes two arguments. The first is the point whose coordinates you
want to translate, and the second is the name of the coordinate system to or from which the transla-
tion should occur:
318 Multivariable Calculus
2,
3S
Out[15]= , 3!
4
3S
In[16]:= CoordinatesToCartesian%2, , 3!, Cylindrical)
4
Out[16]= 2, 2 , 3!
Working with spherical coordinates in this package demands that you pay careful attention to
spherical coordinates are of the form U, I, T, where U is the distance from the point to the origin, I
Mathematica’s conventions for this coordinate system. By default, nonzero points expressed in
is the angle from the vector determined by the point to the positive z axis, and T is the angle used in
polar and cylindrical coordinates. The second and third coordinate positions are transposed in many
standard calculus texts, so beware!
S
Out[17]= 2, , 0!
4
Best of all, you can use these conversion commands to help you remember the conversion formulas:
), ArcTan$x, y(!
z
Out[21]= x2 y2 z2 , ArcCos%
x 2 y2 z 2
If you are not familiar with the ArcTan[x, y] convention, see the subsection “Polar Coordinates” at
the beginning of this section on page 314.
¿ It is worth noting that the VectorAnalysis package supports more than a dozen coordinate
systems. Cartesian, cylindrical, and spherical are simply the most common. For more informa-
tion, type “vector analysis package” into the search field in the Documentation Center.
6.4 Other Coordinate Systems 319
Out[22]=
Here we plot the paraboloid f ,x, y0 x2 y2 , shown over a circular domain of radius 2, with polar
angle T between 0 and 3S s 2:
Out[23]=
320 Multivariable Calculus
This command simply invokes ParametricPlot3D after converting the arguments from cylindrical
to Cartesian coordinates. Be sure to put two underscores after args on the left side of the defining
equation; two underscores mean that args stands for a sequence of one or more arguments. In this
case, args represents the required iterator for the independent variable t and any options you might
add—this will allow you to use any of the options allowed by ParametricPlot3D.
Here is an example of a curve that resides along the cylinder whose equation in cylindrical coordi-
nates is r 1:
Out[25]=
Out[26]=
Out[27]=
In[28]:= SphericalPlot3D#T, I, 0, S, T, 0, 7S s 2, Boxed False, Axes False'
Out[28]=
Most of the options that can be used with the command Plot3D will also work for SphericalPlot3D.
A discussion of these options can be found in the subsection “Options for 3D Plotting Commands”
of Section 6.2 on page 258.
322 Multivariable Calculus
This command simply invokes ParametricPlot3D after converting the arguments from spherical to
Cartesian coordinates. Be sure to put two underscores after args on the left side of the defining
equation; two underscores mean that args stands for a sequence of one or more arguments. In this
case, args represents the required iterator for the independent variable t and any options you might
add—this will allow you to use any of the options allowed by ParametricPlot3D.
Here is an example of a curve that resides on the sphere whose equation in spherical coordinates is
U 1:
Out[30]=
For example, we evaluated the double integral of the function f ,x, y0 5 x2 y2 over the disk of
radius 2 centered at the origin back in the subsection “Integration of Functions of Two or More
Variables” of Section 6.2 on page 293. This is more easily handled in polar coordinates. We convert
the integrand into polar coordinates as follows:
6.4 Other Coordinate Systems 323
Clear$x, y, r, T(;
5 x2 y2 s. x r
Cos#T', y r
Sin#T'
In[31]:=
We can now integrate, replacing Å x Å y with r Å r Å T. Since the region of integration is a disk, the
bounds of integration are easily described in polar coordinates: 0 r 2 and 0 T 2S.
à à rŠr ŠT
2S 2
In[33]:=
0 0
52 S
Out[33]=
3
And how about the triple integral of the function f ,x, y, z0 y z2 over the region bounded by a
sphere of radius two centered at the origin? We did this one in Cartesian coordinates earlier as well.
We accomplish the same result in spherical coordinates as follows. We first convert the integrand
into spherical coordinates. Now what were those conversion formulas?
In[34]:= Needs$"VectorAnalysis`"(
In[37]:= Thread$x, y, z (
¿ The Thread command is used here to “thread” Rule () over the lists of variables and conver-
sion expressions. The FullForm of the input above is Thread[Rule[List[x, y, z], List[]]].
Thread has the effect of distributing Rule over the lists, producing a list of rules rather than a
rule of lists. So the output has the form List[Rule[x,], Rule[y,],Rule[z,]]. Thread is
discussed in Section 8.4 on page 410.
In[38]:= y z2 s.
And now we integrate, replacing Å x Å y Å z with U2 sin+I/ Å U Å I Å T. Since we wish to integrate over a
sphere of radius 2 centered at the origin, we choose as our bounds of integration 0 U 2, 0 I S,
and 0 T 2S. The result agrees with our earlier output, but it evaluates far more quickly due to the
more simple bounds of integration.
à à Ã
U Sin#I'Å U Å I Å T
2S S 2
2
In[39]:=
0 0 0
128 S
Out[39]=
15
Exercises 6.4
r f +T/ sin+n T/, where n is allowed to vary from .01 to 3, and the variable T assumes values from
1. Make a Manipulate with two controllers that displays a graph of the polar function
0 to length, where length is permitted to vary from 2S to 100S. The resulting curves are known as
roses.
to view the graph of the polar function r f +T/. Test your solution on the function f +T/ T.
2. Explain how to use ParametricPlot to produce the same output as that produced by PolarPlot
3. Explain how to use ParametricPlot to produce the graph of the inverse polar function T f +r/.
Carry this out on the function f +r/ r 2r 1 as r goes from 0 to 2.
2
4. Make a Manipulate to view various PolarPlots of the superformula with polar equation
-
cos- 1
sin- 1 1
1
mT n2 mT n3 n1
r+T/ . Use sliders that allow m, n1 , n2 , and n3 to range from 1 to 20.
4 4
Restrict m to assume only integer values in this range.
5. A homotopy between two surfaces is a smooth deformation from one surface to the other gov-
homotopy from the Roman surface (t 0) to the Boy surface +t 1/ given by:
erned by a single real parameter t ranging from 0 to 1. Make a Manipulate that illustrates the
3cos2 v
z ,
2t 2 sin+3u/sin+2v/
S S
where u and 0 v S.
2 2
6.5 Vector Fields 325
Clear$f, x, y(;
x4 y4 6x2 y2 1, 4x3 y 4x y3
In[1]:=
Out[2]= 1 x4 6 x2 y2 y4 , 4 x3 y 4 x y3
Clear$g, x, y, z(;
y z, z x, x y
In[3]:=
g
Out[4]= y z, x z, x y
In[5]:= Needs#"VectorFieldPlots`"'
Out[6]=
326 Multivariable Calculus
The first argument to VectorFieldPlot is the vector field to be plotted. It is followed by two iterators,
one for each of the two coordinate variables. Each side of the rectangular domain is subdivided into
15 equal pieces, and at each of the 15 l 15 225 points of the resulting grid the tail of a vector is
placed, the value of the vector field at that point. The lengths of the vectors are scaled so that even
the longest vectors will not overlap one another. You can change the number of vectors displayed
with the PlotPoints option. Set it to a positive integer such as 10 to view a 10 l10 display. Set it to a
list of two such integers such as {10, 5} to view ten columns and five rows of vectors. Lower Plotg
Points settings will speed up the evaluation time, and in some cases, they can produce a more
readable image. Note that by default the same scale is given to each axis.
GraphicsRow$
VectorFieldPlot$y, x , x, 1, 1, y, 0, 1 , PlotPoints 10(,
In[7]:=
Out[7]=
A related and sometimes useful command is GradientFieldPlot. The syntax for this command is
identical to that of Plot3D and ContourPlot. It will essentially apply VectorFieldPlot to the gradient
FieldPlot for the function f ,x, y0 xy. The gradient of this function is <y, x@, precisely the field we
of the function given as the first argument. Below we superimpose a ContourPlot with a Gradientg
plotted earlier. Note that the gradient vector for any point is orthogonal to the level curve through
that point. The gradient points in the direction of steepest ascent.
6.5 Vector Fields 327
Show$
ContourPlot$x
y, x, 1, 1, y, 1, 1 , Contours 20(,
In[8]:=
Out[8]=
Note also that it is important to list the ContourPlot first to create this graphic. Had it been listed
second, the contour plot would be overlaid on top of the vector field plot, and so would obscure it.
The command VectorFieldPlot3D is used to plot three-dimensional vector fields. By default, arrow-
heads are not drawn. The option setting VectorHeads True can be used to add arrowheads.
GraphicsRow$
VectorFieldPlot3D$x, y, z , x, 1, 1, y, 1, 1 , z, 1, 1(,
In[9]:=
VectorFieldPlot3D$x, y, z ,
x, 1, 1, y, 1, 1 , z, 1, 1, VectorHeads True(
, ImageSize 260(
Out[9]=
By default, there are seven vectors drawn in each coordinate direction, for a total of 73 343 vectors
in the plot. As before, this behavior can be modified via the PlotPoints option. And as you might
expect, there is a GradientFieldPlot3D command, whose syntax precisely matches that of Contourg
we show the gradient field for the function f ,x, y, z0 x2 y z, and on the right we add three level
Plot3D. It can be used to plot the gradient field for a real-valued function of three variables. Below
328 Multivariable Calculus
surfaces for this function as well. Again, the gradient vectors are orthogonal to the level surfaces,
and point in the direction of steepest ascent.
GraphicsRow$
GradientFieldPlot3D$x2 y z, x, 0, 1,
In[10]:=
Out[10]=
¶ ¶
Divergence and Curl of a Three-Dimensional Vector Field
The divergence of a three-dimensional vector field f ,x, y, z0 f1 ,x, y, z0i f2 ,x, y, z0j f3 ,x, y, z0k is
the real-valued function
div f ,x, y, z0 x f1 y f2 z f3 .
The curl of f is the three-dimensional vector field
¶ ¶
curl f ,x, y, z0 ,y f3 z f2 0i ,z f1 x f3 0j ,x f2 y f1 0k.
The easiest way to compute divergence and curl with Mathematica is to load the VectorAnalysis
package, which gives you access to the Div and Curl commands.
In[11]:= Needs$"VectorAnalysis`"(
If you will be working in a single coordinate system, say in Cartesian coordinates, go ahead and set
the coordinate system and name the coordinate variables. This should be done once. The default
coordinate system is Cartesian, but the default coordinate variable names are Xx, Yy, and Zz, which
may not be your first choice. Here we set the coordinate variable names to be x, y, and z:
6.5 Vector Fields 329
Out[12]= Cartesian$x, y, z(
Out[13]= 3xy
If you will be doing calculations in other coordinate systems, you simply set the coordinate system and
the coordinate variable names you would like to use. Typical choices are Cartesian[x, y, z], as we
used above, or Cylindrical[r, T, z], or Spherical[U, I, T]. The commands Div and Curl will accept a
coordinate system specification as an optional second argument.
1 r 3 r2 z
Out[15]=
r
0, r2 , !
T
Out[16]=
r
Exercises 6.5
1. The option setting ScaleFactor None may be added to VectorFieldPlot input to turn off the
automatic scaling of vectors. While the vector fields are then drawn with perfect accuracy, the
vectors may overlap one another. This exercise will illustrate that overlapping vectors can be
confusing to view. This is why the default settings include vector scaling that prevents overlap-
ping vectors.
a. Sketch the vector field F,x, y0 <y, x@, without vector scaling.
b. Show that for this field, the head of every vector lies on the line y x.
2. Use Mathematica to verify that the divergence of the curl of any vector field is zero.
330 Multivariable Calculus
¶
Line Integrals
Here is a parameterization of a curve r +t/ that joins the point +1, 1/ to the point +2, 2/ as t runs
from 1 to 2:
1.5
1.0
Out[5]= 0.5
0.5
1.0
Clear#f';
f$xB, yB( : x4 y4 6x2 y2 1, 4x3 y 4x y3
In[6]:=
And here we superimpose a plot of the vector field with a plot of the curve:
In[8]:= Needs#"VectorFieldPlots`"'
In[9]:= Show$VectorFieldPlot$f$x, y(, x, 1, 2, y, 1, 2 , PlotPoints 10(, curve(
Out[9]=
6.6 Line Integrals and Surface Integrals 331
¶
The calculation of the line integral ¼ f º Å r is straightforward. Here is the integrand:
à Št
2
In[11]:=
1
54 102
Out[11]=
5005
In[12]:= N#'
Out[12]= 10.8096
Line integrals in three or more dimensions are just as easy to evaluate. Plots for three dimensions
will require the commands ParametricPlot3D and VectorFieldPlot3D.
Surface Integrals
Here is a surface:
Clear$), u, v, x, y, z(;
x#uB, vB' : ,1 v2 0Sin#u';
In[13]:=
Out[18]=
332 Multivariable Calculus
In[19]:= Clear#f';
In[21]:= Needs#"VectorFieldPlots`"'
Show$VectorFieldPlot3D$f$x, y, z(,
x, 1, 1, y, 1, 1 , z, 1, 1, PlotPoints 8(, surface(
In[22]:=
Out[22]=
The surface integral can be evaluated with ease. Here is the integrand. Be sure to use the cross
product (l) and not the (larger) multiplication operator (×) when pulling that symbol from the
BasicMathInput palette.
In[23]:= f$x#u, v', y#u, v', z#u, v'(.+u )#u, v' l v )#u, v'/
2 ,1 v2 0 ,2 Cos#2 u' 2 v2 Cos#2 u'0 Sin#u' 2 ,1 v2 0 ,Cos#u' v2 Cos#u'0 Sin#u' Sin#2 u'
2
Out[23]=
1
,4 v Cos#2 u' Sin#u' 4 v3 Cos#2 u' Sin#u' 2 v Cos#u' Sin#2 u' 2 v3 Cos#u' Sin#2 u'0
v
In[24]:= Simplify#'
-2 ,6 4 v2 0 Cos#2 u' ,1 v2 0 Sin#u' Sin#3 u' 2 v2 Sin#3 u' v4 Sin#3 u'1
2
à Ã
1 2S
In[25]:= Å u Å v
1 0
32 S
Out[25]=
35
6.6 Line Integrals and Surface Integrals 333
Exercises 6.6
;y,x?
1. Consider the vector field F,x, y0 .
x2 y2
¶
part a.
e. Evaluate the line integral ¼ F º Å r for 0 t 2.
a. Show that this is a gradient field, and superimpose its plot with a contour plot of its potential
¶ ¶
function on the domain 2 x 2, 2 y 2.
b. Evaluate the line integral ¼ F º Å r for any curve r +t/ from +S s 2, 0/ to +S s 2, 0/.
7
Linear Algebra
7.1 Matrices
Entering Matrices
Traditionally, matrices are denoted by capital letters, but in Mathematica you will want to use
lowercase letters, since capitals are reserved for built-in functions. If you really can’t live in a world
where matrices are denoted by lowercase letters, you can use uppercase letters provided you do not
use those letters that are the names of built-in commands or constants: C, D, E, I, K, N, and O.
To enter a matrix in Mathematica first type the name of your matrix followed by an equal sign. Then
select Table/Matrix # New… in the Insert menu. A dialogue box will appear. Select Matrix and enter
the correct number of rows and columns, then click OK. A matrix of the appropriate dimensions will
appear in a fresh input cell with a placeholder for each entry. Click on the first placeholder and type
a value, and then use the Í key to move to the next entry. Enter the cell when you have finished:
2 3 4 5 6 7
1 1 1 1 1 1
In[1]:= mat1 4 5 4 5 4 5
11 2 2 2 2 2
0 0 0 0 0 1
Out[1]= 2, 3, 4, 5, 6, 7, 1, 1, 1, 1, 1, 1, 4, 5, 4, 5, 4, 5, 11, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 1
Look carefully at the output above. Mathematica thinks of a matrix as a list of lists. Each row is
enclosed in curly brackets with entries separated by commas, the rows are separated by commas, and
the entire matrix is enclosed in curly brackets. You can enter a matrix in this form also, but it can be
a little messy:
The command MatrixForm will produce a nicely formatted rectangular array with brackets on the
sides. It is best not to use the MatrixForm command when defining a matrix, as it would then be
impossible to perform some operations. It is better to simply request that the output be in Matrixg
Form whenever you want a nice look at your matrix:
336 Linear Algebra
You can request that Mathematica output every matrix in MatrixForm by typing the following
command at the beginning of a session:
In[5]:= mat1
Out[5]//MatrixForm=
2 3 4 5 6 7
1 1 1 1 1 1
4 5 4 5 4 5
11 2 2 2 2 2
0 0 0 0 0 1
In order to avoid confusion we will continue to affix //MatrixForm to all our inputs in this chapter.
Mathematica is happy to report the dimensions of your matrix. When fed a matrix as input, the
Dimensions command returns a list containing the number of rows and columns in the matrix,
respectively:
Dimensions#mat1'
5, 6
In[6]:=
Out[6]=
There are several commands that produce matrices quickly. To get a 3 l 5 matrix with random
integer entries between 0 and 50, type:
The familiar Table command is easy to use. The next command gives a 5 l 5 matrix whose i, jth
entry is i 2j:
You can also produce a zero matrix by using Table/Matrix # New… in the Insert menu. Just check the
Fill with 0 box. Yet another way is to use the command ConstantArray, like this:
We can produce a 4 l 4 lower triangular matrix with entries on and below the diagonal equal to
i 2j, and above the diagonal equal to 0, by typing:
The If command takes three arguments. The first is a condition or predicate, i.e., an expression that
evaluates to either True or False. The second is the expression to evaluate if the condition is true.
The third is the expression to evaluate if the condition is false. If is discussed in Section 8.5.
The Array command works much like the Table command but uses a function (either built-in or
arguments, the command Array[f , m, n] gives the m l n matrix whose i, jth entry is f ,i, j0. For
user-defined) rather than an expression to compute the entries. For a function f that takes two
example, using the built-in function Min for f produces a matrix where each entry is the minimum
of the row number and column number of that entry’s position:
In[14]:= Clear#f';
f$iB, jB( : i3 j2 ;
Array#f, 2, 3' ss MatrixForm
Out[16]//MatrixForm=
2 5 10
9 12 17
We can use the Array command to produce a general 3 l 4 matrix whose i, jth entry (the entry in
row i and column j) is aij .
Clear#a, mat';
mat Array#aÓÓ &, 3, 4'; mat ss MatrixForm
In[17]:=
Out[18]//MatrixForm=
a1,1 a1,2 a1,3 a1,4
a2,1 a2,2 a2,3 a2,4
a3,1 a3,2 a3,3 a3,4
This can also be accomplished using Table/Matrix # New… in the Insert menu by checking the Fill
with 0 and Fill Diagonal with 1 boxes.
The following command gives a diagonal matrix with the enclosed list on the diagonal:
Or a subdiagonal matrix:
Editing Matrices
It is a simple matter to add another row or column to an existing matrix. Start with either a matrix
generated by the Table/Matrix # New… dialogue box, or any MatrixForm output. To add a row, click
on the matrix just above where you want a new row to appear and press the key combination Ź΢Ï.
A row of placeholders will appear. To add a new column, click on the matrix where you want the
new column to appear. Press the key combination ŹÎ,Ï and a column of placeholders will appear. If
the original matrix appeared in a MatrixForm output cell, the modified matrix will appear in a new
input cell. You can also use the Table/Matrix menu for these tasks: look in the submenu for Add Row
and Add Column:
2 3 4 5 f 6 7
1 1 1 1 f 1 1
f f f f f f f
In[23]:= mat1 ;
4 5 4 5 f 4 5
11 2 2 2 f 2 2
0 0 0 0 f 0 1
To form a new matrix from existing matrices we use the command ArrayFlatten. This command
allows us to use entire matrices as if they are individual entries in a matrix. Thus the following
command will stack the matrices mat1 and mat2 on top of each other. We use curly brackets, {}, to
indicate that each matrix should be treated as a row in the new matrix and then “flattened” into a
single matrix. This command will only return a matrix if the input matrices have the same number
of columns.
7.1 Matrices 341
Out[25]//MatrixForm=
8 1 7 7
6 9 5 6
0 8 0 7
Out[27]//MatrixForm=
0 3 3 6
9 2 8 1
7 7 8 9
To form the matrix consisting of mat1 and mat2 side by side we use curly brackets to indicate that
the individual matrices should form a single row.
One can also form a block matrix. Below we have a matrix comprised of four blocks, where mat1
appears in the upper left position, and mat2 appears in the lower right position. The remaining
positions are comprised entirely of zeros.
342 Linear Algebra
Out[31]//MatrixForm=
8 1 7 7 0 0 0 0
6 9 5 6 0 0 0 0
0 8 0 7 0 0 0 0
0 0 0 0 0 3 3 6
0 0 0 0 9 2 8 1
0 0 0 0 7 7 8 9
Using Grid instead of MatrixForm, we can make the blocks easily visible:
8 1 7 7 0 0 0 0
6 9 5 6 0 0 0 0
0 8 0 7 0 0 0 0
Out[32]=
0 0 0 0 0 3 3 6
0 0 0 0 9 2 8 1
0 0 0 0 7 7 8 9
The Take command can be used to extract submatrices of a given matrix. We’ll use a general 5 l 5
matrix to get a good look at what is happening:
Clear#a, mat';
mat Array#aÓÓ &, 5, 5'; mat ss MatrixForm
In[33]:=
Out[34]//MatrixForm=
a1,1 a1,2 a1,3 a1,4 a1,5
a2,1 a2,2 a2,3 a2,4 a2,5
a3,1 a3,2 a3,3 a3,4 a3,5
a4,1 a4,2 a4,3 a4,4 a4,5
a5,1 a5,2 a5,3 a5,4 a5,5
Take can be used with 2 or 3 arguments. The first argument is the matrix name, the second indi-
cates which rows are desired, the optional third argument indicates the columns. The following
command will return the first three rows of the matrix mat:
The list 1, 1, 2 above indicates that we want to start at the first row, end at the last row, and
So to extract columns we use All for the second entry. To get the last 3 columns enter:
We can extract a submatrix by indicating a range of values for the rows and columns in lists,
Or we could ask for the submatrix consisting of only the first and fourth rows and the second and
fourth columns.
The Span command is an alternative to Take. The notation is similar and slightly more efficient. A
Span is indicated by the ;; symbol. The name of the command is not needed. The previous Take
command is equivalent to the following using Span:
Exercises 7.1
1. Use the Table command to enter a matrix with the integers 1 through 10 on the diagonal, 0
below the diagonal, and 1 above the diagonal.
2. Consider the block matrix shown below. Write a custom command blockMatrix that will
generate matrices of any size of this form. For instance, the output below should result from
blockMatrix[5]//MatrixForm.
7.2 Performing Gaussian Elimination 345
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 2 0 0 0 0 0 0 0 0 0 0 0 0
0 2 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 3 3 3 0 0 0 0 0 0 0 0 0
0 0 0 3 3 3 0 0 0 0 0 0 0 0 0
0 0 0 3 3 3 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 4 4 4 4 0 0 0 0 0
0 0 0 0 0 0 4 4 4 4 0 0 0 0 0
0 0 0 0 0 0 4 4 4 4 0 0 0 0 0
0 0 0 0 0 0 4 4 4 4 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 5 5 5 5 5
In[2]:= mat1327
Out[2]= 6, 9, 5, 6
Or use the e3f7 button found on the BasicMathInput palette. We’ll do this for the remainder of the
chapter, since it looks a bit nicer:
mat1327
6, 9, 5, 6
In[3]:=
Out[3]=
In[4]:= mat133,47
Out[4]= 7
To extract a single column indicate that you want all rows. For example, to get the third column
type:
mat13All,37
7, 5, 0
In[5]:=
Out[5]=
Gaussian Elimination
A matrix is in reduced row echelon form if the first nonzero entry in each row is a 1 with only 0s above
and beneath it. Furthermore, the rows must be arranged so that if one row begins with more 0s than
another, then that row appears beneath the other. Any matrix can be put into reduced row echelon
form by performing successive elementary row operations: multiplying a row by a nonzero constant,
replacing a row by its sum with a multiple of another row, or interchanging two rows.
You can ask Mathematica to find the reduced row echelon form of a matrix by using the command
RowReduce:
In[6]:= Clear#mat';
1 1 4 25
mat 2 1 0 7 ;
3 0 1 1
RowReduce#mat' ss MatrixForm
Out[8]//MatrixForm=
1 0 0 2
0 1 0 3
0 0 1 5
You can also perform “manual” row reduction. Use mat3i7 to refer to the ith row of the matrix mat.
To replace the second row with the sum of the second row and 2 times the first row, type:
Out[10]//MatrixForm=
1 1 4 25
0 1 8 43
3 0 1 1
The first line performed the operation, and the semicolon suppressed the output; the second line
asked Mathematica to display the revised matrix in MatrixForm. Next we can add 3 times the first
row to the third row, and the second row to the first row:
7.2 Performing Gaussian Elimination 347
Out[15]//MatrixForm=
1 0 4 18
0 1 8 43
0 0 11 55
1
Finally, we can multiply the third row by , multiply the second row by 1, add 8 times the
11
third row to the second row, and add 4 times the third row to the first row:
1
In[16]:= mat337 mat337 ;
11
mat327 1mat327 ;
mat327 mat327 8mat 337 ;
mat317 mat 317 4mat337 ;
mat ss MatrixForm
Out[20]//MatrixForm=
1 0 0 2
0 1 0 3
0 0 1 5
Exercises 7.2
1 2
1. Use row operations to write the matrix as the product of an upper triangular matrix and
3 4
an elementary matrix. An elementary matrix is a matrix that differs from the identity matrix by a
single row operation.
348 Linear Algebra
1 0 0 2 2 3
In[1]:= mat3 2 3 4 ; mat4 0 0 1 ;
1 5 1 5 5 5
mat3 mat4 ss MatrixForm
Out[2]//MatrixForm=
3 2 3
2 3 5
4 10 4
And we can multiply matrices. The i, jth entry of the product of the matrix a with the matrix b is the
dot product of the ith row of a with the jth column of b. Multiplication is only possible if the
number of columns of a is equal to the number of rows of b.
In Mathematica, use the dot (i.e., the period) as the multiplication operator for matrices:
Be careful to use the dot to perform matrix multiplication. The symbol * will simply multiply
7.3 Matrix Operations 349
The Transpose command will produce the transpose of a matSJY
Uhe matrix obtained by switching
the rows and columns of that matrix:
To find a power of a matrix use the command MatrixPower. The first argument is the matrix, and
the second argument is the desired power:
The inverse of a square matrix, if it exists, is the matrix whose product with the original matrix is the
identity matrix. A matrix that has an inverse is said to be nonsingular. You can find the inverse of a
nonsingular matrix with the Inverse command:
It is a simple matter to check that the product of a matrix with its inverse is the identity:
¿ Note that the % in the last input represents the inverse of mat3 rather than the MatrixForm
of that inverse. This is the reason for the cell tag Out[73]//MatrixForm= . If you refer to any such
output cell (with % or %%, for instance), Mathematica will use the output generated before
MatrixForm was applied. In other words, the output is a matrix, it is only displayed in
MatrixForm. This makes it easy to incorporate MatrixForm output into new input.
The determinant of a square matrix is a number that is nonzero if and only if the matrix is nonsingu-
lar. Determinants are notoriously painful to compute by hand, but are a snap with Mathematica’s
Det command:
In[11]:= Det#mat3'
Out[11]= 23
Any matrix operation can be performed either on a matrix whose entries are numeric, or on a matrix
whose entries are purely symbolic. For example, you can find the formula for the determinant of a
general 3 3 matrix:
In[13]:= Det#mat5'
Out[13]= a1,3 a2,2 a3,1 a1,2 a2,3 a3,1 a1,3 a2,1 a3,2 a1,1 a2,3 a3,2 a1,2 a2,1 a3,3 a1,1 a2,2 a3,3
Notice how the determinant arises naturally in the inverse of a matrix. Here is the entry in the first
row and first column of the inverse of mat5:
In[14]:= Inverse#mat5'31,17
Here is the same entry with the determinant replaced by the symbol det:
The trace of a matrix is the sum of the entries along the main diagonal. The trace of a matrix may be
calculated with the command Tr:
7.4 Minors and Cofactors 351
In[16]:= Tr#mat5'
Be careful, there is a command whose name is Trace, but it has nothing to do with linear algebra;
don’t use it to compute the trace of a matrix.
Exercises 7.3
1. Using the Dividers option to the Grid command, find a way to format a matrix with vertical bars
on the sides instead of parentheses. It is handy to be able to do this when typesetting, as vertical
bars are traditionally used to denote the determinant of the matrix they enclose. Use your result
to typeset the following equation:
1 2
2.
3 4
1 7 5 0
5 8 6 9
2. Find the inverse of the matrix by appending the identity matrix to this matrix and
2 1 6 4
8 1 2 4
then using Gaussian elimination to find the inverse.
column are deleted from A. M = (Mij 0 is the matrix of minors. But the command Minors will return a
the minor Mij of entry aij is the determinant of the submatrix that remains after the i th row and j th
matrix whose ijth entry is the determinant of the submatrix that remains after the +n i 1/ st row
and ,n j 10 st column are deleted from A. Yes, this is really confusing, but see if you can see the
difference in the examples below.
The custom command below will give us the traditional matrix of minors. Notice that the entries are
the “reverse” of the entries above. The Map command is discussed in Section 8.4.
To get a single minor, say M23 we can simply ask for that entry from the output of the minorsMag
trix command.
In[5]:= minorsMatrix#mat5'32,37
The matrix of cofactors is the matrix whose ijth entry is +1/ij Mij . We can use our minorsMatrix
command to compute a matrix of cofactors.
Notice that the * above will multiply the corresponding entries of the two matrices.
Finally, recall that the adjoint of a matrix is the transpose of its matrix of cofactors. There is a lovely
1
relationship between the inverse of a matrix and its adjoint: A1 adj+A/. Let’s use an example
det+A/
to illustrate this fact.
7.4 Minors and Cofactors 353
Clear#mat';
mat RandomInteger#9, 4, 4';
In[8]:=
mat ss MatrixForm
Out[10]//MatrixForm=
8 1 2 9
6 0 9 8
7 9 5 7
8 0 2 6
Transpose#cofactorsMatrix#mat'' ss MatrixForm
1
In[11]:=
Det#mat'
Out[11]//MatrixForm=
171 35 19 281
803 803 803 803
7 39 90 85
803 803 803 1606
126 101 14 38
803 803 803 803
270 13 30 507
803 803 803 1606
We did indeed!
Exercises 7.4
8 0 3 7
9 4 2 9
1. Find the adjoint of A using the determinant and the inverse of A, then check your
2 8 0 7
8 9 7 0
answer using the cofactorsMatrix command.
2. Write a command to find the determinant of a matrix by cofactor expansion along the first row.
354 Linear Algebra
The output from the SparseArray command gives us the number of nonzero entries and then the
dimensions of the matrix we’ve created. We can use MatrixForm to get a look at this matrix.
In[2]:= s1 ss MatrixForm
Out[2]//MatrixForm=
a 0 0 0 0 0 0
0 0 b 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 c 0 0 0 0 0
0 0 0 0 0 0 d
In[3]:= s2 SparseArray#1, 1, 2, 3, 5, 2, 6, 7 a, b, c, d'
In[4]:= s2 ss MatrixForm
Out[4]//MatrixForm=
a 0 0 0 0 0 0
0 0 b 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 c 0 0 0 0 0
0 0 0 0 0 0 d
SparseArray will create a matrix that fits all the nonzero entries we specify. We can also specify a
matrix of a different size.
In[5]:= s3 SparseArray#1, 1, 2, 3, 5, 2, 6, 7 a, b, c, d, 8, 10'
In[6]:= s3 ss MatrixForm
Out[6]//MatrixForm=
a 0 0 0 0 0 0 0 0 0
0 0 b 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 c 0 0 0 0 0 0 0 0
0 0 0 0 0 0 d 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
We can create a SparseArray in which the unspecified entries have a value other than zero.
In[8]:= s4 ss MatrixForm
Out[8]//MatrixForm=
a 2 2 2 2
2 2 b 2 2
2 2 2 2 2
2 2 2 2 2
2 c 2 2 2
The Normal command will convert the output of SparseArray to the list form of a matrix.
In[9]:= Normal#s4'
Out[9]= a, 2, 2, 2, 2, 2, 2, b, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, c, 2, 2, 2
below creates a 16 by 12 matrix with 1s in positions +2, 3/, +4, 6/, +8, 9/, and +16, 12/.
We can use Table to help us list the nonzero entries of a large matrix. For example the command
This large matrix makes a mess if we ask for a numerical output, but a picture can tell us a lot about
our matrix.
356 Linear Algebra
In[11]:= MatrixPlot#s5'
1 5 12
1 1
5 5
Out[11]=
10 10
16 16
1 5 12
These plots will automatically color entries with larger values in a darker color.
In[13]:= MatrixPlot#s6'
1 5 12
1 1
5 5
Out[13]=
10 10
16 16
1 5 12
ArrayRules will return the positions and values we gave for a sparse array.
In[15]:= ArrayRules#s7'
Band can be used with SparseArray to give a matrix in which a single value is present in each
position on a diagonal beginning at the given starting position.
7.5 Working with Large Matrices 357
Clear#b';
SparseArray#Band#3, 2' 3, Band#1, 4' b, 6, 6'
In[16]:=
In[18]:= ss MatrixForm
Out[18]//MatrixForm=
0 0 0 b 0 0
0 0 0 0 b 0
0 3 0 0 0 b
0 0 3 0 0 0
0 0 0 3 0 0
0 0 0 0 3 0
In[20]:= ss MatrixForm
Out[20]//MatrixForm=
0 0 0 0 0 0
0 0 0 0 0 0
0 2 0 0 0 0
0 0 4 0 0 0
0 0 0 6 0 0
0 0 0 0 8 0
Exercises 7.5
1. Use SparseArray to create the following picture:
1 2 3 4 5 6 7 8 9 10
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
1 2 3 4 5 6 7 8 9 10
358 Linear Algebra
1 2 0 0 0 0 0 0 0 0 0 0
3 4 0 0 0 0 0 0 0 0 0 0
0 0 1 2 0 0 0 0 0 0 0 0
0 0 3 4 0 0 0 0 0 0 0 0
0 0 0 0 1 2 0 0 0 0 0 0
0 0 0 0 3 4 0 0 0 0 0 0
0 0 0 0 0 0 1 2 0 0 0 0
0 0 0 0 0 0 3 4 0 0 0 0
0 0 0 0 0 0 0 0 1 2 0 0
0 0 0 0 0 0 0 0 3 4 0 0
0 0 0 0 0 0 0 0 0 0 1 2
0 0 0 0 0 0 0 0 0 0 3 4
m.x m b
x1 5 x2 4 x3 x4, 3 x1 4 x2 x3 2 x4,
3 x1 2 x2 x3 5 x4, 6 x2 7 x3 x4 m 1, 2, 3, 4
Out[3]=
We can interpret this as a list of four linear equations, each in four variables.
In[4]:= Det#m'
Out[4]= 35
7.6 Solving Systems of Linear Equations 359
We now use the command ArrayFlatten to form the augmented matrix, and the command RowReg
duce to find the reduced row echelon form of the matrix.
The command LinearSolve provides a quick means for solving systems that have a single solution:
!, !, !, !!
127 141 139 13
Out[7]=
35 35 35 35
We can also use the LinearSolve command to form a function for matrix m that can be applied to
any vector b.
In[9]:= f#b'
!, !, !, !!
127 141 139 13
Out[9]=
35 35 35 35
Or we can solve the system m x b for x by multiplying both sides on the left by m1 , to get x
m1 b.
360 Linear Algebra
In[10]:= Inverse#m'.b
!, !, !, !!
127 141 139 13
Out[10]=
35 35 35 35
Finally, we can use the command Solve to solve this system, just as in Section 4.9 on page 191. But
we have to be careful using Solve. When we use the Table/Matrix # New… dialogue box to create m,
x, and b, both m x and b are lists of lists. The Solve command takes a list of equations as its first
argument and a list of variables as its second argument—it unfortunately cannot accept lists of lists.
There is a simple solution: We will have to re-enter x and b without using the Table/Matrix # New…
dialogue box, expressing each as a single list. If we do this, the equation m . x m b is acceptable as
input to the Solve command. Note how Mathematica interprets the equation m . x m b as a single list
of equations when x and b are entered this way:
In[11]:= Clear#x, b'; x x1, x2, x3, x4; b 1, 2, 3, 4; m.x m b
x1 5 x2 4 x3 x4, 3 x1 4 x2 x3 2 x4,
3 x1 2 x2 x3 5 x4, 6 x2 7 x3 x4 m 1, 2, 3, 4
Out[11]=
x1 !!
127 141 139 13
Out[12]= , x2 , x3 , x4
35 35 35 35
An inconsistent system of equations has no solutions. If we use the Solve command on such a
system, the output will be an empty set of curly brackets:
Out[15]=
If you use the LinearSolve command with an inconsistent system you will be told off:
Out[18]= LinearSolve#1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1'
And if you try to find the inverse of m you will be told off again:
In[19]:= Inverse#m'.b
Inverse::sing : Matrix 1, 1, 1, 1, 1, 1, 1, 1, 1 is singular . j
The remaining possibility for a system of equations is that there are an infinite number of solutions.
The Solve command nicely displays the solution set in this situation. The warning message can be
safely ignored in this case:
x1 !!
11 7 x3 6 2 x3
Out[22]= , x2
5 5 5 5
Be very careful when using the LinearSolve command. In a system having an infinite number of
solutions it will return only one of them, giving no warning that there are others. In this example it
returns only the solution where x3 0:
11 6
Out[23]= , , 0!
5 5
Row reduction gives the solution with little possibility for confusion:
Out[24]= 2, 3, 4, 8, 4, 6, 8, 16, 1, 1, 1, 1
362 Linear Algebra
The moral is that you should be very careful using the command LinearSolve unless you know you
have a nonsingular matrix and hence a single solution. To check this, you can use the Det com-
mand, keeping in mind that a singular matrix has determinant zero. When in doubt it is best to use
row reduction and your knowledge of linear algebra to find the solution vectors.
Out[27]= 0
x3 , and x4
vector of the form +t, t, t, 0/, where t is a real number, is a solution, and the vector +1, 1, 1, 0/
This reduced form of the augmented matrix tells us that x1 x3 , x2 0. That is, any
forms a basis for the solution space. Bases are discussed in the next section of this chapter.
The command NullSpace gives a set of basis vectors for the solution space of the homogeneous
7.6 Solving Systems of Linear Equations 363
equation m x 0:
In[29]:= NullSpace#m'
LinearSolve#m, b'
9, 7, 0, 3
In[32]:=
Out[32]=
In[33]:= NullSpace#m'
tion +9, 7, 0, 3/ t+1, 1, 1, 0/. In other words, x1 9 t , x2 7 t , x3 t , and x4 3. This
This tells us that there are an infinite number of solutions. For each real number t , there is a solu-
Exercises 7.6
1. For which values of a will the following system of linear equations have no solutions, one
solution, or an infinite number of solutions?
x2y3z 4
2x y 5z 2
2
4x 3y a z a3
364 Linear Algebra
2. Find the equation of the circle that contains the points +4, 3/, +4, 5/, and +2, 7/.
We can determine whether a given vector b is in the span of the vectors v1 , v2 , v3 , …, vn by letting
m be the matrix whose columns are v1 , v2 , v3 , …, vn , and then determining whether the equation
mx b has a solution. A solution x, if it exists, provides values for the scalars ai .
v2 4, 4, 7;
v3 8, 1, 0;
b 1, 2, 3;
m Transpose#v1, v2, v3';
c LinearSolve#m, b'
, !
3 9 10
Out[7]= ,
2 14 7
3 9 10
We can check that v v v b.
2 1 14 2 7 3
Out[8]= 1, 2, 3
A set of vectors v1 , v2 , v3 , …, vn is said to be linearly independent if every vector in their span can be
expressed in a unique way as a linear combination a1 v1 a2 v2 a3 v3 an vn . Put another way,
this means that the only way to express the zero vector as such a linear combination is to have each
coefficient ai 0. If it is possible to write a1 v1 a2 v2 a3 v3 an vn 0 with at least one of the
ai 0, then the set of vectors v1 , v2 , v3 , …, vn is linearly dependent.
To check whether a set of vectors v1 , v2 , v3 , …, vn is linearly independent, let m be the matrix
whose columns are v1 , v2 , v3 , …, vn , and check that the equation m x 0 has only the trivial
solution:
7.7 Vector Spaces 365
In[9]:= NullSpace#m'
Out[9]=
Yes, these are linearly independent vectors. Alternatively, we could check that the matrix whose
rows (or columns) are v1 , v2 , v3 , …, vn , is nonsingular:
Out[10]= 14
Bases
the vector space. Given a spanning set of vectors v1 , v2 , v3 , …, vn for a vector space we can easily
A basis for a vector space is a set of linearly independent vectors whose span includes every vector in
obtain a basis for that space. Form a matrix whose rows are the vectors v1 , v2 , v3 , …, vn , and row-
reduce:
The nonzero rows of this matrix form a basis for the space spanned by the set v1 , v2 , v3 , v4 . This
space is also called the row space of the matrix m.
We can also find a basis consisting of a subset of the original vectors. If we row-reduce the matrix
whose columns are the vectors v1 , v2 , v3 , …, vn , then the columns containing the leading 1s will
form a basis for the column space, and the corresponding columns from the original matrix will also
form a basis for the column space. (An entry in a row-reduced matrix is called a leading 1 if the
entry is a 1 and it has only zeros to its left.)
366 Linear Algebra
The vectors +1 , 0 , 0 , 0 , 0/, +0, 1, 0, 0, 0/, and +0, 0, 1, 0, 0/ form a basis for the column space of
v1 , v2 , and v4 will form a basis for the space spanned by the set v1 , v2 , v3 , v4 . We can confirm that
m. The vectors from the same columns in m will also form a basis for the column space. Hence
Out[25]=
We see here an example of a general truth: a vector space may have many distinct bases. The num-
ber of vectors in any basis for that vector space, however, will always be the same. This number is
called the dimension of the vector space.
In[26]:= Length$NullSpace#m'(
Out[26]= 1
The rank of a matrix is the common dimension of the row space and the column space. The rank
plus the nullity must equal the number of columns in a matrix.
In[27]:= MatrixRank#m'
Out[27]= 3
7.7 Vector Spaces 367
It is easy to find a unit vector, a vector whose length or norm is 1, in the same direction as a given
vector. We simply need to divide each component by the norm of the vector. The command Normal
ize does this automatically.
In[32]:= Norm#v1'
Out[32]= 30
In[33]:= u1 Normalize#v1'
2 3 2 1
Out[33]= , , 2 , , 0!
15 10 15
30
In[34]:= Norm#u1'
Out[34]= 1
A collection of vectors is orthogonal if the vectors are mutually perpendicular, i.e., if the dot product
of every pair is 0. The set is orthonormal if in addition each vector has norm 1. Given a basis for a
vector space, we can use the Orthogonalize command to find an orthonormal basis. Orthogonalize
uses the Gram–Schmidt process unless another method is specified using the Method option. The
argument for the command Orthogonalize is a list of linearly independent vectors. The output is a
list of mutually orthogonal unit vectors with the same span:
Before we apply Orthogonalize let’s check that our vectors are linearly independent:
Out[35]=
Out[37]//MatrixForm=
2 3 2 1
2 0
15 10 15 30
6 1 2 83 30
4 4
1405 8430 4215 8430 281
It is easy to check that any pair of these vectors is orthogonal. Just enter a list whose items are dot
products of every possible pair of distinct vectors. The output will be a list of zeros if the vectors in
each pair are orthogonal:
Out[38]= 0, 0, 0
Out[39]=
The familiar concepts of vector length and the angle between pairs of vectors in Euclidean vector
spaces can be generalized to other vector spaces that admit an inner product—a generalization of the
dot product. As with the dot product, two vectors whose inner product is zero are said to be orthogo-
nal. And a vector whose inner product with itself is 1 is said to be a unit vector.
For example, consider the vector space P3 of polynomials in the variable x of degree at most three
with real coefficients. Given 2 polynomials p and q in P3 we can form their inner product using the
function f defined below:
In[40]:= Clear$f, p, q(
Out[44]= 1
Out[45]= 0
The Orthogonalize command can be applied in any inner-product space. The default inner product
for Orthogonalize is the dot product but a different inner product function can be specified as an
optional second argument.
In[46]:= v1, v2, v3, v4 Orthogonalize#w1, w2, w3, w4, f'
x3 !
1 3 3 5 1 5 7 3x
Out[46]= , x, x2 ,
2 2 2 3 2 2 5
2
We can apply the inner product to these vectors to check that they are pairwise orthogonal unit
vectors.
Out[47]= 1
Out[48]= 0
QR-Decomposition
The QR-Decomposition of a matrix is a factorization of a matrix with linearly independent column
vectors, into a product of a matrix Q that has orthonormal column vectors and a matrix R that is
invertible and upper triangular. The matrix Q is obtained by applying the Gram–Schmidt process to
the column vectors of the matrix. The matrix R is then uniquely determined.
1 0 0
In[49]:= m 1 2 0 ;
0 1 3
In[50]:= Det#m'
Out[50]= 6
370 Linear Algebra
In[51]:= q Transpose$Orthogonalize$Transpose#m'((;
MatrixForm$q(
Out[52]//MatrixForm=
1 1 1
2 3 6
1 1 1
2 3 6
1 2
0
3 3
In[53]:= r Inverse$q(.m;
MatrixForm#r'
Out[54]//MatrixForm=
2 2 0
0 3 3
0 0 6
In[56]:= qr QRDecomposition#m'
The command QRDecomposition returns 2 matrices, q and r , where qt r is equal to the original
matrix. Notice that the first matrix in this list is the transpose of our matrix q above.
In[58]:= Transpose$qr317(.qr327
Exercises 7.7
+1, 2, 3/,
+4, 5, 6/, v3 +7, 7, 8/ and use the result to show that the product of an orthonormal
1.a Find an orthonormal basis for the vector space spanned by the vectors v1
v2
matrix with its transpose is the identity matrix.
b. Explain why this makes sense.
Clear#m';
m Array#Min, 2, 2'; m ss MatrixForm
In[1]:=
Out[2]//MatrixForm=
1 1
1 2
To get the eigenvalues, type the following command (look for O in the BasicMathInput palette):
.3 5 2, .3 5 2!
1 1
Out[3]=
2 2
We find two eigenvalues and two eigenvectors. Let’s check that m v1 O1 v1 :
.1 5 2, .3 5 2!
1 1
Out[5]=
2 2
In[6]:= O1 v1 ss Simplify
.1 5 2, .3 5 2!
1 1
Out[6]=
2 2
In[7]:= m.v1 m O1 v1
Out[7]= True
The command Eigensystem gives both the eigenvalues and the eigenvectors. The output is a list
whose first item is a list of eigenvalues and whose second item is a list of corresponding eigenvectors:
In[8]:= Eigensystem#m'
We can ask that the output of any of these commands be numerical approximations by replacing m
with N[m]:
In[9]:= Eigensystem#N#m''
Even for a simple matrix with integer entries the eigenvalues can be quite complicated and involve
complex numbers:
Clear#m';
Array#Min, 3, 3'; m ss MatrixForm
In[10]:=
m
Out[11]//MatrixForm=
1 1 1
1 2 2
1 2 3
7.8 Eigenvalues and Eigenvectors 373
In[12]:= Eigenvalues#m'
The eigenvalues here are returned as Root objects, in this case the three roots of the characteristic
polynomial 1 5x 6x2 x3 . The option setting Cubics True will permit the display of such
roots in terms of radicals.
. .9 Ç 3 22
1s3
2
7
72s3 2
,
. .9 Ç 3 22
Out[13]=
1s3
3 32s3
2
- 1 .1 Ç 32 .1 Ç 3 2 . .9 Ç 3 22
1s3
7 2s3 7
2 2
2 ,
.3 .9 Ç 3 22
1s3
2 32s3
- 1 .1 Ç 32 .1 Ç 3 2 . .9 Ç 3 22
1s3
7 2s3
!
7
2 2
2
.3 .9 Ç 3 22
1s3
2 32s3
There is a similar option setting for quartics. One may also get a numerical approximation of the
eigenvalues as follows:
In[14]:= Eigenvalues#m' ss N
For an n l n matrix Mathematica will always return n eigenvalues even if they are not all distinct. The
eigenvalues will occur in the same frequency as the roots of the characteristic polynomial (as
explained in the next subsection). Mathematica will also output n eigenvectors. If there are fewer
than n linearly independent eigenvectors, the output may contain one or more zero vectors. These
zero vectors are there for bookkeeping only; actual eigenvectors are nonzero by definition:
374 Linear Algebra
In[15]:= Clear#m';
2 1 0
m 0 2 0 ;
0 0 0
Eigensystem#m'
In[18]:= Clear#m';
2 1 0
m 1 2 0 ;
0 0 3
c Det$O IdentityMatrix#3' m(
Out[20]= 9 15 O 7 O2 O3
Then we find the roots of the characteristic polynomial:
Solve#c m 0, O'
O 1, O 3, O 3
In[21]:=
Out[21]=
There are two eigenvalues O 1 and O 3. The eigenvalue 3 is reported twice because it occurs twice
as a root of the characteristic polynomial c. We can see this clearly by factoring c:
In[22]:= Factor#c'
Of course, most characteristic polynomials will not factor so nicely. To find the eigenspace of each
eigenvalue Oi we will find the null space of the matrix Oi I m:
The eigenspace for the eigenvalue O 1 has one basis vector: +1, 1, 0/. The eigenspace for the
7.8 Eigenvalues and Eigenvectors 375
In[25]:= Eigensystem#m'
Diagonalization
A square matrix m is diagonalizable if there exists a diagonal matrix d and an invertible matrix p
such that m p d p1 . In this case, the expression on the right hand side is called a Jordan
decomposition or diagonalization of m. An n l n matrix is diagonalizable if and only if it has n linearly
independent eigenvectors. In this case the matrix p will be the matrix whose columns are the
eigenvectors of m and the matrix d will have the eigenvalues of m along the diagonal (and zeros
everywhere else):
We can use Eigensystem to find the eigenvalues and eigenvectors and then form the matrices p and
d ourselves or use JordanDecomposition and have Mathematica compute the matrices p and d.
Notice that the matrices we get by each method are slightly different. The order in which the
eigenvalues and eigenvectors are listed causes this difference.
d DiagonalMatrix#evals';
d ss MatrixForm
In[29]:=
Out[30]//MatrixForm=
3 0 0
0 3 0
0 0 1
p Transpose#evecs';
p ss MatrixForm
In[31]:=
Out[32]//MatrixForm=
0 1 1
0 1 1
1 0 0
376 Linear Algebra
Clear$p, d(;
p, d
In[34]:=
JordanDecomposition#m'
Out[35]= 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 3, 0, 0, 0, 3
Exercises 7.8
2 1 0
1. Form the LU-decomposition of the matrix m 1 2 0 .
0 0 3
We can better understand a linear transformation by studying the effect it has on geometric figures
in its domain. Mathematica can be used to visualize the effect of a linear transformation from 52 to
52 on a geometric object in the plane. We first produce a polygonal shape by specifying the coordi-
nates of its vertices. We can then apply a linear transformation to each of these points and see
where they land. Examining the geometric changes tells us how the linear transformation behaves.
7.9 Visualizing Linear Transformations 377
To produce a figure on which to demonstrate transformations, go to the Graphics menu and select
New Graphic. Next, bring up the drawing tools via Graphics # Drawing Tools, and select the line
segments tool (push the appropriate button, or type the letter s). Now click on the graphic repeat-
edly to draw a picture, being careful not to click on any previous points (to close the loop) until you
are done. Don’t get too fancy; just a single closed loop is all that is needed. For instance, here is a
stunning portrait of our dog, Zoe:
Now click on the graphic so that the orange border is showing, copy it to the clipboard (Edit # Copy)
and paste it into the following command:
The details of how the Cases command works are discussed in Section 8.8. But what it produces is
simply the list of Zoe’s coordinates. Her picture is easily recovered from this coordinate list, either as
a Line or Polygon object:
378 Linear Algebra
Out[2]= , !
¿ If when constructing a graphic you invoke the line segments tool more than once, you will
produce a graphic with more than one Line object in it. Things are a bit more complicated
here. In such cases do not apply First to the Cases input above. Cases will produce a list
containing multiple lists of points. Give this master list a name (such as dog). To display this
list, instead of Graphics[Line[dog]], use Graphics[Map[Line,dog]] (or equivalently:
Graphics[Table[Line[s],{s,dog}]]). To multiply the matrix m by each vertex in the dog lists,
we use Map[m.#&,dog,{2}]. This “maps” the Function[v,m.v] over the dog list at the second
level. Map and Function are described in detail in Section 8.4 on page 403. Finally, to display
the transformed image, put this all together to get:
You will probably not want to use Polygon to render an image made from multiple Line
objects.
1 0
We can reflect Zoe about the y-axis using the matrix . We simply multiply each of Zoe’s
0 1
coordinates by this matrix, then make a Polygon from the transformed coordinates. Both the
original and transformed figures are shown below:
Axes True)
2.0
1.5
Out[3]= 1.0
0.5
2 1 1 2
0 1
Here Zoe is reflected about the line y x, using the matrix :
1 0
7.9 Visualizing Linear Transformations 379
Out[4]=
3 2 1 1 2 3
1
2
3
The most simple type of linear transformation is a dilation or contraction. The standard matrix for
c 0
this type of transformation is . If 0 c 1 we have a contraction, while if c ! 1, we have a
0 c
dilation. Using Manipulate we allow c to vary dynamically. Below we display the dilation matrix
together with the graphical output. Toward this end, we introduce a displayMatrix command that
will round all matrix entries to two decimal places, leave room in front of each entry for a minus
sign, and generally make the matrix entries easy to read as the parameter c is manipulated.
displayMatrix#mB' : MatrixForm
Map$NumberForm$Chop$N#Ó', 103 (, 3, 2, NumberSigns "", " "( &, m, 2(
In[5]:=
¿ The displayMatrix command utilizes both Map and Function, which are discussed in Section
8.4 on page 403, in order to operate individually on each matrix entry. It also makes use of
NumberForm, which is used to regulate the display of numbers, and Chop, which is used to
round sufficiently small numbers to zero. NumberForm is discussed in Section 8.3.
380 Linear Algebra
In[6]:= Manipulate%
10
2.75 0.00
5 0.00 2.75
Out[6]=
10 5 5 10
5
10
We can rotate a figure in two dimensions through an angle T using the standard rotation matrix.
Manipulate$
Labeled$Graphics$Brown, Polygon$Table$RotationMatrix#T'.v, v, dog (( ,
In[8]:=
Bad Dog
4
0.70 0.72
0.72 0.70
Out[8]= 2
4 2 2 4
2
4
7.9 Visualizing Linear Transformations 381
We can compose linear transformations by multiplying the individual matrices for the transforma-
tions. Below we’ve combined a reflection about the x-axis with a dilation and a rotation.
1 0 c 0
In[9]:= Manipulate%With%m . .RotationMatrix#T'!,
0 1 0 c
10 5 5 10
5
10
In 3-dimensions, we can easily access any of the dozens of polyhedra available in the Polyhedrong
Data collection, or any of the Geometry3D objects in the ExampleData collection. Here, for
instance, is the space shuttle:
Out[10]=
We can easily extract its vertex coordinates as follows (to save space we display only the first few):
382 Linear Algebra
Collections of these vertices are assembled to make the polygonal faces. The first face, for instance, is
a triangle comprised of the first, second, and third vertices in the above list.
Out[16]=
So, proceeding as in the two-dimensional case, we dynamically display the figure resulting from the
application of a linear transformation to each of the vertices of the figure above. For instance, below
we show the effect of composing rotations about each of the three coordinate axes, respectively:
7.9 Visualizing Linear Transformations 383
Manipulate$
RotationMatrix#T, 1, 0, 0'.
In[17]:=
With$m
RotationMatrix#I, 0, 1, 0'.RotationMatrix#M, 0, 0, 1',
vertices N ExampleData$"Geometry3D", "SpaceShuttle" , "VertexData"(,
faces ExampleData$"Geometry3D", "SpaceShuttle" , "PolygonData"( ,
Labeled$
Graphics3D$
EdgeForm#', GraphicsComplex$Table#m.v, v, vertices', Polygon#faces'( ,
PlotRange 8(, displayMatrix#m', Right, Top ((,
T, .5, 0, 2S, I, 0, 2S, M, 0, 2S(
Exercises 7.9
1. Draw a simple line drawing and construct a graph that demonstrates its reflection about the x-
axis.
2. One can apply matrix transformations to each vertex in any of the objects in the Polyhedrong
Data collection.
a. Enter the input below to render a square gyrobicupola:
PolyhedronData$"SquareGyrobicupola"(
b. Extract the vertices and face indices for this polyhedron. Hint: A similar extraction was carried
out in the text for the space shuttle. The syntax is slightly different here, however. The relevant
properties are now called "VertexCoordinates" and "FaceIndices". Type PolyhedronData["g
SquareGyrobicupola","Properties"] for a listing of all available properties.
c. How many vertices and faces are there in this example?
d. Construct a Manipulate that will enable you to rotate the polyhedron about any of the three
coordinate axes.
8
Programming
8.1 Introduction
When you put several commands together to accomplish some purpose beyond the capacity of any
one individually, you are programming. Mathematica is intentionally designed for this purpose. Like
anything else, getting good at programming takes practice. But it is also exceedingly handy to have
familiarity with commands that lend themselves to such greater enterprises. We’ve seen plenty of
Mathematica in the first seven chapters; in this chapter we’ll discuss commands that are especially
useful for programming. Keep in mind that we only have room here for a brief introduction to these
concepts. Entire books, much longer than this one, have been written on this subject. Think of this
chapter as a gentle introduction.
We begin in Section 8.2 with some important background material, a consideration of the internal
form of any and every Mathematica expression. Every expression, input, output (or a cell, or an
entire notebook) is highly structured. Before it is possible to operate on any such expression, you
simply have to know what you are dealing with. You have to understand its structure.
Some of the most fundamental structures in Mathematica are the various types of numbers. These are
addressed in Section 8.3. The internal forms of the various types of numbers are discussed, along
with notions such as precision and accuracy. Mathematica has the capacity to carry out calculations
to arbitrarily high precision. In this section we also discuss a myriad of possibilities for the display of
numbers.
Section 8.4 introduces the workhorses of functional programming, commands like Map and Function
and MapThread. Section 8.5 introduces the staples of procedural programming, with predicate
commands (that return True or False), and control structures and looping commands such as If,
Do, While, and For. These commands instruct Mathematica to carry out a sequence of instructions,
and similar commands are often among the first encountered when one learns an elementary
programming language.
Section 8.6 discusses commands that limit the scope of auxiliary functions and symbols that are
sometimes needed in programming. These scoping commands are essential to insulate local defini-
tions from any global assignments that a user might make. Section 8.7 introduces the essential
commands that facilitate iteration, such as NestList, NestWhileList, FoldList, and FixedPointList.
386 Programming
Finally, section 8.8 discusses patterns and pattern matching in the context of defining commands,
making replacements, and for use in specific commands like Cases.
Clear#a, x';
Grid$Table$exp, AtomQ $exp( , exp,
In[1]:=
2, 2.0, 2 s 3, 2 3Ç, S, a, Plot3D, Sin, "a string", 2 a, a#x' (, Dividers Gray(
2 True
2. True
2
True
3
23Ç True
S True
Out[2]= a True
Plot3D True
Sin True
a string True
2a False
a#x' False
A nested expression has the form head[arg1, arg2, …]. The head is typically atomic (it is usually a
command name, although it may itself be a nested expression), and the arguments are either nested
expressions or atoms. The arguments are enclosed in square brackets (typically there are one or more
arguments, but zero arguments are permitted). The command Head will display the head of any
nested expression. Here are a few examples (for each expression in the left column, its head appears
in the right column):
Clear$a, b, c, g, x, y, myCommand(;
Grid$Table$exp, Head$exp( , exp, myCommand#x', g$x, y(, a$b$x, y(, c(, a#b'#c' (,
In[3]:=
Dividers Gray(
8.2 FullForm: What the Kernel Sees 387
myCommand#x' myCommand
g$x, y( g
Out[3]=
a$b$x, y(, c( a
a#b'#c' a#b'
Even the expression a[b][c] qualifies as a legitimate nested expression. The head is the nested
expression a[b].
Every non-atomic Mathematica expression has this form, a head followed by square brackets enclos-
ing zero or more arguments.
That last statement should give you pause. Consider the expression 2 + a. It is not atomic (we saw
this in the second to last output above), so what is the head? Where are the square brackets? What
are the arguments? It does not appear to have the form head[arg1, arg2, …]. What gives?
Out[4]= Plus
What’s going on is that the internal or FullForm of this expression is not revealed when we type 2 +
a. These paltry three characters are parsed into the following before being sent to the kernel for
evaluation:
In[5]:= 2 a ss FullForm
Out[5]//FullForm=
Plus#2, a'
Ah, so this expression does have the form of a nested expression after all, and the head is indeed
Plus. Mathematica allows you to type 2 + a because you’re a human, and that’s what you’re used to
(this is called the infix form of the Plus command). In this and in dozens of other cases you are
permitted to create expressions that do not look like proper nested expressions. This flexibility is
granted simply to make your interactions with Mathematica more natural, and to make the typing as
simple as possible. But in each of these cases your input is parsed into a properly structured nested
expression before being sent to the kernel. It is crucial to understand this fact if you are to program
effectively in Mathematica. FullForm is a great tool for peeking under the hood to view the internal
form of any Mathematica expression. Here are some other examples (for each expression in the left
column we show its FullForm in the right column):
388 Programming
a «« b
a && b And#a, b'
Or#a, b'
a Not#a'
a2
a s. b
Rule#a, 2'
a ss. b
ReplaceAll#a, b'
ReplaceRepeated#a, b'
amb Equal#a, b'
ab Unequal#a, b'
ab Less#a, b'
ab LessEqual#a, b'
a ± Reals Element#a, Reals'
a aa b
a
#x'
StringExpression#a, b'
Derivative#1'#a'#x'
¼ a#x'Å x Integrate#a#x', x'
¿ Note that Quiet has been applied (in postfix form) in the previous input. This suppresses all
warning messages. In this case, for instance, the a /. b input generates a warning message that
b is neither a replacement rule nor a list of replacement rules.
8.2 FullForm: What the Kernel Sees 389
Note that FullForm can be subtle to use in some cases, as an expression may evaluate before Fullg
Form is applied. If one enters FullForm[a = b], for instance, the expression a = b evaluates and
returns b, and then the FullForm of b (which is simply b) is displayed. In fact, the FullForm of a = b
is Set[a, b]. In such cases, wrapping the expression in Defer will prevent this sort of premature
evaluation.
Understanding the structure of the Mathematica language allows you to do many things. One clearly
sees at this point, for instance, the internal distinctions between the symbols =, :=, and m. They
correspond respectively to the commands Set, SetDelayed, and Equal. You will soon be able to
harness your knowledge of the structure of expressions to operate in interesting ways on complex
expressions. This is the essence of programming in Mathematica.
One last symbol deserves our attention in this context: the semicolon. We have used this symbol to
suppress output on many occasions. It also allows us to evaluate several commands in a single input
cell, like this:
In[10]:= a 3; 2a
Out[10]= 6
Defer#FullForm#a 3;''
CompoundExpression #Set#a, 3', Null'
In[12]:=
Out[12]=
The command is CompoundExpression, and each argument is itself an expression. The arguments
are evaluated in turn, but only the output associated with the expression in the final argument is
displayed. CompoundExpression is an invaluable tool in writing Mathematica programs, for it
allows several inputs to be evaluated in turn, one after the other, with only the output of the last
input displayed.
390 Programming
Here’s one more example that may be revealing. Copy any graphic you like into an input cell, and
ask for its FullForm or InputForm to see its underlying structure. In this case we used the Drawing
Tools palette to create a simple image with the Line Segments tool.
In[13]:= ss FullForm
Out[13]//FullForm=
Graphics$List#Line#List#List#0.21111111111111117`, 0.8333333333333334`',
List#0.2972222222222223`, 0.5972222222222223`',
List#0.18611111111111114`, 0.41666666666666674`',
List#0.31666666666666665`, 0.1972222222222222`''',
Line#List#List#0.5611111111111111`, 0.8361111111111111`',
List#0.4722222222222222`, 0.6027777777777779`',
List#0.5722222222222222`, 0.40000000000000013`',
List#0.4833333333333334`, 0.19999999999999996`''',
Line#List#List#0.7527777777777778`, 0.8305555555555556`',
List#0.8611111111111112`, 0.6222222222222222`',
List#0.7361111111111112`, 0.3944444444444444`',
List#0.8166666666666667`, 0.18055555555555558`'''',
Rule$PlotRange, List#List#0, 1', List#0, 1''((
¿ In fact, Mathematica notebooks are themselves valid nested expressions. If you were to open a
Mathematica notebook in a text editor, you would see a plain text file with the structure
Notebook[arg1, arg2, …]. The individual arguments in a notebook are cells, and every cell is
nested expression in its own right of the form Cell[arg1, arg2, …], and so on. This state of
affairs is most definitely intentional, and is even a little bit devious. It is devious in that the
FrontEnd does not reveal the highly structured nature of the underlying document, just as it
does not reveal the FullForm of expressions such as a , unless you ask for it. But under-
neath, the structure is there. The benefit (and a distinguishing feature of Mathematica) is that
you have access to this underlying form. Because cells and even entire notebooks have the
structure of a valid expression, it is possible to program Mathematica to operate on an entire
notebook. This was done, for example, in the writing of this book. Each chapter in this book is
a Mathematica notebook. The entire notebook expressions for all of the chapter files were sent
8.2 FullForm: What the Kernel Sees 391
to the kernel to programmatically generate the table of contents. They were sent again to
make the index, and so on. While the details of these specific techniques lie beyond the scope
of this book, it is important to understand the potential for operating programmatically on an
entire notebook.
Exercises 8.2
1. Find the Head of each of the following expressions:
a. x 2
2
c. 1, 2, 3
b.
d. Ó 1 &
c. 2
d. 1, 2, 3
3. What is the head of the expression a'[x]? Find its FullForm, and base your answer on what you
see. Check your answer with Head.
4. What is the FullForm of the expression 2x + 1 /. x 3? Use Defer and FullForm to find out.
5. What is the FullForm of the expression x = 3; 2x? Use Defer and FullForm to find out.
6. The command TreeForm produces a visual representation of the FullForm of any expression.
Find the TreeForm of the expression 2x + 1 /. x 3.
7. The Part command was introduced in Chapter 3 to extract a part of a list. It is typically invoked
via typing double square brackets. For instance the input {a, b, c}[[2]] will produce the output b.
But Part can also be used to extract parts of compound Mathematica expressions.
a. Apply TreeForm to the input a
b c
d2 .
b. Extract the zeroth, first, and second parts of this same input.
c. Find parts 32,17 and 32,27.
d. Find parts 32,2,17 and 32,2,27.
8. Go to the Graphics menu, create a new graphic, bring up the Drawing Tools palette, and draw an
arrow. Copy the arrow graphic into a new input cell, and apply TreeForm to the result.
two strings string1 and string2 into a single string. The infix form of StringExpression is ~~. That
is, one may invoke StringExpression by typing string1~~string2.
a. Type and enter a string. What do you see? What do you see if you apply FullForm to the
input?
b. Apply FullForm to the input "I am"~~" putting strings together."
c. Apply Defer to the input FullForm["a"~~"b"].
d. The command ToString will take any expression and convert it to a string. It is often used in
conjunction with StringExpression to create a string that depends on some external input.
Use ToString and StringExpression to create a command that will take a numerical input n
and return the string "n is my favorite number."
10. A notebook is comprised of cells. Each cell expression in a notebook has the head Cell. To see
the underlying form of a Cell expression requires a special technique, as the FrontEnd will
display Cell expressions nicely, that is, in a manner which hides the underlying structure of the
expression.
a. Click anywhere in any cell in any notebook, and in the menus go to Cell # ShowExpression.
The underlying cell structure will be revealed. In this state, you can edit it directly if you like.
Then toggle it back to normal with the same technique. Try this on several different cells in
one of your notebooks.
b. A Cell is a versatile structure, capable of many forms, from input to output to text to graphics.
So it may not be surprising to learn that Cell accepts a myriad of options. How many options
would you guess Cell can accept? Test your answer using the Options command.
8.3 Numbers
Types of Numbers: Integer, Rational, Real, and Complex
When working with a calculator or spreadsheet, one is typically not concerned with whether one
enters a whole number (such as 12) or a decimal number (such as 12.0). These are, after all, the same
number. In Mathematica, however, they are treated very differently, and for a good reason. Decimal
numbers are cursed with an inherent ambiguity stemming from the fact that while there are an
infinite number of decimal places, we cannot possibly write them all. There are two distinct
situations in which one would write a decimal number with finitely many places. In one, we write
such a number when we are rounding it, such as when writing S 3.14159. In the other, we agree to
writing 1 s 4 .25. Unfortunately, when one gives a computer a decimal number such as 3.14159 or
stop writing decimal digits if beyond a certain point all the digits are known to be zero, such as when
should the computer interpret that to mean 1 s 4, or should it instead read it as the first two digits of
.25, there is no way for the computer to know which situation you are in. When you type .25,
some potentially longer number, whose other decimal places are not known? Mathematica chooses
the latter: it treats all decimal numbers as approximations, where only the given decimal digits are
known, and where all additional decimal digits are treated as unknown. Mathematica refers to such
numbers as Real numbers.
8.3 Numbers 393
By contrast, an Integer, or whole number, is exact. There is no ambiguity. Likewise, fractions with
integer numerator and denominator are also exact. Such fractions are called Rational numbers.
While any kind of number is an atomic expression in Mathematica, the Head command (introduced
in the last section) can be used to identify the type of any number.
In[1]:= Head#12.0'
Out[1]= Real
In[2]:= Head#12'
Out[2]= Integer
Out[3]= Rational
It is often a shock for new Mathematica users to encounter output such as the following:
22
In[4]:=
7
22
Out[4]=
7
Mathematica simply will not convert an Integer or Rational number to a Real unless instructed to
do so. In cases where you seek a decimal output, either enter at least one Real number in the input,
or use the N command to convert it for you. Note that typing 22. is the same as writing 22.0.
22.
In[5]:=
7
Out[5]= 3.14286
ss N
22
In[6]:=
7
Out[6]= 3.14286
In addition to Integer, Rational, and Real numbers, there are also complex numbers, such as 2+3Ç,
where Ç represents the square root of 1. Regardless of which type of numbers comprise the individ-
ual real and imaginary components of such a number, Mathematica treats the entire expression as a
Complex number.
Out[7]= Complex
394 Programming
Out[8]= Complex
Displaying Numbers
It is important to understand that while real (decimal) numbers are displayed with six significant
digits, internally they are stored to at least machine precision (usually 16 significant digits). Perhaps
you have already discovered this; if you copy a real number from an output cell and then paste it
into a new input cell you will see the “full” machine representation of the number. Alternately, you
can use the command InputForm to display all the digits of a real number.
In[9]:= N#S'
Out[9]= 3.14159
It is possible to display all the digits of a real number in any format you can imagine. While
Mathematica’s InputForm is handy for peeking under the hood, a more practical command for
displaying numbers is NumberForm. Here, for example, we see the first 12 digits of S (including the
digit to the left of the decimal point).
This is not too exciting, as it looks like the output for N[S,12]. However, if the second argument to
NumberForm is a list of two positive integers, the first number specifies the total number of digits to
be displayed and the second specifies the number of digits to the right of the decimal. This can be
very useful. Here we see the first ten decimal places of S (compare the output carefully with the
output above; NumberForm displays the number rounded to the correct number of specified digits):
If you ask for more total digits than needed, they will not be displayed.
However the option NumberPadding allows you to specify characters to pad the areas to the left
and right of the displayed digits.
The padding on the left in the output above appears to have one extra character; this is the space
reserved for the sign character in the case of negative numbers:
A similar command is PaddedForm, which works essentially the same way as NumberForm, but it
will also “pad” the number with white space on the left, leaving room to accommodate all the
requested digits (and the sign character). Using PaddedForm will often free you from having to add
a NumberPadding option to NumberForm.
Out[18]//NumberForm=
1.00000
Out[19]//PaddedForm=
1.00000
PaddedForm is useful for displaying numbers in a table so that numbers in a column are all dis-
played with the same number of places to the right of the decimal, and with decimal points aligned.
396 Programming
Numbers that are very small or very large, however, will disturb the neat display above. Such num-
bers will (sensibly enough) be displayed in scientific notation (with the requested 2 places shown to
the right of the decimal):
1.23 106
If you do not want such numbers represented in scientific notation (for instance, if you are display-
ing monetary values and want your answer in dollars and cents), both NumberForm and Paddedg
Form accept the option setting ExponentFunction (Null&), which prohibits the display of
exponents.
This mechanism provides a sensible means of representing quantities such as money, where pre-
cisely two decimal places should be displayed. The following command could be used whenever a
monetary value x is to be shown. This particular implementation allows for at most ten digits to the
left of the decimal (so don’t use it to display the national debt).
Clear#dollar';
dollar#xB' : PaddedForm$N#x', 12, 2, ExponentFunction +Null &/(
In[25]:=
8.3 Numbers 397
0.00
0.01
Out[27]=
3.14
3141592653.59
Suppose you have one dollar, and it loses 1 s 3 of its value each year. Here’s what happens to your
dollar over time. The second column shows the numerical value to the default six significant digits,
while the third column displays this same value rounded to the nearest penny:
0 1. 1.00
1 0.666667 0.67
2 0.444444 0.44
3 0.296296 0.30
4 0.197531 0.20
5 0.131687 0.13
6 0.0877915 0.09
7 0.0585277 0.06
Out[28]=
8 0.0390184 0.04
9 0.0260123 0.03
10 0.0173415 0.02
11 0.011561 0.01
12 0.00770735 0.01
13 0.00513823 0.01
14 0.00342549 0.00
15 0.00228366 0.00
More complex structures such as loan amortization tables can be built in a similar fashion.
If the number we wish to display is an exact integer (no decimal point) we need not specify digits of
precision, and scientific notation will not be used.
In[29]:= NumberForm$1030 (
Out[29]//NumberForm=
1000000000000000000000000000000
But very large numbers are easier for humans to read if the digits are blocked in, say, groups of three.
NumberForm and PaddedForm have an option called DigitBlock to allow for this sort of display.
398 Programming
In fact there are a host of options that allow you full control over the display of numbers. Look up
NumberForm in the Documentation Center for more information.
Out[31]//NumberForm=
1 00000 00000 00000 00000 00000 00000
Note that you can go into Mathematica’s Preferences panel, and make global adjustments to these
settings. In the Preferences panel, look under Appearance # Numbers # Formatting , and tweak to your
heart’s content. This will invoke your display preferences for every session.
In[32]:= Clear#dollar';
dollar#xB' :
PaddedForm$N#x', 12, 2, ExponentFunction +Null &/, DigitBlock 3(
In[34]:= dollar$109 (
Out[34]//PaddedForm=
1,000,000,000.00
NumberForm and its cousin PaddedForm have several other close relatives including Scientificg
Form, EngineeringForm, and AccountingForm. These work much the same way, but have differ-
ent default settings. Information can be had in the Documentation Center.
In[36]:= 21024
Out[36]= 179 769 313 486 231 590 772 930 519 078 902 473 361 797 697 894 230 657 273 430 081 157 g
732 675 805 500 963 132 708 477 322 407 536 021 120 113 879 871 393 357 658 789 768 g
814 416 622 492 847 430 639 474 124 377 767 893 424 865 485 276 302 219 601 246 094 g
119 453 082 952 085 005 768 838 150 682 342 462 881 473 913 110 540 827 237 163 350 g
510 684 586 298 239 947 245 938 479 716 304 835 356 329 624 224 137 216
In most cases you’ll find that other programs will give an answer like the output above for 21023, but
they will choke on 21024, and fail to produce an answer. This is because most programs rely on your
computer’s hardware, in particular on its floating point unit (FPU), to carry out arithmetic opera-
tions. And this number is simply too big for most floating point units commonly in use at the time
of this writing. It’s a matter that most of us rarely think about, but while the real number system is
infinite, the number system utilized by most FPU’s (commonly IEEE double–precision floating point
arithmetic) is finite. That is, there is a finite quantity of numbers available in this system, and so
there is necessarily a largest number. In most systems it happens to be just under 21024. The bound-
ary occurs at a power of two since the FPU converts numbers to base 2 before operating on them.
You can have Mathematica query your hardware and determine the largest number supported by the
FPU on your machine by entering the following:
In[37]:= $MaxMachineNumber
In[38]:= N$21024 (
Mathematica handles numbers differently than most other programs. It will make use of your
computer’s FPU whenever possible in order to save time, since hardware is generally several orders
of magnitude faster than software. But when you input a real number too large, too small, or too
precise for the FPU to handle, or if the result of evaluating your input produces such a number,
Mathematica will seamlessly switch into high precision mode, abandoning the FPU and carrying out
the calculation itself. In one sense you never need to worry about it, for it happens automatically. In
another sense, it is useful to understand just how Mathematica interprets real numbers so that you
can better understand its output, and so that you can manually switch to high precision arithmetic
should you desire to do so.
For instance, enter the following input. Then select the output with your mouse, copy it, and paste
it into a new input cell. This is what you will see:
In[39]:= N#S'
Out[39]= 3.14159
3.141592653589793`
400 Programming
This procedure causes Mathematica to give you a peek under the hood at how it views this number.
You can also force Mathematica to show it to you with the command FullForm (the command
InputForm will also display all the digits of the number, but it does not display the backquote
character `).
We already know that the N command will by default display only six significant digits of a num-
ber, while internally there is a machine number lurking underneath. Machine numbers, that is,
numbers accessible to your computer’s FPU, never have more than a fixed number of significant
digits, usually 16. We will use the term precision to indicate how many significant digits a real
number has, and hence we say that machine numbers generally have a precision of 16. Machine
numbers are identified (in FullForm) by the backquote ` appearing as the final character. If you ever
copy and paste a number, revealing its internal structure as a machine number, don’t worry. Num-
bers can be input in this form and the output is no different than it would otherwise be.
Out[41]= 314.159
One way to input a high precision number is to type a decimal number with a total of more than 16
digits. Another is to use N specifying (with the second argument) more than 16 digits of precision.
Out[42]= 2.000000000000000000000000000000000000000
If you copy and paste the output above, or apply FullForm to it, you will see the internal structure
of a high precision number:
The FullForm of a high precision number is the number itself followed by the backquote character `
followed by the number’s precision. High precision numbers may also be entered directly this way:
In[44]:= 2`40
Out[44]= 2.000000000000000000000000000000000000000
In order to understand Mathematica’s internal form for any high precision number, we just combine
the notation above with the internal form for scientific notation (discussed in the previous subsec-
tion). For example:
8.3 Numbers 401
A simple way to force Mathematica to jump into high precision mode when evaluating an input is to
make sure every number appearing in the input is a high precision number. If any single number in
the input has only machine precision, the answer can be no more precise. The Precision command
displays the precision of any quantity.
In[46]:= Precision#2`40'
Out[46]= 40.
Out[47]= 40.
In[48]:= Precision#2.'
Out[48]= MachinePrecision
When high precision numbers are combined arithmetically, the precision is no greater than that of
the least precise number in the input.
Out[49]= 4.00000000000000000000000000000
In[50]:= Precision#'
Out[50]= 30.
However, with some operations the precision can decrease below that of the least precise number in
the input. In the following example it is useful to think of each number as an infinite decimal
number, the first few digits of which are 2 followed by 29 or 39 zeros. When subtracted, their
difference begins 0.000000… (with 29 zeros after the decimal point), but beyond that nothing is
known. No significant digits of the difference can be surmised. The precision is 0.
Out[51]= 0. 1030
In[52]:= Precision#'
Out[52]= 0.
This may seem unfair. However, we do know that the result is zero to 29 decimal places, even
though we don’t know any of the actual nonzero digits of the difference. The command Accuracy
will tell you (roughly) how many digits to the right of the decimal point are known to be correct.
402 Programming
In[53]:= Accuracy#'
Out[53]= 29.699
The following won’t give a high precision number, since the machine precision number 2.0 appears
in the input. You cannot take a machine precision real number and increase its precision. In fact,
you cannot do any operation involving a machine precision number and end up with high precision
output.
Out[54]= 2.
In[55]:= Precision#'
Out[55]= MachinePrecision
In[56]:= Precision#2'
Out[56]=
In[57]:= Precision#S'
Out[57]=
Exercises 8.3
1. You may be familiar with the parable about the peasant who is to be rewarded by the king with
many sacks of rice. The peasant says, “Why don’t you simply give me one grain of rice the first
day, 2 the second day, 4 the third day, and so on, each day giving me twice the quantity of the
previous day, and do this for one month?” The King, thinking this will cost less, agrees. Make a
table showing how many grains of rice the King owes each day, from day one to day 31. Use
DigitBlocks of length three to make the numbers easy to read.
2. In this section we advocate the use of PaddedForm to display numbers in a table or column,
each with the same number of decimal places and with the decimal points aligned. Another
technique for accomplishing such alignment without necessarily restricting the number of
decimal places is with the Alignment option in the Grid and Column commands. Look up this
option in the Documentation Center, and use it to display the Table below in a column. You
may also wish to consult Exercise 7 in Section 3.5.
8.4 Map and Function 403
Map is a command for applying a function to each member of a list. For instance, we can test a list
of numbers to see which of the numbers are primes:
Clear#f, a, b, c';
Map#f, a, b, c'
In[2]:=
The first argument of Map is a function or command. The second argument is a list. The members
of the list are fed to the function, one by one, and the resulting list of values is returned.
There is a commonly used infix syntax for Map. Instead of typing Map[f, list], one can instead type
f /@ list. Effectively, one can Map a function over a list with just two keystrokes:
In[4]:= f s a, b, c
This will seem strange at first, but it is akin to typing 2 3 instead of the more formal Plus[2, 3]. It
departs from the standard “square bracket” notation employed by most Mathematica commands.
With a bit of practice, however, it’s quite natural.
We now turn our attention to a second command, Function, that is typically used to construct
functions to be used only once (for instance, functions to be Mapped over a list). In order to create a
function that squares its argument, for example, and apply it to every item in a list, either of these
inputs will do:
404 Programming
Out[5]= a2 , b2 , c2
Out[6]= a2 , b2 , c2
This may seem like overkill; we’ve typed more input than the output we produced. But the idea is
extremely powerful, as we can now map any function over any list. We’ll explore more interesting
examples shortly.
In general, to create a function one types Function[input,output]. The input variable x above could
be replaced by any symbol; it’s simply a dummy variable. An alternate syntax is commonly used
that both minimizes typing and standardizes the name of the dummy variable to be the Slot charac-
ter Ó. To give a Function using this syntax, use only one argument: the output expression (using
the Ó for the variable):
Out[7]= a2 , b2 , c2
Even more brevity in typing can be attained by disposing entirely of the Function command name.
One may simply type the output expression (again using only Ó for the variable) and then type the
ampersand character & to mark the end of the function. It’s a bit odd at first, but you’ll pick up on it
quickly. Instead of Function$Ó2 (, for example, one instead may type a paltry three characters: Ó2 &.
For instance:
Out[8]= a2 , b2 , c2
Or even better:
Out[9]= a2 , b2 , c2
This last form is the most cryptic, but it is also the quickest to type, and is by far the most com-
monly encountered syntax convention for mapping a function over a list. You will see it frequently
in examples in the Documentation Center. Here are a few examples. Remember the Slot character Ó
is simply a stand-in for each member of the list that follows.
8.4 Map and Function 405
3.
3.1
3.14
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
Out[10]=
3.1415926536
3.14159265359
3.141592653590
3.1415926535898
3.14159265358979
3.141592653589793
3.1415926535897932
3.14159265358979324
3.141592653589793238
3.1415926535897932385
Out[11]=
0.5 1.0 1.5 2.0 0.5 1.0 1.5 2.0 0.5 1.0 1.5 2.0
0.5 0.5 0.5
Out[12]=
GraphicsRow$
Plot$xÓ , x, 0, 1, Ticks None, AspectRatio 1, AxesOrigin 0, 0( & s
In[13]:=
1 s 6, 1 s 5, 1 s 4, 1 s 3, 1 s 2, 1, 2, 3, 4, 5, 6(
Out[13]=
In[14]:= Text Style$Grid$Ó, " ", Expand#Ó' & s Table$+1 x/n , n, 9(, Alignment Left(,
"TraditionalForm"(
1x 1x
+1 x/ 2
1 2 x x2
+1 x/3 1 3 x 3 x2 x3
+1 x/4 1 4 x 6 x2 4 x3 x4
+1 x/5 1 5 x 10 x2 10 x3 5 x4 x5
+1 x/6
Out[14]=
1 6 x 15 x2 20 x3 15 x4 6 x5 x6
+1 x/ 7
1 7 x 21 x2 35 x3 35 x4 21 x5 7 x6 x7
+1 x/ 8
1 8 x 28 x2 56 x3 70 x4 56 x5 28 x6 8 x7 x8
+1 x/ 9
1 9 x 36 x2 84 x3 126 x4 126 x5 84 x6 36 x7 9 x8 x9
Note that it is possible to Map a Function over a list without using either command. Beginning in
Mathematica 6, one may use the special iterator form {x, list} in a Table to accomplish the same
thing. In fact, this idea has been used repeatedly throughout this book. For instance:
8.4 Map and Function 407
Out[15]= a2 , b2 , c2
GraphicsRow$
Table$Plot$Sin$x4 (, x, 0, 2, PlotStyle sty(, sty, Dashed, Dotted, Thick ((
In[16]:=
Out[16]=
0.5 1.0 1.5 2.0 0.5 1.0 1.5 2.0 0.5 1.0 1.5 2.0
0.5 0.5 0.5
Keep in mind, however, that Mathematica has been around since the late 1980s. Prior to version 6,
the Map-Function combo was ubiquitous. Its use still pervades examples in the Documentation
Center, and is found in much of the code you are likely to see. And it’s more than a relic. It is, with
a little practice, very easy both to type and to read. It avoids the use of a dummy variable (like the
sty variable in the example above). And in cases where the list being mapped over is generated by
Table, the Map-Function combo negates the need to nest one Table inside another.
It should also be noted that both Function and Map are widely used independently of each other.
In short, both are essential programming tools.
A Function can be given multiple arguments. This form is required by the Sort command, which is
used to sort a given list. For instance:
Out[17]= 40, 1, 79, 47, 51, 68, 45, 12, 19, 18, 64, 100, 91, 5, 11
In[18]:= Sort$myList(
Out[18]= 1, 5, 11, 12, 18, 19, 40, 45, 47, 51, 64, 68, 79, 91, 100
The sorting may be accomplished via a sorting function which returns True precisely when its two
arguments are given in the desired order. Each of the notations below accomplish the same thing:
they put the list in reverse order:
Out[19]= 100, 91, 79, 68, 64, 51, 47, 45, 40, 19, 18, 12, 11, 5, 1
Out[20]= 100, 91, 79, 68, 64, 51, 47, 45, 40, 19, 18, 12, 11, 5, 1
408 Programming
Out[21]= 100, 91, 79, 68, 64, 51, 47, 45, 40, 19, 18, 12, 11, 5, 1
Note that there are many other ways to accomplish this. For instance, one could Reverse the list
after it is sorted in ascending order, or one could call the built-in Greater command, which accom-
plishes the same thing as our Function above:
In[22]:= Reverse$Sort$myList((
Out[22]= 100, 91, 79, 68, 64, 51, 47, 45, 40, 19, 18, 12, 11, 5, 1
Out[23]= 100, 91, 79, 68, 64, 51, 47, 45, 40, 19, 18, 12, 11, 5, 1
But here we sort a list of vectors according to their Norm. There is no better way to accomplish this
feat than with a sorting Function:
82, 7, 96, 97, 10, 6, 13, 96, 6, 28, 63, 42, 45, 75, 17,
87, 98, 40, 84, 86, 58, 62, 0, 6, 59, 35, 65, 2, 97, 35
Out[24]=
62, 0, 6, 28, 63, 42, 45, 75, 17, 59, 35, 65, 13, 96, 6,
97, 10, 6, 2, 97, 35, 82, 7, 96, 84, 86, 58, 87, 98, 40
Out[25]=
And here we sort the same list of vectors according to the value of the third coordinate:
62, 0, 6, 13, 96, 6, 97, 10, 6, 45, 75, 17, 2, 97, 35,
87, 98, 40, 28, 63, 42, 84, 86, 58, 59, 35, 65, 82, 7, 96
Out[26]=
In[27]:= Clear$myList(
y2 0
, x, 2, 2, y, 2, 2 , RegionFunction +Norm#Ó1, Ó2' 2 &/,
2
In[28]:= Plot3D%Æ,x
Out[28]=
Functional Programming
Mathematica is a functional programming language. That is, unlike languages such as BASIC and C,
Mathematica commands can operate not only on specific types of numbers and data structures, but
on arbitrary expressions. In particular, the argument to one function may be another function. This
provides a powerful and elegant paradigm for programming. While we will not provide a
philosophical discussion on the advantages and nature of functional programming, suffice it to say
that if you’ve read and followed this section to this point you’re already doing it. Map and Function
are your point of entry. Commands such as Apply, Thread, and MapThread (introduced below)
will expand your horizons, while Nest, Fold, replacement rules and pattern matching (introduced
later in the chapter) will take you to the next level.
A useful command that is similar to Map is Apply. Like Map, Apply takes two arguments: a func-
tion and an expression (often a list). The output is the second argument, with its head replaced by
the function in the first argument. That’s it; Apply will pluck the head off of any expression and
replace it with something else. For example, Apply[Times, List[a, b, c]] will return Times[a, b, c]:
Out[1]= abc
Out[2]= abc
Here’s another example. We randomly generate 40 points in the plane (ordered pairs of numbers,
with each coordinate between 1 and 1), then replace Point by Line to connect the individual
points with line segments, and by Polygon to fill the resulting regions. Finally, we display all three
together.
410 Programming
Out[4]=
GraphicsRow$
Graphics$Line pts(, Graphics$Gray, Polygon pts (,
In[5]:=
Out[5]=
In[6]:= Clear$pts(
The command Thread can be used to “thread” a function over several lists. In the example below,
the (undefined) function is called f. It is called with two arguments, each of which is a list. Wrap-
ping this expression in Thread causes f to be called on corresponding members of the two lists, with
the output being a list of the results:
Out[7]=
Here are two applications. The first shows how to use Thread to programmatically create a list of
rules from a list of left-side values and a list of right-side values. In this case, the command Rule
plays the role of the function f above. The second example illustrates that Thread has the same
effect as Transpose on a list of lists (i.e., on a matrix). In this case List plays the role of the function
f above.
Out[8]=
8.4 Map and Function 411
Out[9]=
The command MapThread combines the functionality of Map and Thread. The example below
illustrates a typical implementation. It essentially does what Thread does, but there are two argu-
ments, with the function (in this case f) being given alone as the first argument.
An example that illustrates the utility of MapThread follows. Suppose you wish to construct a 3D
graphic of a right cylinder whose top and bottom are regular polygons. These polygons are easily
constructed (we utilize Map to add the third coordinate to each vertex):
In[11]:= n 10;
Out[15]=
The rub is constructing the n rectangles that make up the sides. Suppose that b1 , b2 is a list of two
adjacent vertices on the bottom polygon, and that t1 , t2 are the vertices on the top polygon
directly above these. The side of the cylinder with these four vertices will have the form
Polygon[{b1 , b2 , t2 , t1 }]. To get this, we will Partition the top and bottom vertices into sublists of
length 2 with an offset (or overlap) of 1, and Reverse each ordered pair of vertices on the top. We’ll
then use MapThread to Join the corresponding lists. Here’s an illustration of the idea:
Out[20]=
Exercises 8.4
1. The command First, when applied to a list, will return the first item in the list. More generally,
First[f[a,b,c]] will return the first argument a for any command f. Compare the outputs of the
commands Options[NSolve] and First/@ Options[NSolve]. What is the command f to which
First is being applied in this case?
2. Use Function to define a command which when given an integer argument n between 1 and 26
will return the nth letter of the alphabet. Use it to find the 19th letter of the alphabet.
f ,x, y0
6. How would you use Function to define the multivariable real-valued function
x2 y x 3y?
7. How would you use Function to define the vector field f ,x, y0 <x2 y x 3y, cos x@?
8. Map, Function, and Apply can be used to transform a simple Table into a stunning display. In
this exercise you will work with the table below, where each row holds the left and right sides of
a trigonometric identity.
tan+2 a/ n
cos2 +a/sin2 +a/
2 cos+a/ sin+a/
b. Use Map, Function, Apply and Rule on t, and display the result with TabView and Traditiong
alForm to produce the output below:
2 cos+a/ sin+a/
9. Look up the term functional programming in Wikipedia, and contrast it with procedural
programming.
10. A cipher is an encryption scheme whereby each individual character in the message is replaced
by some other character or symbol. For instance, one could encode a message by replacing every
a with b, every b with c, and in general replacing each letter with the next letter, except for z
which is replaced with a. A cipher such as this that is based on a simple shift (replacing each
letter with the letter a fixed number of characters to its right) is called a Caesar cipher.
a. Use StringReplace, CharacterRange, Thread, and RotateLeft to build a command encode
414 Programming
that will implement this cipher on any String comprised of lowercase letters.
b. Create a decode command to decode an encrypted message (you may wish to use
RotateRight).
11. Modify the commands in the previous exercise so that a second argument controls the number
of places that each character is shifted to the left in the encryption process.
The most simple looping command is Do. Its syntax is like that of Table; the first argument is an
expression, and additional arguments are iterators. The expression is evaluated once for each value
assumed by the iterated variable. For example:
myList ;
Do$PrependTo$myList, k(, k, 10(;
In[1]:=
myList
Out[3]= 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Above we begin with an empty list myList, and then PrependTo (put at the beginning of) this list
each of the first ten whole numbers in turn, beginning with 1. While effective, this procedural code
is certainly not the easiest way to produce this list. Here’s another way:
In[4]:= Reverse$Range#10'(
Out[4]= 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
As a second example, we use Do to write a procedural program for calculating the factorial of any
integer. Yes, there is a built-in command Factorial (!) that does this already; the point is simply to
illustrate how Do works. We begin by having Do calculate 4 factorial. First the dummy variable x is
set to 1. Then as k assumes the integer values 1 to 4 in turn, x is set to k times its current value. So
first x becomes 1 1 1, then 2 1 2, then 3 2 6, then 4 6 24.
x 1;
Do#x k
x, k, 4';
In[5]:=
Out[7]= 24
8.5 Control Structures and Looping 415
We use this idea to create a command fac to calculate the factorial of any positive integer n. We use
SetDelayed (:=) for we do not want to evaluate the right side until fac is called.
For example:
approximating the root of an equation f +x/ 0. You may recall that this method begins with two
simple steps. For instance, below we get serious and harness Do to implement the secant method for
points x0 and x1 near the root in question. One then builds a sequence of values x1 , x2 , x3 ,… that
(one hopes) will be successively better approximations to the actual root. The sequence is con-
structed recursively via the second-order difference equation
f +xn /.
f +xn /f +xn1 /
xn xn1
xn1 xn
Below we harness Do to carry out nine steps of this process on the cubic f +x/ x3 2x 2 (you can
check that f has only one real root), starting at x0 1 and x1 3 s 2. In this implementation we
use x0 and x1 to represent the current values of xn1 and xn , respectively, in the equation above. We
use xtemp to temporarily hold the value of xn so that xn1 may assume this value in the next
iteration. Note also that the iterator for Do in this case is the ultra-simple {9}, which simply means,
“do this nine times.”
x0 1;
3 s 2;
In[12]:=
x1
Do%xtemp x1;
x1 x0
x1 x1 f#x1';
f#x1' f#x0'
x0 xtemp;
Print#N#x1, 40'', 9)
416 Programming
2.090909090909090909090909090909090909091
1.709454061251664447403462050599201065246
1.757205908865359830981764868540767091628
1.769832257373596686295309726912982898092
1.769287639221607075547801570536291382136
1.769292352411024860110478553708965415028
1.769292354238637603574664697348116397970
1.769292354238631415240401342331442514107
1.769292354238631415240409464335033492634
In this case we obtain 38 digit precision, for the actual real root (to forty digit precision) looks like
this:
Out[15]= x m 1.769292354238631415240409464335033492671
While Do is effective, the secant method and the Newton-Raphson method for approximating roots
can be implemented more efficiently using NestList. Implementations can be found in Section 8.7.
See page 437 for Newton-Raphson, and Exercise 5 on page 442 for the secant method.
Do can also accept more than one iterator. Below k assumes integer values from 0 to 3, while m
assumes the values a, b, and c.
myList ;
Do$AppendTo$myList, k m(, k, 0, 3, m, a, b, c(;
In[16]:=
myList
Out[18]= a, b, c, 1 a, 1 b, 1 c, 2 a, 2 b, 2 c, 3 a, 3 b, 3 c
In[19]:= Clear$myList(
Predicates
Many control structures rely on conditions that are either true or false. If a condition is true, a
certain set of instructions are given, while if the condition is false, an alternate set of instructions are
given. In logic, a statement that is either true or false is called a predicate. In computer science, a
command that returns one of the values true or false is sometimes referred to as a query. Mathematica
has many predicate commands, which often end in the letter Q (for query). These commands return
either the symbol True or the symbol False.
8.5 Control Structures and Looping 417
In[20]:= PrimeQ$216 1(
Out[20]= True
Out[21]= False, True, False, True, False, True, False, True, False, True
Another useful predicate command is FreeQ, which will return True if the expression appearing in
its first argument is completely free of the pattern or expression in its second argument. Note that
for atomic expressions (such as numbers), one can use their Head (e.g., Integer, Rational, Real,
Complex) for the second argument.
Out[22]= True
Out[23]= False
Out[24]= False
In[25]:= 2m3
Out[25]= False
Out[26]= True
Note also that one can reverse the output of a predicate command by wrapping it in Not. The prefix
form of Not[expr] is !expr.
In[27]:= False
Out[27]= True
In[28]:= PrimeQ#8'
Out[28]= True
In the case of equations, one may type != for Unequal, or use the button on the BasicMathInput
palette.
418 Programming
In[29]:= 23
Out[29]= True
Here’s an application that requires predicates: the Select command is used to select those items from
a list that satisfy a condition. More precisely, a predicate command is applied to each member of the
given list, and Select returns those items in the list for which the predicate is True. Here we Select
all integers from 1 to 30 that are prime:
It is common to use a Function to create a specialized predicate. Here we select those items in the
given list whose value is at least 4:
Out[31]= 4, 5, 6
Here are all integers from 1 to 1000 with more than 3 distinct prime factors:
Out[32]= 210, 330, 390, 420, 462, 510, 546, 570, 630, 660, 690,
714, 770, 780, 798, 840, 858, 870, 910, 924, 930, 966, 990
Select is useful for extracting numerical items from long lists. For instance, a few of the countries
listed in CountryData do not currently have oil consumption figures available.
Out[33]= Missing#NotAvailable'
Recalling that CountryData[] returns a list of all countries in the data set, the input and output
below reveals that (at the time of this writing) there are 237 countries in the world, and there are
numerical oil consumption values known for 211 of these, while these data are missing for the re-
maining 26.
Text Grid$
Table$k, If$PrimeQ#k', "prime", "composite"( , k, 101, 117, 2(,
In[36]:=
Dividers Gray(
101 prime
103 prime
105 composite
107 prime
Out[36]= 109 prime
111 composite
113 prime
115 composite
117 composite
22 1 is divisible by 3,
24 1 is divisible by 5,
26 1 is divisible by 7,
28 1 is NOT divisible by 9,
210 1 is divisible by 11,
212 1 is divisible by 13,
2 14
1 is NOT divisible by 15,
216 1 is divisible by 17, and so on.
He conjectured that 2n 1 will be evenly divisible by n 1 if and only if n 1 is an odd prime. The
numbers on the right in these examples, after all, are primes precisely in those cases where
2n 1
divisibility occurs. One can restate this conjecture as follows: will be an integer precisely when
n1
n 1 is an odd prime. Here we check it for the values of n up to 200. It would appear that Leibniz
was on to something!
420 Programming
The predicate above (the first argument in the If command) in this case is an equation with one of
True or False appearing on each side. It will be True if and only if the two symbols agree. For
instance:
Out[38]=
A quick way to make sure a long list (such as the Table above) contains only the symbol True is to
Apply the And command to it. And gives True only if each of its arguments is True. We see, for
instance, that Leibniz was correct up through n 338:
But, unfortunately, when n 340 the conjecture fails. And it fails for several larger values of n as
well. Here are the values of n 1 for which it fails up through 10 000 :
Out[42]= 341, 561, 645, 1105, 1387, 1729, 1905, 2047, 2465, 2701, 2821,
3277, 4033, 4369, 4371, 4681, 5461, 6601, 7957, 8321, 8481, 8911
So the conjecture, despite its promising start, is most definitely false. How exactly does it fail? Either
2340 1 234 1
341 is not prime while is an integer, or 341 is prime while is not an integer. It turns out
341 341
to be the former:
8.5 Control Structures and Looping 421
In[43]:= PrimeQ#341'
Out[43]= False
s. n 340)
2n 1
In[44]:= IntegerQ%
n1
Out[44]= True
Out[45]= False, False, False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False, False, False
2p1 1
Does it never fail the other way round? That is, if p is an odd prime, must it be the case that
p
is an integer? The answer is yes. This is a consequence of Fermat’s little theorem (which you can look
up online at MathWorld). So to his credit, Leibniz was half right. If he had a copy of Mathematica, he
certainly would have been able to see the folly of his original conjecture. Given that the first counter-
example occurs at n 340, and involves checking that 2340 1 is divisible by 341, it is understand-
able that he believed this conjecture. And given that you have access to Mathematica, it is a reason-
ably simple matter for you to make investigations of this nature to peer deeply into the world of
numbers.
Which is similar to If. The arguments come in pairs. The first argument in each pair is a predicate,
and the second is an expression to evaluate if that predicate is True. Which will return the output
associated with the first predicate that is True. For instance:
1 is a unit
2 is a prime
3 is a prime
4 is an even composite
5 is a prime
Out[47]=
6 is an even composite
7 is a prime
8 is an even composite
9 is an odd composite
10 is an even composite
The number n 1 satisfies the first and last predicate, but it is the expression corresponding to the
first that is evaluated.
422 Programming
Similar to Which is the Piecewise command, introduced in Section 3.6. It has the advantage of
reading very nicely when its infix form is utilized. Type ÈpwÈ followed by one or more ŹÊ
(Mac OS) or Å¹Ö (Windows), one for each additional line.
1 is a unit
2 is a prime
3 is a prime
4 is an even composite
5 is a prime
Out[48]=
6 is an even composite
7 is a prime
8 is an even composite
9 is an odd composite
10 is an even composite
The While command takes two arguments. The first is a predicate. The second is the body, which
will be evaluated repeatedly until the predicate returns False. Here we use a While loop to find the
first prime number greater than 1000. We Set a dummy variable k to be 1000, and then Increment k
(increase its value by 1) until it is a prime. The value of this prime (the current value of k) is then
returned.
k 1000;
While# PrimeQ #k', Increment#k'';
In[49]:=
Out[51]= 1009
Note that the Increment command has the alternate postfix syntax ++. That is, Increment[k] can
be typed as k++. Here, for instance, we use the same technique to find the first prime greater than
one million:
8.5 Control Structures and Looping 423
k 1 000 000;
While# PrimeQ #k', k ';
In[52]:=
The For command accepts four arguments (no pun intended), although three will suffice if the body
is empty. You can, for instance, use For to write a procedure like those above to find the first prime
number exceeding 1000: for k starting at 1000, and as long as k is not a prime, continue increment-
ing k by one. At this point, return the value of k. Here is how to implement this program:
Out[55]= 1009
The general syntax takes the form For[start, test, increment, body]. Upon entry, start is evaluated, and
then the increment and body are evaluated repeatedly until the test returns False. In the example
below, we take a starting number and repeatedly divide it by 2 until the result is no longer an
integer. The body makes use of the Print command, which forces the value of k to be printed at
each step.
1296
648
324
162
81
Exercises 8.5
1. Enter the input ?*Q to get a listing of all commands that end with a capital Q. Here you will find
many of the basic predicate commands that will output one of the symbols True or False.
2. The great French mathematician Pierre de Fermat (1601–1665) postulated that every number
exceeding by one the quantity two raised to a power of two must be a prime number. That is,
every number of the form 2,2 0 1 is prime according to Fermat. It was about a century later that
n
he was proved wrong, by none other than Leonard Euler. Find the first counterexample to
Fermat’s famous conjecture.
3. The two most commonly used methods for incrementing a dummy variable are Increment
(postfix form ++), described in this section, and PreIncrement (prefix form ++). Enter the com-
mands j = 1; j++ and k = 1; ++k, and describe the difference.
424 Programming
5. Write a For loop to carry out the following procedure: Beginning with the number 1, keep
adding a random integer chosen between 1 and 100 to the current value until such time as the
result is a prime.
a. Write the loop so that all intermediate results are displayed.
b. Write the loop so that a list of all intermediate values, including the last, is displayed.
c. Write the loop so that only the number of iterations required is displayed.
d. Run the procedure from part c 1000 times (using Table), and Tally the results.
In[1]:= n 10;
Out[2]=
The only potentially bad consequence of this construction is that the symbol n has been Set to the
value 10. This has the potential to interfere with other evaluations involving this symbol that you
might try to make. For instance, if after entering the input above, you try to Solve an equation for n
you will run into trouble:
8.6 Scoping Constructs: With and Module 425
Essentially, you must be diligent in Clearing all such assignments before using these symbols in
another setting. A better practice is to make assignments locally. This is easily accomplished by
putting them inside of a scoping command, such as With or Module. Whatever is assigned in a
scoping command stays in the scoping command.
In[4]:= Clear#n';
Out[5]=
In this case, n has not been assigned a value in the Global` context (where one typically works):
In[6]:= n
Out[6]= n
This means that the local assignment made to the symbol n will not interfere with subsequent
evaluations:
With accepts two arguments. Its first argument is a list of assignments. Its second argument is an
expression. The assignments given in the first argument only work in the expression appearing in
the second argument. Their scope is local; they do not persist afterward, nor do they affect any
previous assignments. The command name With suggests its use; you can read the input code like a
sentence that begins, “With n 10, do the following….” Here’s another example. Note how the
426 Programming
With statement does not affect the earlier assignment n = 3, and how this assignment does not affect
the n appearing within the With statement.
n 3;
10, n2 (
In[8]:=
With$n
Out[9]= 100
In[10]:= n
Out[10]= 3
Another useful scoping construct, and indeed a more general one, is Module. It works much like
With, insulating any symbols you have already defined from its own local variables, and vice-versa.
The main difference between Module and With (from the user’s perspective) is that the local
variables in a Module do not need to be assigned in the first argument (although they do have to be
listed there). Delayed assignments (:=) may also be used in a Module, while only immediate assign-
ments (=) can be used in With. Here’s a simple example:
We’re going to draw a star shape. We’ll begin with a list of 2n points equally spaced around the unit
circle. We then multiply every second point (as we move clockwise around the circle) by a scalar, to
move it farther from the origin along its radial axis. Finally, we connect the resulting list of points
with line segments. Let’s do this step by step with n 5:
In[11]:= n 5;
Note how our list has 2n 1 11 points, with the first equal to the last. To get a corresponding list
of scale factors for each of these points, we utilize the Riffle command to intersperse a scale-factor of
2.5 at every second position in a list of n 1 6 ones. The resulting list has length 2n 1 to match
our list of points.
8.6 Scoping Constructs: With and Module 427
Out[13]=
The final picture is obtained by multiplying the two lists (which multiplies their corresponding
members), and wrapping the resulting list of points in the Line command:
Out[14]=
Here is how we could organize the individual commands above into a coherent piece of code, all in
a single input cell, and in such a way that none of the local variables interferes with a global symbol
of the same name. Note that while n and scaleFactor are assigned in the first argument, the other
local variables pts and scaleList are listed but not assigned there. Rather, they are assigned in the
body of the Module. This body (the second argument to Module) is a CompoundExpression
(expressions separated by semicolons). The first two such expressions define the local variables pts
and scaleList, and the third creates the Graphics. Note also that the values assigned to pts and
scaleList depend on the values of n and scaleFactor. This would be impossible using With, where
all local variables must be assigned in the first argument, and independently of one another.
Out[17]=
Note that it is not necessary to use any local variables in the definition above, and hence not neces-
sary to use a Module at all. One could just do the following:
This happens in the example provided below, where the local variable circlePts is used twice. This
example shows code to produce illustrations of a circular frustum (loosely speaking, a cone with its
tip cut off). The output is a Manipulate in which the user controls the top and bottom radii and the
height h between them. The code is based on the example given at the end of Section 8.4 on page
411. In this case the top and bottom circles are approximated by many-sided polygons. The number
of sides for these polygons is determined by the step size (.05) in the Table that defines circlePts.
8.6 Scoping Constructs: With and Module 429
In[19]:= Manipulate%
),
r1, .6, "r1 ", .01, 1, Appearance "Labeled" ,
r2, 1., "r2 ", .01, 1, Appearance "Labeled" ,
h, 1., 0, 2, Appearance "Labeled" , Alignment Center)
r1 0.6
r2 1.
h 1.
surface area
Out[20]= , 1.78!
Note that moving the slider (we moved the one above to 1.78) will actually make an assignment to
the symbol x:
In[21]:= x
Out[21]= 1.78
The construction of dynamic interfaces with Mathematica, while remarkably simple compared with
most other programming languages, is a vast subject that falls beyond the scope of this book. The
tutorials in the Documentation Center titled “Introduction to Dynamic” and “Advanced Dynamic
Functionality” are excellent resources for those who wish to explore this arena. Our purpose here is
to introduce the DynamicModule command, and to understand its role in the context of the other
scoping commands. Like Module, any symbols declared in a DynamicModule will be insulated
from assignments made elsewhere. Below, for instance, we duplicate the input above within a
DynamicModule.
DynamicModule$x 2.5,
Slider$Dynamic#x', 0, 3(, Dynamic#x' (
In[22]:=
Out[22]= , 2.5!
The dynamic content is now completely insulated from the global variable x, whose value is still
1.78:
In[23]:= x
Out[23]= 1.78
8.6 Scoping Constructs: With and Module 431
Here’s a simple but more interesting example. Below we take a thick, orange PolarPlot whose
independent variable tops out at the dynamically controlled quantity u, and superimpose it with
the same (but thin) PolarPlot on the full domain 0 T 2S. A slider allows you to adjust u, so that
you can follow the parameterization from 0 to 2S.
DynamicModule$u 4.5,
Column$Slider$Dynamic#u', .01, 2S(,
In[24]:=
Out[24]=
2 2 4
2
4
This example illustrates once again the powerful implications of working in a functional program-
ming environment. The Dynamic command accepts general Mathematica expressions (such as the
Show expression above), and permits them to be dynamically updated.
The interface and input code above is nearly identical to what one could produce using Manipug
late. The real benefit of using dynamic programming constructions (instead of Manipulate) is the
precise control that is afforded regarding which quantities get dynamically updated and which do
not. This is controlled by careful placement of Dynamic elements. Above, both the static polar plot
and the dynamic orange plot are re-evaluated whenever the slider is moved. This is a consequence of
wrapping the entire Show expression in Dynamic. An alternate and slightly more sophisticated
input that produces the same output follows. In this case, the static plot is evaluated only once.
Only the thick orange plot is dynamically updated by the controller.
432 Programming
Out[25]=
2 2 4
2
4
In the input above we applied First to the thick, orange PolarPlot. This returns the first argument of
the Graphics generated by PolarPlot (essentially a Line object with dozens or hundreds of points).
Dynamic is applied to this quantity, and it is later displayed (by wrapping it in Graphics) together
with the static plot. The behavior of this output and the previous one are essentially the same, but
in principle this latter one is zippier and more responsive as the slider is moved, as fewer items need
to be dynamically updated. If you were to add a static but complicated ContourPlot to the Show
argument in each of the last two inputs, the relative zippiness provided by this latter approach
would be obvious.
A DynamicModule, unlike a Module, stores its information in the Front End. If you save a note-
book with the output cell above included and re-open it later, it will display properly and the slider
will still work, even if you do not re-evaluate the input. Moreover, you could copy the output above
and paste it in several different places. Each pasted copy would work independently of the others. In
essence, the DynamicModule can be thought of as providing insulation in such a way that it stakes
out specific real estate in a notebook in which the localizations take place.
8.7 Iterations: Nest and Fold 433
Exercises 8.6
1 1 1
1. Here is one way to generate the nth partial sum 1 of the harmonic series. Explain
2 3 n
how it works, and use it to calculate the 100th partial sum of the harmonic series. Compare the
output with that of the built-in command HarmonicNumber. See also Section 8.7 on page 439
for an alternate definition using Fold.
); s)
1
harmonicNumber#nB' : Module%s 0, For%i 1, i n, i , s s
i
2. A local variable named x within a Module creates a symbol with a name such as x$30075, where
the stuff to the right of the local variable name is the current value of the system variable $Modg
uleNumber. This variable is incremented each time any Module is called. Create a Do loop that
will evaluate the expression Module[{x}, Print[x]] ten times.
3. In addition to With and Module, there is a third scoping command called Block, whose syntax
and purpose matches that of Module. Block uses a slightly different approach to insulate its local
variables. To illustrate how it works, suppose that you have entered x = 3 in a session, and then
create a Block with the local assignment x = 2. No new symbols will be created. Rather, when the
Block is evaluated the value of x will be temporarily cleared and the local assignment will be
utilized. After Block is finished the old value of x will be restored.
a. Enter the input below to see Block in action.
The command NestList is a fundamental tool for iterating a function. When one enters Nestg
List[command, start, n], a list of length n 1 is created with start as the first entry. This is followed by
the result of applying command to start and then the result of applying command to this result, and
434 Programming
so on, up through n applications of command. The command Nest is similar, and will output only
the last item in this list.
Out[3]= f#f#f#x'''
Here’s a famous example of the type of problem that is especially amenable to computer exploration
using iterations. It is known as the reverse-add problem, or sometimes as the versum problem (this
being a derivative of “reverse sum”). Take a positive integer and add it to the integer obtained by
writing the original number backwards. For instance, if the original number is 29, one adds
29 92 121. The result in this case is a palindrome, a number that reads the same forward and
backward. If you start with 39 and carry out this procedure you get 39 93 132 which is not a
palindrome. However, apply the procedure to 132 and you get the palindrome 363. It was conjec-
tured long ago that no matter what the starting number a palindrome will eventually result when
this procedure is iterated.
Here is a means of using Mathematica to carry out the reverse-add procedure. Each step of the
procedure can be accomplished by extracting the digits of the input number (IntegerDigits), revers-
ing this list of digits (Reverse), then converting the reversed digit list back to a number (FromDigits)
and finally adding it to the original.
In[4]:= Clear$step(;
step#nB' : n FromDigits$Reverse$IntegerDigits#n'((
For example:
To explore the conjecture (that every input will lead eventually to a palindrome), let’s make a
command that will identify palindromes. The command palStyle accepts an integer as input and
outputs that integer with a frame around it if it is a palindrome and in black otherwise.
Out[9]= 79, 176, 847, 1595, 7546, 14 003, 44 044 , 88 088 , 176 176, 847 847,
1 596 595, 7 553 546, 14 007 103, 44 177 144 , 88 354 288, 176 599 676,
853 595 347, 1 597 190 705, 6 668 108 656 , 13 236 127 322 , 35 608 290 553 !
For this particular application it would be nice to have a mechanism whereby the iterations would
stop as soon as a palindrome is produced, for it is simply not clear how many iterations may be
required. The command NestWhileList (or NestWhile if only the last item is to be output) is the
ticket. The syntax is like that of NestList, but instead of using a positive integer for the third argu-
ment to indicate the number of iterations, use a predicate instead. The iterations will continue as
long as the predicate returns True. Here, for instance, we set up a pure function for this purpose:
And here we see the number of iterations required to reach a palindrome for each of the first 195
integers. That is, above each integer n on the horizontal axis we see a vertical bar indicating the
minimal number of iterations required to reach a palindrome. Integers n that are palindromes
appear directly on the horizontal axis:
In[11]:= ListPlot$Table$n,
Length$NestWhileList$step, n, IntegerDigits#Ó' Reverse$IntegerDigits#Ó'( &((
1 , n, 195(, Filling Axis, PlotStyle PointSize#.002',
PlotRange All, AspectRatio 1 s 3, AxesOrigin 0, 0(
Out[11]=
We stopped at 195 here for a very good reason. The number 196 will not produce a palindrome even
after millions of iterations. For this reason it is strongly suspected that the original conjecture is false,
although at the time of this writing this has not been proved. In other words, no one really knows if
after enough iterations of this procedure starting at 196 a palindrome will be produced. All that is
known is that a palindrome will not be produced quickly. If you were to call our NestWhileList
input with 196 as the starting value, it would run (if you let it) for days, weeks, maybe years. Suffice
it to say that you might get bored waiting. For this reason it is possible to add an escape mechanism
to NestWhileList so that after a certain number of iterations it will stop, regardless of whether the
predicate is True or not. The following input accomplishes this. The fourth argument (1) indicates
that the predicate needs only one argument (the last result). The final argument (50) specifies the
436 Programming
maximal number of iterations to allow. Here we see that there are no palindromes in the first 50
iterations when one starts with 196:
In[12]:= palStyle s
NestWhileList$step, 196, IntegerDigits#Ó' Reverse$IntegerDigits#Ó'( &, 1, 50(
Out[12]= 196, 887, 1675, 7436, 13 783, 52 514, 94 039, 187 088, 1 067 869, 10 755 470, 18 211 171,
35 322 452, 60 744 805, 111 589 511, 227 574 622, 454 050 344, 897 100 798,
1 794 102 596 , 8 746 117 567, 16 403 234 045, 70 446 464 506 , 130 992 928 913 ,
450 822 227 944 , 900 544 455 998 , 1 800 098 901 007 , 8 801 197 801 088 ,
17 602 285 712 176 , 84 724 043 932 847 , 159 547 977 975 595 , 755 127 757 721 546 ,
1 400 255 515 443 103 , 4 413 700 670 963 144 , 8 827 391 431 036 288 ,
17 653 692 772 973 576 , 85 191 620 502 609 247 , 159 482 241 005 228 405 ,
664 304 741 147 513 356 , 1 317 620 482 294 916 822 , 3 603 815 405 135 183 953 ,
7 197 630 720 180 367 016 , 13 305 261 530 450 734 933 , 47 248 966 933 966 985 264 ,
93 507 933 867 933 969 538 , 177 104 867 844 767 940 077 , 947 154 635 293 536 341 848 ,
1 795 298 270 686 072 793 597 , 9 749 270 977 546 801 719 568 ,
175 095 833 209 091 234 750 057 , 925 153 265 399 993 573 340 628
18 408 442 064 004 592 449 047 , 92 502 871 604 050 616 929 528 ,
Let’s apply this procedure to each of the first thousand numbers and make a list of the results:
data Table$NestWhileList$step, n,
IntegerDigits#Ó' Reverse$IntegerDigits#Ó'( &, 1, 50(, n, 1000(;
In[13]:=
For example:
Out[14]= 485, 1069, 10 670, 18 271, 35 552, 61 105, 111 221, 233 332 !
Here is a ListPlot like that produced earlier, but with a Tooltip added which will display the coordi-
nates of a data point as you mouseover it.
Out[15]=
Below are all numbers between 1 and 1000 that, like 196, do not produce a palindrome after 50
8.7 Iterations: Nest and Fold 437
iterations. In fact, even with many more iterations none of these numbers have ever produced a
palindrome. See Exercise 2.
Out[16]= 196, 295, 394, 493, 592, 689, 691, 788, 790, 879, 887, 978, 986
In[17]:= Clear#data'
mating a root of an equation f +x/ 0, where f is a differentiable function. The technique, you may
For a second example of programming iteratively, consider the Newton-Raphson method for approxi-
recall, entails making an initial guess x0 for the root, and then calculating a sequence of (what we
hope will be) successively better approximations x1 , x2 , x3 , … via the iterative formula
f +xn /
f '+xn /
xn1 xn
Here is a command newtonStep that can be iterated with NestList. In order that it may be iterated,
it needs to accept a single numerical input. But we would also like to be able to specify the function f
whose root we wish to approximate. We accommodate both of these demands by using the syntax
below:
));
f#x'
newtonStep#fB' Function%x, Simplify%x
f '#x'
In[20]:= f#xB' : 2 x2 ;
newtonStep#f'#x'
1 x
Out[21]=
x 2
1 x
Out[22]=
x 2
Either way, we know that the function f +x/ 2 x2 has a positive root at x 2 . Here we use the
Newton-Raphson technique to approximate this root, using the initial value of N[1,40]:
438 Programming
1.000000000000000000000000000000000000000
1.500000000000000000000000000000000000000
1.416666666666666666666666666666666666667
1.41421568627450980392156862745098039216
Out[23]= 1.41421356237468991062629557889013491012
1.41421356237309504880168962350253024361
1.41421356237309504880168872420969807857
1.4142135623730950488016887242096980786
1.4142135623730950488016887242096980786
We see that seven iterations are sufficient to give 38 digit accuracy in this case (since the last two
rows—the seventh and eight iterates—agree at every digit, and since two digits of precision were lost
during the iteration process). This agrees with our knowledge of the root:
In[24]:= N% 2 , 38)
Out[24]= 1.4142135623730950488016887242096980786
It may be worth recalling that the built-in command FindRoot is designed to be used in cases such
as this, where a good approximation to the root of a function is desired. Programming the Newton-
Raphson method is intended to shed light on the behavior of this algorithm. We don’t mean to
imply that it is the best and only tool for this purpose.
Out[25]= x 1.414213562373095048801688724209698078570
There are several other iteration commands available beyond Nest and NestList. One of the most
useful is FixedPointList. This is a special case of NestWhileList that halts when the outputs become
indisinguishable from one another. That is, it provides a simpler means of doing what the NestWhile
List input below does:
1.000000000000000000000000000000000000000
1.500000000000000000000000000000000000000
1.416666666666666666666666666666666666667
1.41421568627450980392156862745098039216
Out[26]=
1.41421356237468991062629557889013491012
1.41421356237309504880168962350253024361
1.41421356237309504880168872420969807857
1.4142135623730950488016887242096980786
8.7 Iterations: Nest and Fold 439
1.000000000000000000000000000000000000000
1.500000000000000000000000000000000000000
1.416666666666666666666666666666666666667
1.41421568627450980392156862745098039216
Out[27]=
1.41421356237468991062629557889013491012
1.41421356237309504880168962350253024361
1.41421356237309504880168872420969807857
1.4142135623730950488016887242096980786
And, as you would expect, there is a FixedPoint command that simply returns the final value:
Out[28]= 1.4142135623730950488016887242096980786
Here we use Newton’s method to give an approximation of S s 4 (i.e., as a root of f +x/ sin x cos x).
Out[29]= 0.78539816339744830961566084581987572105
Out[30]= 0.78539816339744830961566084581987572105
Note that FixedPoint and FixedPointList can accept a third argument, which specifies the maximal
number of iterations. This is useful when it is not clear in advance that the iteration will converge.
The commands Fold and FoldList are used to iterate a function of two variables over its first vari-
able, while the second variable assumes successive values in a given list. That sounds worse than it
is. The input below illustrates the idea. The first argument is the function to be iterated, the second
argument is the starting value for this function’s first variable, and the third argument is the list of
values for the function’s second variable. The length of this list controls the number of iterations to
perform:
Clear#f, a';
FoldList#f, a, 1, 2, 3'
In[31]:=
Out[32]= a, f#a, 1', f#f#a, 1', 2', f#f#f#a, 1', 2', 3'
Here is how to use FoldList to create a list whose nth member is the nth partial sum
1 1 1
1 of the harmonic series:
2 3 n
440 Programming
1
In[34]:= FoldList%Ó1 &, 1, Range#2, 10')
Ó2
1, !
3 11 25 137 49 363 761 7129 7381
Out[34]= , , , , , , , ,
2 6 12 60 20 140 280 2520 2520
Notice that changing the initial value from 1 to 1. causes numerical approximations to be used
throughout. This increases the speed of computation and produces results sufficient for plotting:
1
In[35]:= FoldList%Ó1 &, 1., Range#2, 10')
Ó2
Out[35]= 1., 1.5, 1.83333, 2.08333, 2.28333, 2.45, 2.59286, 2.71786, 2.82897, 2.92897
1
In[36]:= ListPlot FoldList%Ó1 &, 1., Range#2, 200')
Ó2
6
Out[36]=
3
An even simpler means of calculating partial sums is via the Accumulate command. Given a finite
list {a, b, c,…}, Accumulate will return a list of the partial sums: {a, a+b, a+b+c,…}.
1, !
3 11 25 137 49 363 761 7129 7381
Out[37]= , , , , , , , ,
2 6 12 60 20 140 280 2520 2520
Another useful iteration command is Differences, which will return the differences between succes-
sive members in a list.
Differences#1, 4, 9, 16'
3, 5, 7
In[38]:=
Out[38]=
Whereas one could use Nest to iterate this command, it will accept a second argument (specifying
the number of iterations desired) to save you the trouble:
8.7 Iterations: Nest and Fold 441
In[39]:= Differences$Range#10'2 (
In[40]:= Differences$Range#10'2 , 2(
Out[40]= 2, 2, 2, 2, 2, 2, 2, 2
In[41]:= Differences$Range#10'2 , 3(
Out[41]= 0, 0, 0, 0, 0, 0, 0
The input below shows a means of displaying successive differences for the first ten terms in the
harmonic sequence, where each row after the first represents the differences for the row above. The
display is a Column with each row aligned at its center. Individual rows are Grids (each with only
one row), where the ItemSize option is utilized to guarantee a fixed width for each item. It is this
fixed width that is needed to produce an easy-to-read display. Exercise 10 in Section 8.8 will have
you build a command to automate this procedure for any initial list.
Exercises 8.7
1. If one were to set f = Function[x, 2x], then the input Nest[f, x, 4] would produce the output 16x.
Give the definition of a Function called f so that Nest[f, x, 4] produces the outputs below. Be sure
to check your answers.
a. 10000 x
442 Programming
b. x16
c. 1 1 1 1x
1
d. 1
1 1
1
1
1
1x
2. When the reverse-add procedure is applied to some numbers, a palindrome is not produced even
after millions of iterations. It is suspected (although it has not yet been proved) that a palin-
drome will never result with these numbers. The numbers in this class are known as the Lychrel
numbers (do an internet search for A023108 and follow the link to the Online Encyclopedia of
Integer Sequences for more information). Carry out an investigation of the orbit of the number
196 under ten thousand iterations of the reverse-add procedure, and confirm that no palindrome
is produced.
3. When an iteration scheme has a fixed point, it is often a matter of interest to understand how
quickly the fixed point is approached. Does it take many iterations to get (for instance) 100 digits
of precision, or just a few? A very simple means for garnering a qualitative assessment of the rate
of convergence for an iterative sequence of real numbers can be had as follows: Make an Arrayg
Plot where each row represents an iterate, and where each digit is represented by a different
tonal value. When a particular decimal position stabilizes to its final value, the column in the
array representing that position will be monotone from that point on down. This concept is
illustrated below:
b. Map the function First[RealDigits[#]]& over the output above to convert each number into a
list of its digits.
c. Wrap the output above with ArrayPlot to produce a visual representation.
d. Repeat parts a through c in a single input, but where the initial value is N[1,100].
e. Repeat part d, where the function to be iterated is f +x/
1 x
, and where there are 200 (as
x 3
opposed to 10) iterations in total. Contrast the results to those of part d.
4. Add the option setting ColorFunction "Rainbow" to your favorite ArrayPlot and see what
happens.
5. The secant method for finding a real root of an equation f +x/ 0 was discussed in Section 8.5 on
page 415, where it was implemented via a Do loop. If you go online and visit MathWorld
(www.mathworld.com) and lookup “secant method,” you will find the Mathematica code shown
6. When a function f is iterated and converges to a fixed point x
, it must be the case that f +x
/ x
(why?). Geometrically, this means that the point +x
, x
/ is the intersection of the graphs of
y f +x/ and y x. One often illustrates the convergence of the iteration from a particular starting
f +x/ and y
together with line segments joining the points +x0 , x0 /, ,x0 , f +x0 /0, ,f +x0 /, f +x0 /0, ,f +x0 /, f ,f +x0 /00,
value x0 by making a cobweb diagram. This is comprised of the graphs of y x,
iteration scheme. An illustration is provided below for the function f +x/ 2.9x+1 x/ and starting
and so on, with alternating vertical and horizontal segments heading ever deeper into the
0.8
0.6
0.4
0.2
of functions f +x/ a x+1 x/, with a slider for a ranging from 2.5 to 3.7, and a second slider for
7. Make a Manipulate showing cobweb diagrams (like that of the previous exercise) for the family
8. The command ContinuedFraction will accept a real number as input and will output a list of
integers that specifies the simple continued fraction form of the input (provided a finite or repeat-
ing continued fraction exists). The output {1,2,3}, for example, represents the continued fraction
1
1 1
.
2
3
10 1
a. Enter ContinuedFraction[10/7], and check that 1 1
.
7 2
3
b. Use Fold and Defer to write a command named displayCF that will accept a (finite) digit list
as input and will display the simple continued fraction corresponding to that digit list. For
1
instance, displayCF[{1,2,3}] should return 1 1
. Moreover (by using Defer), you will be able
2
3
1 1 1 1
9. Use Accumulate to find the first ten partial sums of the series 1 . Euler
4 9 16 25
2
S
showed this series converges to in 1735, solving a decades old problem (known as the Basel
6
problem) and securing fame for himself in the process. Make a ListPlot of the first thousand
S2
partial sums together with a horizontal line at height . Comment on the rate of convergence.
6
8.8 Patterns
For those who wish to program with Mathematica, patterns are often the most inaccessible aspect of
the language. They are the last frontier to be conquered. For those who “get it” on the other hand,
patterns are most definitely a source of power. With an understanding of the fundamentals of
patterns, there is the possibility of developing toward power-user status. Without such an
understanding, there is little hope.
The first thing to recognize is that you have been using patterns for quite a while. The most typical
instance is in a function definition that includes an underscore (_) on the left hand side, like the first
input shown below. While the subject of patterns is far too vast to adequately cover here, our hope
is to be able to convey enough basic knowledge and some illustrative examples so that you will be
able to recognize the power of patterns and replacement rules as you go about your work.
A pattern is a structure that can be used to represent an entire class of expressions. Mathematica has
extensive tools for building sophisticated patterns, for detecting when a particular expression
matches a given pattern, and for making replacements according to criteria given as patterns. We
have already used the most basic type of pattern when defining functions. The x_ on the left side of
the definition below, for example, is a pattern.
In[4]:= FullForm#xB'
Out[4]//FullForm=
Pattern#x, Blank#''
We already have a pretty clear sense that this means x is the independent variable. If the function f
above is called with a numerical argument, say for instance that the user enters f[2], then the
expression on the right side of the definition will be evaluated with 2 replacing the x and the result
8.8 Patterns 445
is 3. In the broader context of Mathematica itself, the pattern x_ can represent any structurally valid
expression (either an atom or a nested expression, as discussed in Section 8.2). For instance:
In[5]:= f$g#u'(
Out[5]= 1 g#u'
One important use of patterns is to restrict the class of expressions that will match the left side of a
definition. If, for instance, one wanted to define a function f that would only work with numeric
arguments, this structure would do the trick:
Clear#f';
f#xB ? NumericQ ' : x 1
In[6]:=
In[8]:= f#24'
Out[8]= 25
Non-numeric input does not match the pattern x_?NumericQ, and so the definition given above is
not applied; rather the expression is returned unevaluated:
In[9]:= f$g#u'(
Out[9]= f$g#u'(
There is much to say here. First note the structure of the pattern. The name of the pattern (we used
x) can be, of course, whatever you like. It is simply the name used to refer to the pattern on the
right side of the definition. The underscore is essential; we’ll discuss this soon. The ? can be followed
by any predicate command. An expression matches the pattern if and only if the predicate returns
True for that expression. A handy way to explore this idea is with the command MatchQ. The first
argument is an expression and the second is a pattern.
Out[10]= False
Out[11]= True
For the purpose of matching the name x is not even necessary. The underscore (Blank[]) suffices:
Out[12]= True
Next, note that the same symbol f may be given a different definition for a different form of input.
Recall that the function f was defined above for numeric input x as x 1. We can add another
definition for an input that is a string; f will then return an output corresponding to either type of
input.
446 Programming
In[15]:= f#3'
Out[15]= 4
In[16]:= f$apple(
Out[16]= f$apple(
Since apple is neither a string nor numeric (its head is Symbol), f returns unevaluated.
This notion of multiple definitions, one for each of several forms of input, can be useful. For exam-
positive integer n. If n is even, return n s 2. If n is odd, return 3n 1. Iterate this process while the
ple, consider the famous Collatz conjecture (for Lothar Collatz, who proposed it in 1937): start with a
result is not 1. The conjecture states that regardless of the starting number, the process will eventu-
ally lead to the number 1. The conjecture has been tested extensively, and while it appears to be
true, it has not been proven. But programming the function to be iterated is a snap:
Clear#f';
f#nB ? EvenQ' : n s 2;
In[17]:=
Here, for example, is the orbit of the starting number 342. It takes a while, but it eventually gets to
1. See Exercise 5 to further explore this conjecture.
Out[20]= 342, 171, 514, 257, 772, 386, 193, 580, 290, 145, 436, 218, 109, 328, 164, 82,
41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182,
91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790,
395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132,
566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238,
1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077,
9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244,
122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
8.8 Patterns 447
Now the function f above could also effectively be defined as a Piecewise function. Patterns in this
case provide an alternate approach. There are other cases where patterns provide a uniquely elegant
means of identifying a pertinent class of expression. In order to see such examples it is necessary to
broaden our knowledge of pattern structures. The next fundamental pattern structure we introduce
is a _ (Blank[]) followed immediately by a symbol, typically a command name. Any expression
having that symbol as its Head will match this pattern. It is the internal FullForm of the expression
(discussed in Section 8.2) that determines a match.
Out[21]= True
Out[22]= False
If a particular expression (such as {1,2,3} below) matches two patterns, the more specific will gener-
ally be used first:
In[23]:= Clear#f';
f#xBList' : Apply#Times, x';
f#xB' : x
Out[26]= 6
In[27]:= f$g#u'(
Out[27]= g#u'
Note that the evaluation sequence is important. Structurally, the input 232 is represented as
Power[23, 2] before evaluation. Its head is Power. After evaluation it becomes 529, and its head is
Integer. Expressions will be evaluated before being matched to a pattern.
In[28]:= Defer$FullForm$232 ((
Out[29]=
One may combine the two pattern structures discussed above. Suppose, for instance, you wish to
create a function which will only accept a positive integer as its argument. This can be accomplished
with the pattern _Integer?Positive. It will only match an expression that evaluates to an Integer,
and which returns True when the predicate command Positive is applied.
448 Programming
In[31]:= Clear#f';
f$xBInteger ? Positive( : 3x 1
In case you were wondering, yes, this could also be accomplished via the slightly more cumbersome
pattern _?(Positive[#]&&IntegerQ[#]&).
In[34]:= MatchQ $Ó, B ? ,Positive#Ó' && IntegerQ#Ó' &0( & s 23, 23, 23.
Now let’s consider those pesky underscores. While a single underscore _ (Blank[]) will match any
expression, a double underscore __ (two underscores back-to-back, full name BlankSequence[]) is an
object that will match any sequence of one or more expressions (i.e., expressions separated by com-
mas). Just as with the single underscore, it can be preceded by a name (e.g., x__) and it can be
followed by either a question mark and predicate, or by a command name. For instance, consider
the following definition. The pattern x__Integer will be matched by a sequence of one or more
integers. Every argument in the sequence must be an integer in order for there to be a match. The
name x refers to the entire sequence.
Out[37]= 6
In[38]:= f#3'
Out[38]= 3
Using the double underscore, you can easily create a command that is based on a built-in command.
For instance, below we create a command f that simply invokes ParametricPlot3D with the same
arguments. It produces two versions of the same image. The pattern args__ represents the entire
sequence of arguments.
8.8 Patterns 449
Out[42]=
Finally, there is the triple underscore ___ (BlankNullSequence[]), which will match any sequence of
zero or more expressions. This is especially useful for adding optional arguments to a user-defined
command. For example, the command myPlot will call the Plot command with some specific
option settings, including a PlotLabel and AxesLabel that are based on the values of the requisite
arguments. In the definition below, f and iter represent the requisite arguments for the Plot com-
mand, while opts represents any additional option settings the user wishes to add. Since such
settings have the head Rule, we demand this via the pattern opts___Rule. The triple underscore is
appropriate here since myPlot might be called without any options.
In[43]:= Clear$myPlot(;
myPlot$fB, iterBList, optsBBBRule( : Plot$f, iter, opts, PlotStyle Thick,
PlotLabel "y " aa ToString#TraditionalForm#f'', AxesLabel iter317, "y" (
y 1 x2
y
1.0
0.8
Out[45]= 0.6
0.4
0.2
x
1.0 0.5 0.5 1.0
450 Programming
Note that the PlotLabel and AxesLabels are based on the values provided to the myPlot command’s
requisite arguments (in the example below, for instance, the variable t is used instead of x). Note
also that because opts appears on the right side of the myPlot definition before the specific option
settings (e.g., PlotStyle Thick), any user-supplied option settings will override these defaults. For
instance, here a different PlotStyle is specified:
y Æt
y
20
15
Out[46]=
10
t
0.5 1.0 1.5 2.0 2.5 3.0
The three types of underscores can also be used in a StringExpression (~~). The inputs below
demonstrate their use in this setting. Given several strings, StringExpression will concatenate them
into a single string, as in the PlotLabel setting for myPlot above. Below the command Dictionaryg
Lookup is used to scour the dictionary for words that begin with “angle.” In the first case it finds all
such words that have a single additional character (there are three). In the second case it finds all
such words with one or more additional letters (there are seven, including the three from the first
output). In the last it finds all such words with zero or more additional letters. The output is the
same as the second case with one exception: the word “angle” itself is also present.
In[47]:= DictionaryLookup$"angle" aa B(
So far we have discussed the three types of underscores, that it is permissible to name a pattern by
preceding any type of underscore with a symbol (e.g., x_), and that it is possible to restrict the type
of expression that will match a pattern in one of two ways: by following the underscore with either
a command name (such as x_Integer), or with a question mark followed by a predicate command
(such as x_?NonNegative). When defining your own commands this knowledge will get you a long
way, as patterns like these are very common on the left side of definitions. But patterns have many
other uses, and there are countless cases where more sophisticated pattern objects are needed. The
next order of business will be to explain how such objects are constructed.
8.8 Patterns 451
We will do this by introducing the Cases command. Like Select, this command is used to extract
items from a list. Unlike Select (which applies a predicate command to the list items and returns
those for which the predicate is True), Cases returns those items from the list that match a pattern.
For instance:
Out[50]= 0, 2, 4, 6
For the most basic use of Cases, the first argument is the list, and the second is a pattern object. It is
the multitude of possible variations for this second argument we wish to address. Here are some
illustrative examples:
Clear$x, g, a, b, c(;
Cases$x s. NSolve$3x4 x3 5x2 7x 1, x(, BReal(
In[51]:=
Out[54]= a2 , a3 , aS , a4
Out[55]= aS , a4
Out[56]= a2 , a3 , a4
Out[58]= g#a'
The point here is that a pattern object can be any ordinary expression, but typically it will contain
one or more of the various underscores.
Pattern objects can also make use of a number of special commands. For instance, Except[pattern] is
a pattern object that will match any expression except those that match pattern. It is useful in cases
when it is more convenient to say what something isn’t rather than what it is. The input below is a
simplified example of a list where some members have the form Missing["Not Available"]. For
instance, many of the curated data commands such as CountryData will use this symbol when
there is missing data. Cases and Except can be used to extract those data values that are not missing.
Out[61]= 1, 2
The Repeated (..) command is useful for matching repeating sequences of objects. In the first input
below we find all cases of a list comprised of the same expression a repeated multiple times. In the
next input we find all cases of a list comprised only of integers.
Out[62]=
Other such pattern commands include Longest, Shortest, Condition, and PatternSequence.
Most of the pattern objects used in the Cases examples above were not named. Another setting that
often makes use of pattern objects is that of making replacements, and this enterprise generally
requires that patterns be named. Here are two simple examples. In the first, no patterns are used. In
the second, a simple named pattern is used to make the replacements:
In[64]:= 1 x x2 x3 s. x x2
Out[64]= 1 x2 x4 x6
In[65]:= 1 x x2 x3 s. aBInteger a 1
Out[65]= 2 x x3 x4
In the second example every integer in the expression is increased by 1. It is important to make clear
that the x in the expression does not get transformed to x2 under this replacement (even though x is
mathematically equivalent to x1 ). Rather, patterns are matched to the underlying FullForm of the
expression in question.
Now imagine that in the last example you wish to increase by 1 only the exponents (not the 1 at the
far left). The pattern object Power[x, n_] will match the exponents, or equivalently xnB . Note that
you need to name the pattern (in this case n) in order to refer to it on the right side of the rule.
8.8 Patterns 453
Out[66]= 1 x x3 x4
¿ If you want to increase the exponents on all of the x’s, the simplest means of doing so is with
the pattern object Power[x, n_.]. Note the dot (a simple period) after the underscore. The n_.
represents an optional argument to a function, and it will assume a default value if it is
omitted. For the Power command, the default value is 1. In other words, MatchQ[x, Power[x,
n_.]] will return True. So the rule Power[x, n_.] Power[x, n+1] will do the trick.
In the example below, a simple replacement rule is used to turn an integer into a row of a table:
In[67]:= Grid$Range#10' s. nBInteger Defer#n ', " ", n , Alignment Right( ss
TraditionalForm
Out[67]//TraditionalForm=
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40 320
9 362 880
10 3 628 800
Here’s yet another example of named patterns being used in the context of making replacements.
We begin with a table, where on any row you will find two mathematically equivalent trigonometric
expressions.
Looking carefully at the expanded expressions in the right column, we observe that the sine func-
the expressions on the right: use the fact that sin2 +a/ 1 cos2 +a/. Or raising each side of this
tion only occurs with an even exponent. This means we can easily eliminate all sine functions from
identity to an arbitrary integer power n, we have sin2n +a/ ,1 cos2 +a/0 . Here is how one could
n
Finally, we Expand this to get a nice expression for cos+7a/ as a polynomial in cos+a/:
In[71]:= Expand#'
In[72]:= Grid%
Table%Cos#k a',
Exercise 6 asks you to use this table to prove that cos+S s 21/ is a root of the polynomial
f +x/ 1 16x 32x2 48x3 96x4 32x5 64x6 .
The Cases command discussed earlier in this section can also make replacements. That is, one can
find all cases within a list (or indeed any expression) of subexpressions that match a particular
pattern, and replace each of these by something else. It sounds a bit far fetched, but it’s actually
incredibly powerful and useful. There was an example in Section 7.9, for instance, where we
extracted all Line objects from a graphic and replaced each with the underlying list of points. Let’s
recreate an example like that one:
The first argument to Cases here is not a list, but rather a Graphics that was produced with the
Drawing Tools palette. Note the third argument to Cases is Infinity. Cases goes into the FullForm of
the Graphics and searches at every level (since the third argument is Infinity) for subexpressions
matching the pattern object Line[pts_]. Each matching expression is replaced by pts, and a list of all
456 Programming
such matching expressions is returned. The result in this case is three lists of points (or more pre-
cisely, a list of three lists of points).
When making replacements, it is often desirable to assign a name to an entire pattern object. The
Pattern command is used for this purpose. The infix form of this command is the colon (:). An
expression of the form name:pattern is used to associate name with pattern. In the (simple but common)
setting where the pattern object is a simple underscore, the colon can be eliminated altogether. That
is the expression x_ (that is so commonly seen) is equivalent to x:_. The colon is essential when
naming a more intricate pattern object. Consider, for instance, the example below in which every
row that begins with 1 in a matrix gets replaced with that same row multiplied by 3:
1 3 5 7
The name a is associated with the pattern object {1, __}. So 3a represents the scalar 3 times this list,
which has the effect of multiplying every member of the list by 3.
Note that when you make a replacement via ReplaceAll (/.), the very first item in the evaluation
sequence will be the right side of the Rule. That is, when you enter the cell containing a replace-
ment, the right side of the Rule is evaluated first. In the example below (which is just like the
previous example, except here we Reverse each row in the matrix that begins with 1) this is problem-
atic. The input Reverse[a] generates an error message (because a is a Symbol, not a List). The
output, however, is correct.
1 3 5 7
Out[75]//MatrixForm=
7 5 3 1
7 9 1 8
4 3 2 1
2 5 9 1
In a case such as this, it is better to delay evaluation of the right side of the Rule until after the
replacements have been made. Then Reverse will be applied only to an actual list, and all is fine.
8.8 Patterns 457
1 3 5 7
Another example may help to clarify the distinction between Rule and RuleDelayed. In the first
input below, the right side of Rule is evaluated prior to making the replacements. Hence every
replacement receives the same random integer. In the second input, the right side of RuleDelayed is
not evaluated until after the replacements have been made. Hence RandomInteger[100] is evalu-
ated three times.
¿ In order to understand the evaluation sequence upon entering a particular expression, wrap
the expression with Trace. The result will be a list of every expression that is encountered
during the evaluation process, with the final item being the output. In the case of an expres-
sion with head ReplaceAll whose second argument is a Rule, the right side of the Rule will be
the first thing evaluated.
The final pattern command that we will introduce is called Optional. This allows you to build a
command with an optional argument. Optional accepts a pattern object as its first argument, and
the default value to be used if that pattern is omitted as its second argument. For instance, this
command will draw a random sample from the list x. If a second argument is given, that will be the
size of the sample. If no second argument is given, a random sample of size three will be generated.
In[79]:= Clear#f';
randomSample$xBList, Optional$yB, 3(( : RandomChoice$x, y(
In[81]:= randomSample$Range#100', 5(
In[82]:= randomSample$Range#100'(
¿ The infix form of Optional is a colon (:). There are two distinct commands whose infix form is
given by a colon (:). For an expression matching the form symbol:pattern, the meaning is
Pattern[symbol, pattern]. On the other hand, for an expression matching the form
pattern:expression, the meaning is Optional[pattern, expression]. So the left side of the definition
above could have been entered as randomSample[x_List, y_:3]. This can be confusing to
someone trying to learn about patterns, but it never leads to syntactic ambiguity, for the first
argument to Pattern must be symbol, while the first argument to Optional should be a
pattern object. Mercifully, this dual use of a single symbol is exceedingly rare (another
example is !, which is used for both Factorial and the logical negation command Not).
Exercises 8.8
1. Define a function f with a single argument. The function will return unevaluated unless
a. the argument is an even integer greater than 10. In this case the function returns the string
"success".
b. the argument is either an even integer, or is greater than 10. In this case the function returns
the string "success".
2. Explain the following output. Doesn’t x_1 represent a pattern that will only match the number 1?
In[83]:= Clear#f';
f#xB1' : "success";
f s 1, 2, 2., "donkey"
3. Find a word that contains the five letters “angle,” (contiguous, and in that order) and which
begins with the letter “q” and ends with the letter “s.”
4. A DNA molecule is comprised of two complementary strands twisted into a double helix, where
each strand may be represented as an ordered sequence of the letters A, C, G, and T. The comple-
mentary strand is built from a given strand by replacing every A by T, every T by A, every G by
C, and every C by G. In other words, A is swapped with T, and C is swapped with G. Define a
command complementaryDNA that will take a list of character strings from the four-letter
alphabet "A","C","G", and "T" (which is how we will represent a strand of DNA) and return the
complementary strand, in which all As and Ts are switched, and in which all Gs and Cs are
switched.
5. This exercise concerns the Collatz conjecture, which was discussed in this section.
a. Write a command collatz, that when given a positive integer will return the orbit of that
integer under the iterated Collatz process. The conjecture states that every orbit ends at 1, so
8.8 Patterns 459
use NestWhileList with iterations occurring as long as the iterated function does not return 1.
To be safe, put a cap on it so that it will never carry out more than 1000 iterations.
b. Run the collatz command on each of the first 20 integers, and make a Table of the results.
Map the command Length over this table to see how many iterations were carried out for
each input. Make a ListPlot of the results. Did any number require all 1000 possible itera-
tions? If not, we can be confident that every orbit ends in 1.
c. Map the Partition command over your data table to replace an orbit such as {5,16,8,4,2,1}
with a list of pairs of successive numbers, like this: {{5,16},{16,8},{8,4},{4,2},{2,1}}.
d. Flatten the result at level 1 to produce a single list of pairs, then feed that list of pairs to the
Union command (to eliminate duplicate pairs). The list should end like this:
{…{88,44},{106,53},{160,80}}.
e. Use Map to Apply the command Rule to each pair from part c to obtain an amalgamated list
of all orbits. It should end like this: {…, 88 44, 106 53, 160 80}. Now feed the result to the
command GraphPlot to get a visualization of the orbit space for the Collatz process.
f. Repeat the entire exercise for the first 100 integers (rather than just the first twenty). Do it yet
again for the first thousand.
6. Use the trigonometric example from page 454 to prove that cos+S s 21/ is a root of the polynomial
f +x/ 1 16x 32x2 48x3 96x4 32x5 64x6 . You may wish to take a look at the example
from Section 4.6 on page 180.
7. Make a replacement to Range[15] and wrap the result in TabView to produce the output shown
below.
362 880
8. Make a command scaleRuns that will take a list 3 of zeros and ones, and return a list in which
every run of k consecutive ones in 3 is replaced with k consecutive ks. For instance, the input
{1,1,0,1,1,1,0,0} should produce the output {2,2,0,3,3,3,0,0}. You may want to make use of the
Split command and the Repeated command.
9. Use the scaleRuns command of the previous exercise to build a command that will take a list of
zeros and ones and display it using an ArrayPlot with a single row (with one item in the array
for each member of the list), and where each consecutive run of ones is shaded according to the
length of the run. Use Partition to modify this command so that it will break a long sequence
(say with more than 50 elements) into several rows.
10. Make a command differenceTable that will accept two arguments. The first is a list. The second
is an optional argument (with default value 3) that specifies the ItemSize for each item in a
Grid. The output will be a difference table display like the one appearing at the end of Section
8.7 on page 441. You can model the command on the input for the example given there.
Index
f
, 414 e ,5
, 36 f e , 203
f,f e , 281
, 45
à e Šf , 227
f
B, 52 f
`, 44 Ã e Å f , 222
Å e , 230
;, 40 f
?, 41 f f
s., 153
m , 150
ef , 5
aa, 85
BB, 448
!!, 42
, 422
aa, 450
««, 96
: , 52
## '', 153
;;,127, 344
s, 403
??, 127
BBB, 449
Expand#e' , 148 ss., 288
:!, 457
Factor#e' , 172
Ó, 404
^, 2
Simplify#e' , 180
, 2
B, 444
TrigReduce#e' , 179
, 2
, 13 , 20
s, 2
?, 445
, 232
:, 456, 458
e3f7 , 153
462 Index
Â, 159
, 457 scaling, 60
Á, 159
Axes, 64, 260
AxesEdge, 260
AxesLabel, 68
AxesOrigin, 61
Abort Evaluation , 38
AxesStyle, 64
AccountingForm, 398
Arrowheads, 65
Accumulate, 440
Axis, 106
Accuracy, 401
Add Column , 120, 340 Background, 64
Add Row , 120, 340 Band, 356
AdEPn, 43 Basel problem, 444
addition, 2 basis, 365
adjoint, 352 orthogonal, 367
Alignment, 87 orthonormal, 367
All, 128, 343 beep, 47
Ambient, 263 BlankNullSequence, 449
And, 96, 388, 420 BlankSequence, 448
Animate, 83 Blend, 63
Animator, 83 Block, 433
antiderivative, 222 Blue, 63
Apart, 175 Boxed, 260
Appearance, 398 BoxRatios, 260
Appearance, 126, 283 brackets, 89
Apply, 409, 420 cell, 3
approximate number, 7 curly, 19
ArcCos, 12 round, 8
ArcCot, 12 square, 10, 25
ArcCsc, 12 Butt, 46
ArcSec, 12
ArcSin, 12 C#1', 158
ArcTan, 12 Caesar cipher, 413
arguments, 10 Cancel, 172
Array, 338 Cardano, 157
ArrayFlatten, 340 Cartesian, 328
ArrayRules, 356 Cartesian coordinates, 314, 318
Arrow, 253 Cases, 141, 451
AspectRatio, 60 cell
assignment, 20 bracket, 3
Index 463
radian, 12 root
RadioButtonBar, 83 cube, 5
radius of curvature, 308 nth, 5
Rainbow, 442 principal, 167
RandomComplex, 130 square, 17
RandomInteger, 130, 337 Root, 155
RandomReal, 130 roots, 147
Range, 67 approximate, 148
rank, 366 complex, 152
Raster, 115 exact, 154
rate of change irrational, 149
average, 200 RotateLeft, 413
instantaneous, 201 RotateRight, 414
Rational, 393 RotationMatrix, 380
rational functions, 171 RowReduce, 346
Rationalize, 443 RSolve, 189
Re, 18 Rule, 449
Real, 392 RuleDelayed, 457
realPower, 58 Running..., 38, 47
Reals, 163
Rectangle, 115 Save, 23
recurrence relation, 142, 189 ScaleFactor, 329
RecursionLimit, 144 scatter plot, 122
Red, 63 scientific notation, 11, 394
red caret, 24 ScientificForm, 398
Reduce, 157 Sec, 12
reduced row echelon form, 346 secant method, 415
reflection, 378 Second, 45
region of convergence, 247 second derivative, 206
RegionFunction, 263 Section, 28
RegionPlot, 295 Select, 141, 418
RegionPlot3D, 295 selection placeholder, 5
Remove, 46 sequence, 246
Repeated, 452, 459 Series, 247
ReplaceAll, 153, 288, 388, 452 Set, 52
replacement rule, 150, 153, 256 SetDelayed, 52
ReplaceRepeated, 288, 388 SetDirectory, 138
Rescale, 298 SetterBar, 79
residuals, 125 Shapes, 121
resize a graphic, 55
Short, 41
Rest, 141
Shortest, 452
Reverse, 408
Show, 107
reversFBEd problem, 434
reversFDMose, 4 Show Toolbar , 27
RevolutionPlot3D, 242, 258, 319 ShowExpression, 392
RGBColor, 74 Sign, 255
riddle, 85 significant digits, 11, 394
Riemann sum, 230 Simplify, 157, 172, 176
Riffle, 426 Simpson's rule, 231
Right, 70 Sin, 12
slider, 16, 76
Slider, 430
Slider2D, 80
470 Index