# Why do I get a converging result when pressing cosine multiple times on a calculator? [duplicate]

I'm trying to comprehend the following: If I choose any starting value (e.g. 1) and keep clicking on cosine on the calculator (in radian mode), it gives me a result of about 0.739085...(I believe it's the result of cos(x) = x), but when I repeat the same procedure using sin and tan, I get something completely different (looks like for sin it's converging to 0 while for tan I get very wild results).

• Might I recommend that you look at the Cobweb Plot associated to the sequences you define here. This gives a satisfactory visual interpretation of the phenomena you are observing. Commented Oct 11, 2017 at 19:26
• en.m.wikipedia.org/wiki/Cobweb_plot Commented Oct 11, 2017 at 19:26
• mathworld.wolfram.com/DottieNumber.html
– Dan
Commented Oct 11, 2017 at 22:47
• A picture might be worth a thousand words. Look at the iteration sequence of the sine . Commented Oct 11, 2017 at 22:58
• Same question just got 1 upvote two years back and now gets 21 upvotes.
– user312097
Commented Oct 12, 2017 at 23:43

Let $\cos^n$ denote the $n$-fold composition of the cosine function with itself, e.g. $$\cos^3(\theta) = \cos(\cos(\cos(\theta))).$$ Note that this is not usually what this notation means in, for example, introductory calculus texts. However, it is convenient in the current context.

What you are computing (assuming that it exists) is $$\lim_{n\to\infty} \cos^n(\theta).$$ Again, suppose that this limit exists and is equal to $L$. That is, suppose that there is some $L\in\mathbb{R}$ such that $$\lim_{n\to\infty} \cos^n(\theta) = L.$$ Taking the cosine of each side of this, and using the continuity of the cosine, we get $$\cos(L) = \cos( \lim_{n\to\infty} \cos^{n}(\theta) ) = \lim_{n\to\infty} \cos^{n+1}(\theta) = L.$$ Thus $L$, assuming that it exists, is a solution the equation $$\cos(x) - x = 0.$$ This confirms your suspicion that the number you are getting is a solution to this equation (presuming that such a number exists at all).

At this point, numerical methods are needed to get anything nicer out of this equation (hitting a button on the calculator repeatedly is such a numerical method), but you can prove that there is a solution via the intermediate value theorem: $\cos(0) - 0 = 1 \ge 0$ and $\cos(\pi) - \pi = -\pi < 0$, so there is a solution between $0$ and $\pi$.

What remains is to show that this limit actually exists. Note that the cosine function is a contraction on the interval $[-1,1]$. That is, if $x,y\in [-1,1]$, then there is a constant $|C| < 1$ such that $|\cos(x)-\cos(y)| < C|x-y|$. There are nice theorems that tell us that there must be a unique fixed point (specifically, theory that eventually leads to a theorem alternatively known as the "Contraction Mapping Principle" or the "Banach Fixed Point Theorem").

Note that cosine is a contraction on $[-1,1]$, and that $\cos(\mathbb{R})$ (i.e. the image of $\mathbb{R}$ under the cosine function, i.e. the range of the cosine function) is that interval. Thus, from any starting point, after the first iteration we are in the regime where $\cos$ is contractive, and so we will eventually converge to our fixed point, which was "determined" above (in so much as we can describe it). This pretty much says everything there is to know about the cosine function in this context.

The sine function is similar---it is contractive on the same interval, but it is much easier to find the fixed point: $$\sin(x) - x = 0 \implies x = 0.$$

The tangent function is not so nice. It is actually a dilation on the symmetric unit interval, hence we can't apply the Banach Fixed Point Theorem as above. There may be a fixed point, but to get at it, we'll have to work backwards, and consider iterative application of the $\arctan$ function.

These kinds of objects are studied as "dynamical systems." In the language of dynamical systems, the fixed points of cosine and sine functions are attractive, in that everything eventually "falls into" those points. The fixed point of the tangent function is repulsive (or repelent), in that things are pushed away from it.

• Why was Wolfie unable to get an exact form of the solution to $\cos x-x=0$? Commented Jul 12, 2020 at 20:31
• @gen-zreadytoperish Because, as I said above, you are going to need to use numerical methods to solve this. There isn't a nice, closed form, analytic solution. See here. Commented Jul 12, 2020 at 22:21

Here's a picture:

The blue line is $y=\cos x$, the green line is $y=x$. The red dashed line shows what happens when you repeatedly apply the cosine on an initial starting value, in this case zero. You can see how it converges to the intersection of the two lines, i.e., the point where $x=\cos x$.

Generating the red dashed line

In the image, start from the point $(0,0)$ and follow the dashed line. Moving vertically from $(x,y)$ to $(x,y')$ corresponds to applying $y'\gets\cos x$. Moving horizontally from $(x,y)$ to $(x',y)$ corresponds to applying $x'\gets y$. Starting from $(x_0,y_0)$, the points then follow this sequence ($V$ and $H$ denote moving vertically and horizontally respectively): \begin{align} (x_0,y_0) &\overset{V}{\to} (x_0,\cos x_0)\\ &\overset{H}{\to} (\cos x_0,\cos x_0)\\ &\overset{V}{\to} (\cos x_0, \cos(\cos x_0))\\ &\overset{H}{\to} (\cos(\cos x_0),\cos(\cos x_0))\\ &\overset{V}{\to} (\cos(\cos x_0),\cos(\cos(\cos x_0)))\\ &\overset{H}{\to} \cdots \end{align}

• which software have you used for this? Commented May 17, 2020 at 14:23
• I believe I just used GNU Octave. Commented May 18, 2020 at 15:05

On a small enough interval (say $[-\pi/2 + \varepsilon, \pi/2 - \varepsilon]$), $\cos(x)$ is a contraction mapping, i.e. $$|\cos(x) - \cos(y)| \le q |x-y|$$ for some $q < 1.$ (This is because its derivative is bounded by $1$.) The Banach fixed-point theorem applies to show that $\cos(x) = x$ has a unique solution, and for any starting point $x$ in that interval, the sequence $(x_n)_n$ defined by $x_0 = x$ and $x_{n+1} = \cos(x_n)$ converges to that solution.

The fact that $\cos(x)$ is periodic and even lets you extend this result to all $\mathbb{R}.$ It's similar for $\sin(x)$ (note $\sin(0) = 0$ is the unique fixed point). It doesn't work for $\tan(x)$ because $\tan(x)$ is not a contraction.

• But cosine doesn't necessarily map everything on that interval back to that interval.Perhaps you should take the interval to be $[-1, 1]$. Cosine is a contraction there. Commented Oct 11, 2017 at 19:16

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.

This is a classic application of the well-known Newton-Raphson method.

We want to solve $$y = x - \cos x$$ for $$y = 0$$

$$y = x - \cos x$$

Differentiating,

$$dy/dx = 1 + \sin x$$

Now $$\frac{dy}{dx} \approx \Delta y / \Delta x$$

So we can let

$$\Delta x = \frac{\Delta y}{1 + \sin x}$$

Now

$$\Delta y = -y = \cos x - x$$

Thus

$$\Delta x = \frac{\cos x - x}{1 + \sin x}$$

We could stop there, but if we continue we get a nice cancellation.

Let $$x'$$ be the new approximation for $$x$$. That is

\begin{align} x' & = x + \Delta x\\ & = x + \frac{\cos x - x}{1 + \sin x}\\ & = \frac{x + x \sin x + \cos x - x}{1 + \sin x}\\ x' & = \frac{x \sin x + \cos x}{1 + \sin x}\\ \end{align}

Here's a Python program which uses that formula. We only need to do a few loops to reach the limit of precision of 64 bit floating-point numbers.

from math import sin, cos

x = 1
for i in range(4):
print(i, x)
s = sin(x)
x = (cos(x) + s * x) / (s + 1)

print(x, cos(x))


###output

0 1
1 0.7503638678402439
2 0.7391128909113617
3 0.7390851333852839
0.7390851332151607 0.7390851332151607


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

from mpmath import mp

def print_digits(s, colsize=5, rowsize=50, blocksize=250):
''' Print high precision decimal string in columns, rows, & blocks '''
# Split off integer part & decimal point
*parts, s = s.partition('.')
print(''.join(parts))

# Format digits after the decimal place
cols = []
for i in range(0, len(s), colsize):
j = i + colsize
# Columns are separated by a space,
# rows are separated by a single newline,
# blocks are separated by two newlines.
sep = (' ' if j % rowsize
else '\n' if j % blocksize else '\n\n')
cols.extend([s[i:j], sep])
print(''.join(cols))

# Set the precision to 2000 decimal places
mp.dps = 2000

sin, cos = mp.sin, mp.cos
x = 1
for i in range(11):
s = sin(x)
x = (cos(x) + s * x) / (s + 1)

print_digits(str(x))


###output

0.
73908 51332 15160 64165 53120 87673 87340 40134 11758 90075
74649 65680 63577 32846 54883 54759 45993 76106 93176 65318
49801 24664 39871 63027 71490 36913 08420 31578 04405 74620
77868 85249 03891 53928 94388 45095 23480 13356 31276 77223
15809 56353 77657 24512 04373 41993 64335 12538 40978 00343

40646 70047 94021 43478 08027 18018 83771 13613 82042 06631
63350 37277 99169 67312 23230 06138 86582 03621 77081 09978
97062 68424 05880 94898 68326 18606 00485 89895 85487 25736
76401 50752 27608 18039 14595 18101 62815 91200 96461 64606
75440 51326 41517 10644 66281 10936 08258 48783 71383 95555

61751 41494 71593 90062 77527 56325 86349 38869 73014 08366
51525 11520 42678 85153 02529 41718 03651 76420 17708 60718
99276 01609 87432 71545 52267 56579 82462 97611 77553 96166
99549 31115 85665 34834 95383 85231 59636 02527 49955 87252
50666 64013 13187 40139 25388 88055 20618 69859 21392 52528

54154 11079 10029 98282 92986 40521 69046 55473 66968 71438
73564 60065 21225 46891 49975 92096 99758 50136 42495 08565
04732 49725 84248 37155 48364 83437 27583 74675 25453 35800
66420 04788 39718 85848 90145 31155 06041 78123 37047 77395
34717 10345 11958 54600 72656 14647 21419 78753 73880 23680

29553 44127 94853 01620 77437 43315 90133 91933 23148 76628
28552 17782 70052 31111 78246 86229 57127 86199 58490 58929
78171 80601 56715 85092 53714 04181 46882 85824 54046 44526
55883 15798 59786 67282 99052 07226 86870 94531 30864 95350
44481 38762 32367 76569 23613 25971 52294 15582 29334 15223

69636 98322 65805 17766 85366 37759 37066 43679 29565 98287
11924 91109 47930 11267 60115 22614 29243 71121 71487 02935
43102 93038 78065 42303 10930 07600 02409 80335 56727 30891
51766 68247 56247 72917 20259 45634 73838 58189 99548 42071
81825 61281 93120 90775 73740 22398 85853 96422 00631 32270

25328 45511 60110 76298 67410 60230 99696 24600 46288 53977
84470 79428 66907 48442 61986 19229 57526 39675 15917 84325
95550 97753 94210 80967 31399 81533 82554 94805 27727 29806
59555 18624 34873 99989 44589 81750 86210 57831 26013 92783
08279 72906 91694 42203 86806 52977 23102 01474 61968 67501

16979 00223 36525 95179 73670 54931 31976 05150 85764 33674
63449 04471 87196 10601 81776 20236 18885 83971 51415 31833
42332 51271 72162 70117 51797 43026 02487 76822 00067 45587
38185 02965 94214 70461 70423 42065 97465 13966 68395 61557
29100 71089 57281 30968 30388 12301 43385 35545 47878 98962


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


For all $x, |\sin x| \le |x|$

The sequence $\sin \circ \sin\circ \cdots \circ \sin x$ monotonically approaches $0$

More generally the sequence $f^n(x)$ where $n$ represents composition, will converge some $x$ if there exists some $x$ such that $f(x) = x$ and $|f'(x)| < 1$ in a sufficiently large neighborhood.

When that is the case $x-\delta < f(x+\delta) < x + \delta$ and with each iteration we find ourselves closer to $x.$

$\cos x$ fits the bill.

There are values of $\tan x$ such that $\tan x = x$ passing one test, e.g. $x\approx 4.494$ however $|\frac {d}{dx} \tan x | \ge 1$ failing the second test.

$\ln x$ will fail, because it does not pass the first test.

According to Wikipedia (the greatest source ever), the fixed point at $x$ is attractive if the function is continuously differentiable in an open neighbourhood of $x$ and $|f'(x)|<1$ in that neighbourhood.

It is left as an exercise to the reader to confirm that it is indeed the case.