Lesson goal: Iterating a function with a for-loop

Previous: Finding square roots | Home | Next: Interating the logistic function

Programming languages often have built in functions that perform a particular task. Previous lessons demonstrated those that take square roots, find random numbers, make pi available, and compute absolute values. There an interesting math lesson that combines functions and for-loops, called "iteration." The theme is to address what happens to a number if you apply a function to it over and over again.

As an example, suppose you took the number $137$ and took the square root of it. You'd get $11.7$. Then, what if you took the square root of $11.7$? And what if you kept doing over and over again, maybe 100 times? Would an eventual number or pattern emerge?

It's a strange question, but a very important one in modern research mathematics called "interation." With a for-loop, you can easily repeatedly apply a function to a number as many times as you want. The result of this "research" is to determine what (stable) number these interations eventually produce.

In this lesson, we'll try interating the square root of some number. In other words, we'll choose a number and apply the square root to it, over and over again.

Now you try. Try choosing an initial value for x. Fix the for loop to count to 100 or so, then program in the function to interate in the x= line under the for loop.

Type your code here:


See your results here:

This code will not run. You have to put in a starting number in the x= line. Next, finish the for i=1, line to put in how many times you wish to interate the square root (try 100). Lastly, what will you put in for the x= line inside of the for loop to take the square root of x itself?

When you are done with the square root, try interating math.sin, then math.cos. Dismiss.

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.