Fun with Fixed Points

Today we’re going to have fun with fixed points. A fixed point of a function f is the value x such that f(x) = x. Not everyone function has a fixed point. For example, f(x) = x + 1 has no fixed point. But many functions do. Cosine is one such function. Today we’re going to write code that finds the fixed point of the cosine function.

The procedure is basically this:

  1. Pick a number a. I’ll start with 1.0.
  2. Check whether a is about the same as cos(a). If it is, return it.
  3. Set a := cos(a) and repeat.

Here’s the procedure in Ruby:

And because we’re feeling whimsical, we’re going to do the same thing in Python:

And Rust:

And look! All three of these return exactly the same value 😀. OK that’s all for today. That was fun!

Till next time, happy learning!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s