Numerical Methods: Recursive Square Root |
Numerous algorithms exist for determining the square root of a non-negative
real number.
An interesting one that can be derived from Newton's Method, involves starting
with an initial guess followed by successive
refinements.
For example, suppose the square root of a number, n, is sought. Taking an initial guess g0, we would have our answer if g02 was sufficiently close to n, say,
|n-g02|< 0.001
If it is not, we could use the average of g and n/g0, call it g1, and try again. As a result, our feedback strategy should reveal a sequences of closer approximations to the square root of n.
Task: Write a recursive function,
float sqrt(float n, float g)
that will return an approximation to the square root of n, accurate to 0.001.