Exploring real iteration with Mathematica

As we move into our study of dynamics, it will be nice to have a computational aid in our studies. In this little document, I’ll collect a few examples. This is not a general intro to Mathematica; just a few quick examples illustrating the ideas in our class.

General comments

As in most computer languages, syntax - like capitalization, spacing, {braces}, vs [brackets] vs (parentheses)), tends to be important. Fortunately, the syntax of Mathematica is very consistent. built in commands follow the TypeOutTheFullCommand scheme. Function argements are enclosed in [square brackets]

Examples

Defining and iterating a function

Let’s get right to it! Suppose I want to iterate the function f(x)=(x^2)-1 and view the results. First, I define the function:

f[x_]=x^2-1;

Note the syntax: the [square brackets] and the underscore_ to indicate an argument.

Then, we iterate. Iteration is important enough that Mathematica has a whole family of built in functions just for that purpose. The most basic are Nest and NestList. The difference is that NestList returns the whole list of points generated, while Nest just returns the last value. Both functions take three arguments: the function, a starting point, and the number of iterates to compute. Thus:

NestList[f,1.23,25]

Note that we typed just the function name - that is, the f, rather than f[x]. Note also, that we’ve detected an attractive orbit!

Finding and illustrating fixed points

As we know, a fixed point of f is a point satisfying f(x)=x. Thus, finding fixed points boils down to solving equations and Mathematica has a number of tools for doing so. The most basic are Solve (which finds exact, algebraic solutions) and NSolve (which finds numerical approximations). Here’s how to use them to find the fixed points of f(x)=(x^2)-1.

f[x_] = x^2-1;
algebraicSolutions = Solve[f[x]==x,x]
numericSolutions = NSolve[f[x]==x,x]

Note that the results are returned as lists of rules. We can get just the values using the ReplaceAll operator, denoted /..

{x1,x2} = x /. algebraicSolutions

These points should appear as the points of intersection between the graphs of y=f(x) and y=x. Here’s the simplest way to plot those two functions.

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

There are some issues with this plot from our perspective, though. In particular, the aspect ratio is off and it would be nice to plot the points of intersection on the graph. We can use options to address this. These generally have the form
 option->value
Note that that the groovy arrow can be entered as “->”. Here’s how to do what we want:

plotToSave = Plot[{f[x],x},{x,-1.8,1.8},AspectRatio->Automatic,
Epilog -> {PointSize[Large],{Point[{{x1,x1},{x2,x2}}]}}]

Finally, let’s save the image. You can also do this from the file menu but the Export command provides greater flexibility.

Export["myPicture.png", plotToSave]