case western reserve university  
Mike Hurley's pages Sun. May 05 2024
 

Mathematica cribsheet


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

Return to the index.


Basic arithmetic

Return to the index.


Brackets

Return to the index.


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[%].

Return to the index.


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.

Return to the index.


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.

Return to the index.

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

x=1
y = 3*x
x=2
z=y
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.)

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

Return to the index.


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}].

Return to the index.


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}]

Return to the index.


Basic Algebra

Return to the index.


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

Return to the index.


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.

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.

Return to the index.


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.

Return to the index.


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.

Return to the index.



This site is maintained by Mike Hurley; this page was last updated on 5/31/19, 12:57 PM .

Legal Information | © 2018 Case Western Reserve University | Contact Prof. Hurley
..