Suppose you were given a number and asked to find its square root? How would you do it? The great Isaac Newton
thought about such puzzles, and came up with the following method.
Say you want to take the square root of a number $x$. Newton's method always started with a guess. But what to guess? Well, we know
the square root of a number is less than the number itself, like $\sqrt{25}=5$, $\sqrt{625}=25$, and $\sqrt{10}\approx 3.2$ so our guess should always be
less than the number itself. How about the number divided by two? So our initial
guess at the $\sqrt{x}$ will be $x/2$ or $g=x/2$.
The idea of Newton's method is to keep correcting our guess until a better and better result for the square root of our original number appears. So, we'll
correct our guess to be $z$ where $z=g-\frac{g^2-x}{2\times g}$ (this is a formula Newton came up with). When done,
$z$ will be a better estimate of the square root than our intial guess.
What do we do next? That's right! Call $z$ our new [and better] "guess" and run the procedure over again. If
we keep doing this again and again, a pretty decent result for the square root of our number will emerge.
Let's write a function called mysqrt that we can call to find square roots using Newton's method.
Now you try. Using fixing the sqrt function according to the discussion above to implement Newton's "guess and check" method for finding square roots.
Type your code here:
See your results here:
The code will not run as is! First, what will you put in for the guess in the g= line? Second,
after the better guess better is calculated, you need to assign this to the g right before the end to seed
the next iteration through the for loop. Next, fill in a number for num= (this will be the number for which you want to take the square root).
Lastly, doing Newton's method repeatedly yields better and better results. How many times do you want to run this technique? Choose some ending
limit for the for=1, for-loop (try 10 or so).
Share your code
Show a friend, family member, or teacher what you've done!