root algorithm (example on while-loops)
short article we’ll explore a square
root algorithm as an excuse to use
in our numerical software. We’re not going to use the built-in
typically implement routines to compute the exponential function and
natural logarithm, and then compute the root of a positive real number x using this identity:
follow a different approach: an iterative
method using while-loops.
most common iterative method of square root calculation is known as the
method’ or ‘Babylonian
step, estimate a number. Think of this number as your
first approach to a root (the
closer to the actual square root of x,
the fewer iterations will be needed to achieve the desired precision).
step, divide your number by this (known inexact) square
step, take the average of the result of the second step
and ‘the root’.
step, use the result of the third step to repeat steps 2
and 3 until you have a
number that is accurate enough for you.
technique is a variation of the Newton-Raphson
iterative solution method and
it involves a simple algorithm, which results in a number closer to the
square root each time it is repeated.
that accomplishes the algorithm is:
function ta =
Choose (arbitrarily) a first approach
Divide your number by that first approach
Get the mean between the two previous numbers
Repeat until you obtain a good enough root
while abs(x -
ta^2) > 0.000000001
use or try our function like this, from the command window for example:
algorithm needed four iterations to obtain the final result (in y1,
that only two iterations were needed to have an accuracy within three
y2 is the result from the function ‘sqrt’, to compare with.
algorithm needed six iterations to obtain the final result (in y3,
that only four iterations were needed to have an accuracy within one
y4 is the result from the function ‘sqrt’, to compare with.
y4 = 15.96871942267131
From 'Square Root algorithm' to home
From 'Square Root algorithm to