Why does this sequence converge to $\pi$?

399 Views Asked by At

Over at our friends at codegolf.SE, I asked a question about programs that seemed to converge to $\pi$, but didn't actually do that.

One of the answers (by soktinpk) was a solution that, although I can see many opportunities for numerical errors leading to a not-quite-$\pi$ result, I can't figure out why it seems to converge to $\pi$ in the first place. The scheme is as follows:

  • Pick a number $h\ll1$. Take $s_0=1, s_1=1$.

  • While $s_n\ge 0$, calculate the next term as follows: $$s_n=2s_{n-1}-(1+h^2)s_{n-2}$$

  • When the above procedure terminates, calculate $p=2nh$, and behold, $p\approx\pi$!

What causes this? Is it an accidental Taylor series, an inadvertent approximation of a sinusoid (that's my guess, perhaps it's something like $s\approx \sin(n/h)$ solved for $s=0$), or does this series have nothing to do with $\pi$ and is it just a result of people shouting LOOK IT'S $\Pi$ whenever they see 3.14...?

1

There are 1 best solutions below

0
On BEST ANSWER

The characteristic equation for the recurrence relation $$s_{n} - 2s_{n-1} + (1+h^2)s_{n-2} = 0$$ is $\displaystyle\;\lambda^2 - 2\lambda + (1+h^2) = (\lambda-1)^2 + h^2 = 0\;$ which has roots $1 \pm i h$. The general solution of the recurrence relation has the form $$s_n = A (1+ih)^n + B(1-ih)^n$$ If one impose the initial condition $s_0 = s_1 = 1$, one find $A = B = \frac12$. This leads to

$$s_n = \frac12 ((1+ih)^n + (1-ih)^n)$$

Let $\theta = \tan^{-1}(h)$, we have $$1 \pm ih = 1 \pm i\tan(\theta) = \frac{1}{\cos\theta}e^{\pm i\theta} = (1+\tan(\theta)^2)^{1/2} e^{i\theta}$$ We can simplify $s_n$ as $$s_n = \frac12 (1+h^2)^{n/2} \left( e^{in\theta} + e^{-in\theta} \right) = (1+h^2)^{n/2} \cos(n\theta)$$

At least for small $h$, the first $n$ that make $s_n < 0$ is one which make $n \theta > \frac{\pi}{2}$.
It is clear this equals to $\left\lfloor \frac{\pi}{2\theta} \right\rfloor + 1$ and hence

$$2nh = 2h\left(\left\lfloor \frac{\pi}{2\tan^{-1}(h)} \right\rfloor + 1\right)$$

For small $h$, $\tan^{-1}(h) \approx h$. This implies $n \approx \frac{\pi}{2h} \implies 2nh \approx \pi$.

In certain sense, the computed $p \approx \pi$ because for small $h$, $1+ih \approx e^{ih}$.