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:

- Pick a number
`a`

. I’ll start with 1.0.
- Check whether
`a`

is about the same as `cos(a)`

. If it is, return it.
- 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!

-Will

### Like this:

Like Loading...

*Related*