Skip to main content
fixed typo
Source Link
PM 2Ring
  • 5.9k
  • 1
  • 23
  • 33

Using the arbitrary precision mpathmpmath library, we can easily get a lot more digits with only a few more loops.

Using the arbitrary precision mpath library, we can easily get a lot more digits with only a few more loops.

Using the arbitrary precision mpmath library, we can easily get a lot more digits with only a few more loops.

added 1621 characters in body
Source Link
PM 2Ring
  • 5.9k
  • 1
  • 23
  • 33

We can simplify this formula. The simplified version doesn't converge as rapidly, but it's easier to compute, and it still converges faster than iterating $x = \cos x$

The sine of Dottie's number is $\approx 0.673612$. We can round that to $\frac{2}{3}$ and plug that into our formula. It will still converge even if we start with $x = 1$, but it's better to start with a closer approximation, say $x = \frac{3}{4}$. Note that $\left(\frac{2}{3}\right)^2 + \left(\frac{3}{4}\right)^2 = \frac{64 + 81}{144} = \frac{145}{144} \approx 1$.

$$x' = \frac{x \sin x + \cos x}{1 + \sin x}$$

Substituting $\sin x = \frac{2}{3}$

$$\begin{align} x' & = \frac{\frac{2}{3} x + \cos x}{1 + \frac{2}{3}}\\ & = \frac{\frac{2}{3} x + \cos x}{\frac{5}{3}}\\ & = \frac{2 x + 3 \cos x}{5}\\ x' & = 0.4 x + 0.6 \cos x \end{align}$$

In other words, we perform a weighted mean of $x$ and $\cos x$. This still converges to the correct value. At convergence,

$$\begin{align} x & = \frac{2 x + 3 \cos x}{5}\\ 5x & = 2x + 3 \cos x\\ x & = \cos x \end{align}$$

And here's a short demo in Python.

from math import cos

x = 0.75
for i in range(8):
    y = cos(x)
    print(i, x, y)
    x = 0.4 * x + 0.6 * y

###output

0 0.75 0.7316888688738209
1 0.7390133213242926 0.7391335046629345
2 0.7390854313274777 0.7390849324030849
3 0.739085131972842 0.7390851340520015
4 0.7390851332203376 0.7390851332116734
5 0.7390851332151391 0.7390851332151751
6 0.7390851332151607 0.7390851332151607
7 0.7390851332151607 0.7390851332151607

We can simplify this formula. The simplified version doesn't converge as rapidly, but it's easier to compute, and it still converges faster than iterating $x = \cos x$

The sine of Dottie's number is $\approx 0.673612$. We can round that to $\frac{2}{3}$ and plug that into our formula. It will still converge even if we start with $x = 1$, but it's better to start with a closer approximation, say $x = \frac{3}{4}$. Note that $\left(\frac{2}{3}\right)^2 + \left(\frac{3}{4}\right)^2 = \frac{64 + 81}{144} = \frac{145}{144} \approx 1$.

$$x' = \frac{x \sin x + \cos x}{1 + \sin x}$$

Substituting $\sin x = \frac{2}{3}$

$$\begin{align} x' & = \frac{\frac{2}{3} x + \cos x}{1 + \frac{2}{3}}\\ & = \frac{\frac{2}{3} x + \cos x}{\frac{5}{3}}\\ & = \frac{2 x + 3 \cos x}{5}\\ x' & = 0.4 x + 0.6 \cos x \end{align}$$

In other words, we perform a weighted mean of $x$ and $\cos x$. This still converges to the correct value. At convergence,

$$\begin{align} x & = \frac{2 x + 3 \cos x}{5}\\ 5x & = 2x + 3 \cos x\\ x & = \cos x \end{align}$$

And here's a short demo in Python.

from math import cos

x = 0.75
for i in range(8):
    y = cos(x)
    print(i, x, y)
    x = 0.4 * x + 0.6 * y

###output

0 0.75 0.7316888688738209
1 0.7390133213242926 0.7391335046629345
2 0.7390854313274777 0.7390849324030849
3 0.739085131972842 0.7390851340520015
4 0.7390851332203376 0.7390851332116734
5 0.7390851332151391 0.7390851332151751
6 0.7390851332151607 0.7390851332151607
7 0.7390851332151607 0.7390851332151607
added 453 characters in body
Source Link
PM 2Ring
  • 5.9k
  • 1
  • 23
  • 33

As Xander Henderson and others have mentioned, this operation converges on the solution of the equation $\cos x = x$, and have explained why it converges, so I won't cover that fact in this answer. I'll merely mention that if you look at the graphs of $y = \cos x$ and $y = x$ it's pretty obvious that there's one and only one real solution to $\cos x = x$. However, I noticed that none of the existing answers actually give a value for this $x$, sometimes known as Dottie's Number, or mention a faster way to evaluate it, and I intend to remedy that situation.

It has also been mentioned that there's no closed-form solution to this equation using elementary functions. It converges reasonably quickly, as you've discovered, (and as I discovered several decades ago, when scientific calculators first appeared :) ), but we can easily use a little bit of calculus to find an algorithm that converges much more rapidly.

As Xander Henderson and others have mentioned, this operation converges on the solution of the equation $\cos x = x$, and has also been mentioned there's no closed-form solution to this equation using elementary functions. It converges reasonably quickly, as you've discovered, (and as I discovered several decades ago, when scientific calculators first appeared :) ), but we can easily use a little bit of calculus to find an algorithm that converges much more rapidly.

As Xander Henderson and others have mentioned, this operation converges on the solution of the equation $\cos x = x$, and have explained why it converges, so I won't cover that fact in this answer. I'll merely mention that if you look at the graphs of $y = \cos x$ and $y = x$ it's pretty obvious that there's one and only one real solution to $\cos x = x$. However, I noticed that none of the existing answers actually give a value for this $x$, sometimes known as Dottie's Number, or mention a faster way to evaluate it, and I intend to remedy that situation.

It has also been mentioned that there's no closed-form solution to this equation using elementary functions. It converges reasonably quickly, as you've discovered, (and as I discovered several decades ago, when scientific calculators first appeared :) ), but we can easily use a little bit of calculus to find an algorithm that converges much more rapidly.

Source Link
PM 2Ring
  • 5.9k
  • 1
  • 23
  • 33
Loading