|
This document lists some very rudimentary information about using Mathematica in introductory calculus. It is not a general introduction to Mathematica. In particular, most of the commands discussed below have a number of optional arguments that allow a user to fine-tune their effects; these optional arguments are not discussed below.
Mathematica has gone through a number of versions over the years, and it is possible that some of the information here is either incorrect or irrelevant for the current version, which is 10. I am not aware of anything important here that would be invalidated by the version changes, but there may be something. There may be places where I refer to a Mathematica notebook with a suffix of ".ma" or ".m"; in recent versions of Mathematica, the appropriate suffix is ".nb".
Index
Here is a link to another document listing a number of common problems that often mystify inexperienced users of Mathematica.
General Syntax
Remember That Built-In Mathematica Functions Are One Word Each And Begin With Capital Letters. (e.g., the natural logarithm of 6 is Log6])
If The Command Name Is BuiltUp of MultipleWords, Usually TheFirstLetter Of EachPart Is Capitalized (e.g., ArcSin[])
Using a Notebook interface (Mac or Windows), use the enter key to evaluate commands (not the Return key). (This is so that a whole series of commands may be entered, one per line, and then get evaluated together.) Shift-Return can be used in place of Enter, and on a Macintosh, cmd-Return can be used in place of Enter.
Commands are either listed on separate lines, or several to a line, separated by semicolons. (A single command may extend over more than 1 line; if it does it is a good idea to use parentheses to ensure that Mathematica reads it as intended.)
If a command is followed by a semicolon, the result of the command is not displayed.
Many Mathematica commands (for graphing, integration, looping, ... ) require what is called an "iterator", which tells the command to do something with a certain variable over a certain range. All of these iterators take the form
{ whichVar, startingValue, endingValue}
where "whichVar" is the name of the variable to be used, and "startingValue", "endingValue" are the lower and upper bounds of the range of "whichVar" to be considered. For example
Plot[Sin[aPumpkin], {aPumpkin, 0, 3 Pi}]
Plots the sine function over the range of inputs from 0 to 3 Pi:
Integrate[x^2 + 2, {x, -3, 6}]
computes the integral of x^2 + 2 over the range -3 < x < 6.
Basic arithmetic
Pi is the constant pi = 3.14159. . .
E is the constant e = 2.713. . .
Space between quantities is interpreted as multiplication
^ stands for exponentiation; 3^2 = 3*3 = 9
N[] the the Mathematica command to Numerically evaluate its argument; N[Pi] returns 3.14159 (it is possible to adjust the number of digits returned; N[Pi,7] returns 3.141593 (6 decimal places for 7 digits)).
Brackets
Functional arguments are enclosed in [square brackets]
Lists are enclosed in {curly, braces}
Parentheses are used (for grouping).
The n-th element of a list is referred to using double square brackets. For instance if we have a list of small prime numbers, smallPrimes = {2, 3, 5, 7, 11}, then smallPrimes[[4]] would be 7.
Referring to previous lines
Mathematica numbers each input as "In[n]" and the corresponding output as "Out[n]", where "n" is a counter. You can refer to these directly, as in Simplify[Out[32]]. A synonym is just a percent sign followed directly by the number of the Out-line, for instance %32. You can also use % to refer to the last "Out[]" line, as in Simplify[%].
Deleting Cells
At least on a Macintosh, the front end will not let you delete a cell, but you can select a cell and "Cut" it; if you never paste it back, then this has the same cosmetic effect as deleting it. WARNING: If you do this, Mathematica still remembers what was in the cell that you deleted.
Equals Signs
In everyday Mathematical usage is is common to have the equals sign to indicate several related but slightly distinct concepts; as your mathematical sophistication grows it becomes more important to be aware of these subtle distinctions. This is especially true when dealing with a computer package such as Mathematica, because the package is unable to "infer from context" exactly how to interpret the equals sign; instead it must be told explicitly. Thus in Mathematica there are several different variations on the simple equals sign. Here is a list of some of them.
Immediate assignment of values
Simple "=" is used for immediate assignment of values; entering "x=2" means that "x" will be interpreted as having the value of 2 from now on. (The Mathematica jargon for this assignment of values is the "Set" command.)
Delayed assignment of values
The expression ":=" is used for delayed assignment, where the rule on the right side is associated with the left side, as opposed to the current value of the right side being associated with the left. For instance the commands
give z the value 3 (changing x after y was defined has no effect on y). On the other hand, the commands
- x=1
- y = 3*x
- x=2
- z=y
give z the value 6. (In Mathematica, ":=" is called the " SetDelayed " command.)
- x=1
- y := 3*x
- x=2
- z=y
Checking for equality
The expression "==" is used to check equality of the terms on the left and the right; when evaluated, it returns "True" or "False" . "==" is called the "Equal" command. For instance " 1+1==2" returns True. (There is a stronger version of "==", namely "===", which will not return True if there is any doubt whatsoever that the two terms evaluate to exactly the same thing. For instance "1 === 1.0" returns False, because the left side is the integer 1 and the right side is the floating point number 1.0; it is possible that the 1.0 had been obtained through a calculation involving some rounding errors and so it should not be viewed as being identical to the whole number 1.)
Solving equations
The "==" symbol is also used in setting up equations to be solved through the Solve command. For example,
Solve[x^2 + x == 2, x] returns { {x -> -2}, {x -> 1}}, which is the list of values of x that give the same values to both of the equation. (In the Solve command, the x after the comma is telling mathematica that we want to solve for values of x.)Assignment rules
The expression "->" is used to assign the value on the right to the expression on the left. For instance, in the last paragraph, the Solve command returned a list of such assignments; basically it is saying that if you insist that x^2 + x be equal to 2, then you are (implicitly) insisting that x be either -2 or 1. These assignments (or Rule's) are put into effect with the "ReplaceAll" command which is abbreviated as "/."; for example
(a^2 x^3 - 3 a x y)/.{a->2}
returns 4 x^3 - 6 x y
Differentiation
The basic Mathematica differentiation command is D[f,x], where f stands for the function to be differentiated, and x is the variable of differentiation; i.e. D[f,x] is Mathematica-ese for df/dx.
Higher derivatives can be found either by nesting the D commands by hand (D[D[f,x],x]) or in the form D[f,{x,2}].
Integration
The basic Mathematica integration commands are Integrate[f,x] (for the indefinite integral or antiderivative of f with respect to x), and Integrate[f,{x,a,b}] (for the definite integral of f with respect to x for a< x < b).
Note that the second version (for definite integrals) relies on the first to work; it finds the antiderivative, and evaluates it at b and at a, and then subtracts. If Mathematica can't find an explicit antiderivative, it returns the input unchanged. For definite integrals, you can get a numerical approximation of the value of the integral by using the command NIntegrate[f,{x,a,b}]
Basic Algebra
Together Combines a sum involving fractional terms into a single fraction by finding a common denominator.
Apart is the opposite of Together; it splits a fraction into a sum of fractions with denominators as simple as possible. (It is essentially the process of "Partial fractions".)
Simplify attempts to find the simplest formula that expresses a given quantity.
Expand Multiplies out all factors (Expand[(x+1)^2] returns "x^2 + 2 x + 1").
Factor tries factor a polynomial over the integers; it acts like an inverse operation to Expand (Factor[x^2 + 2 x + 1] returns "(x+1)^2").v
Equation Solving
Solve[leftSide == rightSide, whichVariable] tells Mathematica to try to solve the equation "leftSide=rightSide" for the variable "whichVariable".
Note the double equals sign "==" in the command.
For example, Solve[x+y + 2 == 2x - y + 4, x] returns "x-> -2(1 - y)", while Solve[x+y + 2 == 2x - y + 4, y] returns "y-> (2+x)/2". If you have several simultaneous equations, enter them as a list (i.e. separated by commas, all enclosed in a single set of {braces}); you may also ask for a list of variables to be solved for.Note that the Solve command is attempting to find exact algebraic solutions, so it is likely to fail if the equation to be solved is difficult. You can ask for a numerical solution with NSolve, but it is essentially the same as applying N to the output of Solve, so it will fail to work on the same equations as Solve. For tough equations, use FindRoot[left==right,{whichVar, startHere}] , where the equation is entered as in Solve, and the second argument is a list consisting of the name of the variable to be solved for, and an initial approximation "startHere" to the root you want to find numerically. For example, FindRoot[t^3 - 2 t + .2 == 0, {t, 1}] returns "t-> 1.36128".
Graphics
The basic graphics commands are Plot and Show. For example,
Plot[Sin[2 x], {x, -6, 3 PI}]
plots the part of the graph of the function "y = sin(2x)", with x on the horizontal axis, and showing the part with x between -6 and 2 pi. More about these commands can be found in the introductory Mathematica notebook by K. Stroyan entitled ".aMathcaIntro.ma". You may plot several functions at once by including them as a list (Plot[{f,g}, {x, -2, 3}]. You can plot in polar coordinates with
PolarPlot[r, {theta, thetaLow, thetaHigh}].
There is also ParametricPlot[{xcoord, ycoord},{t,tLow,tHigh}] for plotting a curve whose x and y coordinates are each given in terms of some other parameter t.You can save a graph by assigning it a name:
monaLisa = Plot[Sin[2 x], {x, -6, 3 PI}]
You can later use the Show command to display the saved graphic:
Show[monaLisa]
You can give a list of graphics to the Show command to have them all displayed in one picture.
Some of the more advanced graphics options are described below. Return to the index.
Graphics Options
A number of options are available for the Show, Plot, and Parametric Plot commands. The syntax for these is of the form
OptionName -> OptionValue
and different options are separated by commas. (For some Options there can be a {List} of OptionValues). Some examples are
Return to the index.
PlotRange->{bottom, top} which specifies the upper and lower cut-off's in a graph
PlotRange-> { { left, right},{bottom, top}}(note the list of lists) which specifies all four cut-offs in a graph; this is mostly used for Parametric Plot's.
PlotStyle->style or list of styles can be used to change color, thickness, dashing of plots; for example
PlotStyle-> {RGBColor[1,0,0], Thickness[.05]} will cause the graph to be drawn in red with a thickness of 5% of the thickness of the total picture.Axes -> False will cause no axes to be graphed.
AxesLabel -> output puts the label "output" on the vertical axis; if you give a list of labels then all axes can be labelled, e.g. AxesLabel -> {input, output}
Ticks ->None will cause no tick marks or labels to be printed on the axes.
DisplayFunction-> Identity will cause the graphic to be created but not displayed (this can be useful if you want to separately create individual graphs that will later be combined in a single picture).
DisplayFunction-> $DisplayFunction causes the graphic to be displayed on the default device for displays (normally the screen). This is used to display graphics that were created with display turned off (see the previous item).
Lists
In Mathematica, a List is enclosed in {braces} and the items in the list are separated by commas. An item can be another list. For example
a = {6, 5, 2 Pi, x - y^2, {4,3,2} }
is a list of 5 items, the first 3 of which are numbers, the fourth is a formula, and the fifth is a sublist of 3 numbers; this list has been given the name "a". Mathematica has a number of commands for manipulating lists (and matices, which are treated as lists of lists of the same size), but we won't get into them here.
Programming
1. Using Loops
The basic command for writing a loop is the "Table" command, for example
Table[Plot[Sin[x] + n x, { {x, 0 , 2 Pi}], {n, 0, 3}],
will cause 4 graphs to be created, each of the form y = Sin[x} + n*x for x between 0 and 2 Pi; the 4 graphs correspond to 4 different values of n, namely 0, 1, 2, 3.
2. Using Packages
In Mathematica, a Package is a Mathematica text file that contains definitions of commands other than the ones that are built into the Mathematica application itself. In fact, Mathematica comes with a number of Packages; some of these are loaded when Mathematica starts, others are not loaded, but they are in directories that Mathematica will search to find them if they are requested. The Packages that come with Mathematica can be found on the CWRU software library.
Packages that do not come with Mathematica are a bit more trouble to work with, because they may not be located in a directory that Mathematica will automatically search through. Here is a link to further information.
This site is maintained by Mike Hurley; this page was last updated on 5/31/19, 12:57 PM .
.. |