Here are the commands we executed on Tuesday, Oct. 26 1999, in the computer lab in BH 406. I have edited and re-organized a bit of it, for clarity, and added a few things here an there. This is not intended as a tutorial, but just as a set of examples that give you the ideas.
Please send me your suggestions for further examples at any time.
Note: to execute a command, you must hit the Shift-Enter key combination, not just the Enter key. This is to allow commands to have multiple lines at the user's discretion. The input is always in an Input Cell, marked by a Cell Label of the form In[ ]:=.
You can execute multiple cells by highlighting the cell brackets and then hitting Shift-Enter. You can execute all the cells in a notebook by using the menu command
Kernel/Evaluation/Evaluate Notebook, or by highlighting all the cells using ctrl-A, then hitting Shift-Enter.
Incidentally, you can get quite fancy with formatting Mathematica Notebooks, but we won't get into that here.
Here is a basic plot of a basic function.
The default aspect ratio for a plot is , where is the golden ratio. By the way, Matt knows the golden ratio:
We can get any aspect ratio we want. E.g.,
We can insist upon a true aspect ratio, but the results aren't always nice.
In the above case, we have a true aspect ratio but aren't shown the entire plot. We can insist on that too.
But that has other undesirable effects. (Good thing we didn't try that with in place of .) We can look at a portion of the plot. PlotRange
can be used to set the range of -values, as in the next two examples...
... or you can set the horizontal and vertical ranges independently. Notice that the PlotRange
is independent of the actual range of values plotted!
We can plot many functions simultaneously. Here is the function along with the function , which we calculated as the tangent line to the first function when .
It looks okay, but the slope doesn't look like 3. Let's go with the natural aspect ratio again.
And oh yes, we can use different colors.
There are a many Options
for the Plot
command. We cannot look at all of them now, but just to give you an idea, ...
There are many kinds of plotting that Matt knows. Some are built in, others can be loaded from "Standard Packages" which are included. Here is the bifolium. The first command loads the ImplicitPlot package.
Needs["Graphics`ImplicitPlot`"]
ImplicitPlot[(x^2+y^2)^2==4x^2 y, {x, -2, 2}]
Later, in Calc II, you'll plot stuff in two dimensions. (And did I mention derivatives in two dimensions?)
Ugh. I have always found that the default number of points used for ploting is far too few in Matt's two-dimensional routine.
So let's increase it to 50 (in each direction).
We can change the ViewPoint
. (For this, the menu command Input/3d ViewPoint Selector can be used.)
This was not discussed in the live demo, but I'll mention it anyway, to give you a glimpse of what might be part of your mathematical future. Another way of representing two-dimensional data is the DensityPlot
.
Here's another, called ContourPlot
.
You can mess with colors, etc.
While we are at it, here's another way to view the bifolium. I'm giving it a name for future use.
Hey, I don't see any bifolium. Try again, but notice that the bifolium is the solution to . So I'll plot the previous along with a plot of zero.
Now look at them together. See it?
That's just for fun. Here's a similar view using ContourPlot
, but showing three contours. (Looks like we found a "bug" in the program.)
That bird looks hungry.
First of all, recall that the underscore_ is used to denote the dummy variable in a function's definition. More about that later.
Here is the function you all gave me.
Here's the derivative. It's pretty easy. Notice, however, that it isn't necessarily expressed the way you might want.
The Simplify
command can sometimes get you something nicer, but not always. Notice the use of %
, which refers to the previous Output.
Second (and third and fourth, etc.) derivatives are just as easy to enter.
(I don't want to play with that function anymore. )
Speaking of functions, let's mention now the use of delimiters by Mathematica. It is very strict and very consistent: use brackets [ ] as you would to delimit functions. So, f[x]
instead of . All Mathematica commands, such as Plot
, Simplify
, etc., are functions. There are other symbols in Mathematica that aren't functions, e.g., numerical constants (GoldenRatio
, Pi
, E
), options to functions (PlotRange
, AspectRatio
, etc.), and some other things, but commands are functions. The next set of delimiters is the braces {}, which delimit lists. Lists are integral to Mathematica and there are many, many list processing commands. No time for that now, but think of the lists as you might finite sets, vectors, etc. Finally, there are the parentheses (), which are used exclusively for grouping.
About the underscore on variables when defining functions. If you don't use one, Matt assumes you literally mean what you say. Suppose you want to define the function .
Now you want to compute , which should be . But instead, we get
as the value of r[1]
is unknown (except as r[1]).
What we really did was tell Matt that the function should return in the event that it receives the Symbol
s
. This is certainly useful for applying functions to various sets. But if we want to tell Matt that where is a dummy variable, then we use the underscore. Let's actually define
But notice that it still remembers the original definition.
So, to be safe, Clear
the old values first.
The following is probably self-explanatory, at least after you look at the graph below.
Unfortunately, Matt won't attempt the derivative of such a function.
Sin^2[x]
instead of Sin[x]^2
, as I did by mistake. (Open up this cell for more trivia.)
Matt knows limits.
Of course, is undefined.:
We can plot this and probably won't get yelled at, even though is undefined at .
Okay, here's the limit. (Of course,we know how to compute this one with ease.)
The graph certainly agrees with the limit.
We can also look at one-sided limits. Here are two limits, one as and one where .
These might appear to be backwards,but think about it this way: from zero, the Direction
toward 1
is from the left, while the Direction
toward -1
is from the right.
Wow, the following is disappointing:
It seems that the default direction is from the right. (See section 3.6.8, Finding Limits, in the Help.) We'll have to handle this ourselves.
If you must, you could do things your own way.
But suppose we need to now where , for the function above. There is a function called Solve
that is extremely useful. For instance, it can solve polynomial equations in a heartbeat. We'll look at that in a moment. We try it on .
Solve
tells Matt to try to find an exact solution, but Matt cannot do so for this equation. (Neither can anyone else.) Luckily, we can find numerical solutions. (One of the basic tools is Newton's method, but that is all internal.) We'll tell it to start looking near .
So the actual point of intersection is roughly
Here's a graph, for fun, showing the point.
Let's look at another function, more like one we've seen in class.
We like to set derivatives equal to zero. Here is a summary of what we do by hand.
But we are able to cut out the intermediate steps and go right for the throat.
Let's note that later on we'll also be interested in the places that the second derivative is zero. No problem.
We like to find extremes. Here's a canned function that does part of that. It finds a relative minimum, if you steer it in the right direction.
There is no bult-in FindMaximum
, but we can create one. I won't explain how this works, nor will I warranty its use.
Seems to work:
As we said, Solve
can find exact solutions. Here's a lowly quadratic.
You can verify that using the quadratic formula. Oh, yeah, here's Matt's version of the quadratic formula:
Matt knows cubics.
It is hard to decipher this one, but note the imaginary number ⅈ
, which denotes . I named the output s3
so we could mess with it below.
Usually, the function N
is used for basic numerical evaluations. It turns exact values into decimals.
(We pause for a moment to observe that the complex roots of this polynomial equation occur in complex conjugate pairs, since the poly in question has real coefficients.)
Now, while it might not seem necessary very often, occasionally one wants lots of precision. You want 200 digits? No problem.
Take another look at the numerical solution to .
How do we actually use these in other situations, without actually copying the values? The construct x->-3.28198 is an example of a replacement rule, and here are some examples of how it works.
So, for example, in our earlier example, we can obtain a list of the values (without replacement rules) for easier manipulation as follows.
We can now operate on this list of values. We can add them:
Say, that's a pretty simple result. We might recall that there is a basic result concerning the sum of the roots of a polynomial equation. Check this out.
Aha. And we recall a similar fact about the product of the roots.
And now we might wonder if there is a similar result that produces , and of course there is, but I'll tell you later if you want.
Since polynomials are so basic and important, we mention that polynomial equations of degrees up to 4 can always be solved exactly (even by humans). For instance,
That's hideous.
So what about degree 5 polys (and higher)? In general, the quintic has no solution by radicals. This is a famous classical result that we cannot go into here, but it won't slow us down too much. In fact, Mathematica's "solution" of a quintic is easier to read than that of a quartic (4th degree) equation, because it cannot explicitly tell us the exact solutions.
Still, we can use these as before.
Finally, we note that there is a single function that combines N
and Solve
. It is called (surprise) NSolve
, and is essentially N
applied to Solve
.
Mathematica is a very powerful numerical computing platform, but it is also a sophisticated list processing machine. We can only scratch the surface here with some examples.
If you already have a list and you need to play with it, there are many functions for this.
Random numbers are useful in modeling problems. Matt has different types of (pseudo)random numbers, some simple varieties being as follows.
Let's compute the average of the previous result, which we have named i100
. First, an averaging function:
To give you an idea of the number of Mathematica commands, here are the ones beginning with A
. (Actually, not all of these are commands; some are options for commands. But still...)
Abort Append
AbortProtect AppendTo
Above Apply
Abs ArcCos
AbsoluteDashing ArcCosh
AbsoluteOptions ArcCot
AbsolutePointSize ArcCoth
AbsoluteThickness ArcCsc
AbsoluteTime ArcCsch
AccountingForm ArcSec
Accuracy ArcSech
AccuracyGoal ArcSin
Active ArcSinh
ActiveItem ArcTan
Adams ArcTanh
AddOnHelpPath Arg
AddTo ArgumentCountQ
AdjustmentBox ArithmeticGeometricMean
AdjustmentBoxOptions Array
After AspectRatio
AiryAi AspectRatioFixed
AiryAiPrime Assumptions
AiryBi AtomQ
AiryBiPrime Attributes
AlgebraicRules AutoDelete
AlgebraicRulesData AutoEvaluateEvents
Algebraics AutoGeneratedPackage
Alias AutoIndent
AlignmentMarker AutoIndentSpacings
All AutoItalicWords
AllowInlineCells AutoloadPath
Alternatives Automatic
AmbientLight AutoOpenPalettes
Analytic AutoScroll
AnchoredSearch AutoSpacing
And AutoStyleOptions
AnimationCycleOffset Axes
AnimationCycleRepetitions AxesEdge
AnimationDirection AxesLabel
AnimationDisplayTime AxesOrigin
Apart AxesStyle
ApartSquareFree Axis
AppellF1
We mentioned in class that some texts define inverse cotangent function so that its range is . That's just what Matt does!! From the Help:
• For real , the results are always in the range -π/2 to π/2, excluding 0.
(They should mention also, "excluding .")
Another bothersome item: principal roots! This actually makes other things simple, but it is extremely annoying. The (real) cube root of is of course . But look at this:
Not ? Here's its numerical value:
What the hey? Well, if you recall DeMoivre's theorem, you know that every nonzero complex number has distinct 'th roots. If is a complex number, then these are given by . The principal n'th root is the one obtained by letting . Here, θ is the angle in the complex plane, in standard position, terminating at . In any case, the principal root is the one used by Mathematica.
This can be a pain.
I don't like getting yelled at by a computer. If this is too much to bear, there is a package called RealOnly
that can be loaded so that these troubles are swept under the rug.
Just remember that is Log[x]
. You can change that.