Introduction to Programming with Mathematica, Third Edition

4.3: Iterating functions

4.3 Iterating functions

A commonly performed task in computer science and mathematics is to repeatedly apply a function to some expression. Iterating functions has a long and rich tradition in the history of computing. Perhaps the most famous example is Newton's method for root finding. Chaos theory rests on studying how iterated functions behave under small perturbations of their initial conditions or starting values. In this section, we will introduce several functions available in Mathematica for function iteration. In later chapters we will apply these and other programming constructs to look at some applications of iteration, including Newton's method.

The Nest function is used to iterate functions. Here, g is iterated (or applied to a) four times.

    <i class="emphasis">In[1]</i>:= <b class="bold">Nest [g, a, 4]</b>    <i class="emphasis">Out[1]</i>= g[g[g[g[a]]]]

The NestList function displays all of the intermediate values of the Nest operation.

    <i class="emphasis">In[2]</i>:= <b class="bold">NestList [g, a, 4]</b>    <i class="emphasis">Out[2]</i>= {a, g[a], g[g[a]], g[g[g[a]]], g[g[g[g[a]]]]}

Using a starting value of 0.85, this generates a list often iterates of the Cos function.

    <i class="emphasis">In[3]</i>:= <b class="bold">NestList [Cos, 0.85, 10]</b>    <i class="emphasis">Out[3]</i>= {0.85, 0.659983, 0.790003, 0.703843, 0.76236, 0.723208,             0.749687, 0.731902, 0.743904, 0.73583, 0.741274}

The list elements above are the values of 0.85, Cos [0.85], Cos [Cos [0.85]], and so on.

 ...

UNLIMITED FREE ACCESS TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Waveplates and Retardation Plates
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.