Mathematica Handout 1:  Useful commands

Math 53 - 1	Swarthmore College		Spring 2011		David McClendon

This handout describes the syntax for a variety of commands that we use in the study of dynamical systems.  Roughly speaking, this handout will describe how to :
	1.  Make graphs
	2.  Find solutions to equations numerically
	3.  Iterate functions and produce data on orbits

Graphs

Here is the most basic plot command :

Plot[x^2, {x, -2, 2}]

To plot more than one function at once, enclose the list of functions with braces :

Plot[{x^2, x, Cos[x]}, {x, -2, 2}]

Notice that the line y = x does not appear to have slope 1 here because the x - and y - axes are in different scales.  To fix this, add some more commands to the Plot command :

Plot[{x^2, x, Cos[x]}, {x, -2, 2}, AspectRatio -> Automatic]

If you want to control the height of the y - axis that you see on the picture, insert the following option :

Plot[{x^2, x, Cos[x]}, {x, -4, 4}, PlotRange -> {-1, 2},  AspectRatio -> Automatic]

We will frequently want to study the behavior of a family of transformations all at once.  To produce an animation, I like to use the Manipulate command.  Here is a command which plots the family of curves y = ax^2, all in the window [-4, 4] by [-10, 10], where a ranges from - 2 to 2.  Once the command is run, you can change the value of a by moving the slider bar or typing a numeric value of a into the box.  You can also run an animation with the buttons below the slider bar.

Manipulate[Plot[a x^2,{x,-4, 4},PlotRange->{-10, 10}],{a, -2,2, Appearance->"Open"}]

Mathematica gives you the ability to name functions so you don't have to keep typing in their formulas all the time.  For example, suppose you want to study the function f(x) = 2x sin(4x - 3).  You can define the function using the following command (note the underscore after the x):

f[x_] = 2x Sin[4x - 3]

After running this command Mathematica knows that the function f is really 2x sin(4x-3).  So if you type in

f[8]

you will get the value f (8) of the function when x = 8.  If you want a graph of the function you can type this:

Plot[f[x], {x, -5, 5}]

Solving equations

There are a variety of methods you can use on Mathematica to solve equations.  You can use the Solve command (the x after the comma tells Mathematica which variable to solve for and I think is required for the command to work):

Solve[5x^2 - 3x == 7, x]

Notice in the equation that you need two equals signs.  This is because when Mathematica sees a single equals sign, it thinks what you are trying to do is assign the right hand side of the equation to be the left-hand side.  For example, typing "x = 7" means you are assigning the value 7 to the variable x.

Notice also that Mathematica gave exact answers (in terms of square roots) to the equation.  If you want decimal approximations, use the NSolve command:

NSolve[5x^2 - 3x == 7, x]

You can also use these commands in conjunction with functions.  For example, this command finds the points of intersection of g (x) = 3 x^3 and h (x) = 2 x^4 - x^2 + 1 :

g[x_] = 3x^3;
h[x_] =2x^4 - x^2 + 1;
NSolve[g[x] == h[x], x]

One drawback to the use of Solve and NSolve is that these commands only work with polynomial and rational expressions.  If you try this command you will get an error :

NSolve[Cos[x] == x^2, x]

To get around this problem, use the FindRoot command.  This uses an iterative procedure similar to Newton' s method to calculate a numerical approximation to a solution of the given equation.  Like Newton' s method, this procedure requires an inital "guess" as to what the solution is.  For example, suppose we wanted to find solutions of cos x = x^2.  A good way to do this is to first sketch the graphs :

Plot[{Cos[x], x^2}, {x, -4, 4}, PlotRange -> {-1.5, 1.5}]

We see from the graph that the equation cos x = x^2 has two solutions, one around .8 and one around - .8.  We then use the FindRoot command to precisely approximate these solutions, using .8 and - .8 as our respective guesses :

FindRoot[Cos[x] == x^2, {x, .8}]

FindRoot[Cos[x] == x^2, {x, -.8}]

So we see that the solutions to cos x = x^2 are roughly x = ± .824132.

Iterating functions

Let' s suppose we start with a function T : ℝ → ℝ; our goal, of course, is to study such a transformation under iteration.  Computer packages such as Mathematica are particularly valuable because they can perform calculations that aren't doable by hand that reveal behavior in dynamical systems we might not otherwise guess exists.

We take as an example the transformation T (x) = -x^2 - x.  Start by entering this function into Mathematica:

T[x_] = -x^2- x

To generate iterates of T, use the Nest and NestList commands.  Once you tell Mathematica what T is, the command Nest[T, x, n] produces the function (T^n)(x).  If I wanted to find T(T(x)) and call this function T2, I'd use the command

T2[x_] = Nest[T,x,2]

Let' s say I wanted to know what (T^5)(2/3) was.  Here is the command (in general you need to tell Mathematica what T is first; also, you don't have to use the letter T for your function):

Nest[T, 2/3,5]

To get a decimal approximation, subsequently run this command :

N[%]

If you want a list of the first n iterates of a point, use the NestList command.  For example, NestList[T, x, n] will produce the list of points {x, T (x), (T^2)(x), ..., (T^n)(x)}:

NestList[T,2/3,4]

Here is a nice program taken from Holmgren' s book that lists the iterates (h^n)(z) where n ranges from FirstIterate to LastIterate.  If you copy this cell from this notebook into another notebook and then change h, z, FirstIterate and LastIterate, you can use this to list orbits of points under any transformation you like.  (To copy the cell, click the "]" to the right of the cell.  Then the cell would be treated as highlighted text in a Word document.)

Clear[h,x,i,y, z];
h[x_] = -x^2-x;
z = (2/3);
FirstIterate =0;
LastIterate = 3;

i = 0;
y = N[z];
While[i ≤ LastIterate, If[i ≥ FirstIterate, Print[i, "   ",N[y,8]]];
y = h[y]; i = i+1]

Here is another program adapted from one in Holmgren that draws cobweb diagrams.  In this program, whatever function you are iterating is called j (x) (in this example, j(x) = x^2 - 1) and the particular value of x for which you want to draw the cobweb is called StartingValue.  This draws the cobweb connecting the points (T^n)(x) where n ranges from FirstIterate to LastIterate.  The variables xmin, xmax, ymin and ymax set the viewing window for the picture that is generated.

j[x_]  = -x^2 - x;
StartingValue = .5;
FirstIterate = 0;
LastIterate = 20;
xmin = -1;
xmax = 1;
ymin  = -1;
ymax =1;
i = 0;
y = N[StartingValue];

While[i < FirstIterate, {y = j[y]; i = i + 1}];
DataTable = {{y,y}, {y,j[y]}};
While[i < LastIterate, y = j[y];
AppendTo[DataTable, {y,y}];
AppendTo[DataTable, {y, j[y]}];
i = i + 1];
AppendTo[DataTable, {j[y], j[y]}];
Cobweb = ListPlot[DataTable, Joined -> True, PlotRange -> {{xmin, xmax}, {ymin, ymax}}, AspectRatio -> Automatic, PlotStyle -> GrayLevel[.3], DisplayFunction -> Identity];
Graph = Plot[{j[x], x}, {x, xmin, xmax}, PlotRange -> {ymin, ymax}, AspectRatio -> Automatic, DisplayFunction -> Identity];
Show[Cobweb, Graph]