Finding Square Root by Newton's Iteration - An Introduction To Python and LaTeX
Finding Square Root by Newton's Iteration - An Introduction To Python and LaTeX
Finding Square Root by Newton's Iteration - An Introduction To Python and LaTeX
Now, is this guess good enough? We can find out by taking the square of Guess and checking whether it is close to X. In this case, our guess is not good enough. So, we will improve our initial guess; our improved guess should be, according to Newtons method:
Average of two numbers: Guess and X/Guess
In this case, it will be average of 1 and 16, that is 8.5. This improved guess is also not good enough, so we shall improve it by again taking average of Guess and X/Guess where Guess is now 8.5. The value we get will be:
(8.5 + 16/8.5) / 2 = 5.19
In just two more steps, we reach close enough to the required value!
We give the names guess and x to the parameters of improve; we could have as well used any other names, say, a and b. The use of the names guess and x makes the code more readable. Note the way we are using one function to build another (improve makes use of average).
>>>
The import statement is required because we are using a function called abs which is defined in the math library. abs returns the absolute value of its argument:
>>> abs(1) 1 >>> abs(-1) 1 >>>
We need to take the absolute value of the difference between guess*guess and x because guess*guess may be either greater than x or less than x; unless the absolute value is taken, this will cause problem when comparing with 0.001 in the next line. The statement:
(d < 0.001)
is either True or False depending on whether d is less than or greater than 0.001.
For finding square root of 16, we will call our function like this:
square_root(1, 16)
This will result in the parameter guess getting the value 1 and parameter x getting the value 16. In the condition part of the while loop, we have written:
not good_enough(guess, x)
Python calls the function good_enough with parameters 1 and 16 - good_enough will return False. The not operator, when it acts on the boolean value False, returns the value True. So we have the boolean value True in the condition part of the while loop. As condition is true, the body will get executed. The body is:
guess = improve(guess, x)
The improve function, given the values 1 and 16, returns 8.5. This becomes the new value of guess. The condition checking part of the while loop gets executed again; function good_enough gets called with parameters 8.5 and 16 and it returns False. The not operator returns True and so the body of the loop gets executed once more. This process stops only when good_enough returns True (in which case the condition becomes False because not True is False). If:
good_enough(guess, x)
is True, that means guess may be taken to be square root of x. When the loop terminates, the next statement:
return guess
returns the value of guess as the value of the function square_root(1, 16). The only problem with our square_root function is that we have to call it with two parameters, the first one being the initial value for guess. It is more natural to have a square root function which takes only one parameter - the number whose square root is required. We can manage this very easily by defining an additional function:
>>> def my_sqrt(x): ... r = square_root(1, x) ... return r ... >>>
9.4. Conclusion
We developed our square root function on top of other, simpler functions. Each function we wrote performed one simple computation and was easy to read and understand. The final square_root function too was very simple (all the hard work was being done by the other functions). Finding the square root is definitely not a big deal, and real life programs are incredibly more complex than this toy example. But the approach we have taken scales well - most big programs are written this way, as a collection of simple functions.