Octave Implementation of Square-root using Newton's Method
John Bryan
- Notation
- $r = \text{number that we want to find the square root of.} \quad {r\in\mathbb{R}|r\ge0}$.
- $r0 = r/4^n \text{ where } 0.25<r0<1, n\in\mathbb{Z}.$
- $\sqrt{r} = \sqrt{(r0)(4^n)} = (\sqrt{r0})(2^n).$
- $X_i = $ approximation to $\sqrt{r0}$ in Newton iteration i.
- $X_0 = $ initial approximation to $\sqrt{r0}$.
- Equations
$$ \begin{align} X_0 = 0.6666r0 + 0.3506.\tag{eq. 1} \end{align}$$
$$ \begin{align} X_{i+1} & =X_i-\frac{f(X_i)}{f^\prime(X_i)} \\[1.5ex]
& = X_i-\frac{r0-X_i^2}{-2X_i} \\[1.5 ex]
& = X_i+\frac{r0}{2X_i}-\frac{X_i}{2}\\[1.5 ex]
& = \frac{X_i}{2}+\frac{r0}{2X_i}.
\tag{eq. 2}
\end{align}
$$
-
Octave implementation.
-
Results.
-
Finding $\sqrt{r} = \sqrt{13}$:
$r0 = r/4^n = 13/4^2 = 13/16 = 0.8125.$
$n = 2.$
$ X_0 = 0.6666r0 + 0.3506 = 0.6666(0.8125) + 0.3506.$
$$
\begin{array}{|c|c|}
\hline
X_0 & 0.\color{red}{89226666666667} \\\hline
X_1 & 0.901\color{red}{43443913130} \\\hline
X_2 & 0.9013878\color{red}{2007155} \\\hline
X_3 & 0.90138781886600\color{red}{} \\\hline
X_4 & 0.90138781886600\color{red}{} \\\hline
\end{array}
$$
$\sqrt{13} = \sqrt{r} = (\sqrt{r0})(2^n) = (X_4)(2^2) = (0.90138781886600)(4) = 3.60555127546399.$