Finding the square root of a number usually involves this function:

\({\sf better}(guess) = \frac{1}{2}(guess + \frac{x}{guess})\)

This is how you can define it in R.

better <- makeFun((guess + x/guess)/2 ~ guess)

The function takes two arguments: \(guess\) and \(x\). The algorithm starts with dividing \(x\) by a number for \(guess\), and adding \(guess\) to it. Usually, we use the number \(1\) as the starting point. We plug that into the function as \(guess\). In other words, \(guess\) is the value that we will iterate forward. We then divide the result in half. Let’s use the function for an example. For the first iteration, let’s do \(guess = 1\)

better(28, x=55)
## [1] 14.98214

This process is repeated (in a number of iterations), by inserting the result (in this case \(28\)) back into the function as \(guess\). That would produce the result \(14.98214\). And we will use that again as \(guess\) and run the equation. This process is done again and again up to a point where the \(guess = {\sf better}(guess)\). Or, at least. We can automate this with Iterate().

Iterate(better(guess, x=55) ~ guess, x0=1, n=8)
##   n     guess
## 1 0  1.000000
## 2 1 28.000000
## 3 2 14.982143
## 4 3  9.326590
## 5 4  7.611854
## 6 5  7.418713
## 7 6  7.416199
## 8 7  7.416198
## 9 8  7.416198

It seems like the result goes down and stabilizes, until it finally settles down on one value. Note that I said in the previous paragraph, “at least.” It’s because this “stabilization” isn’t quite true. Have another look at the table above. 8th \(7.416198\) and 9th \(7.416198\) looks the same. At least the first couple digits. When we compute in higher precision, we will see a tiny, tiny specks of difference the deeper the decimal place goes.

We can plot this out to get an easier look on how the values change with each iteration.

The result of \({\sf better}(guess)\) (black line) meets with the linear (x equals y) dotted line, exactly at \(7.416198\). Here’s a closer look at the graph.

Let’s try again, but with an easier number. \(64\).

Iterate(better(guess, x=64) ~ guess, x0=1, n=8)
##   n     guess
## 1 0  1.000000
## 2 1 32.500000
## 3 2 17.234615
## 4 3 10.474036
## 5 4  8.292192
## 6 5  8.005148
## 7 6  8.000002
## 8 7  8.000000
## 9 8  8.000000

Zoom in, enhance.

Blue dotted line perfectly intersects with the black line, at 8x 8y.

\({\sqrt 64} = 8\)

Reference:

Kaplan, Daniel. 2022. MOSAIC Calculus. GitHub Pages. https://dtkaplan.github.io/MC2/


contra