Newton's Method for Square Root
How do you find the square root of a number? This question has many important applications in science, graphics, and physical simulation. While there are many speedy, innovative (and sometimes mind-blowing) ways to do this, this assignment will have you implement one of the classic methods to solve this problem: Newton's Method.
Background
Like the bisection method we discussed in class, Newton's method is a method for values of such that . In its most general form, we give it the function and an initial guess . As long as is "close enough" to the correct value, Newton's method is guaranteed to converge.
In this assignment, we will specialize Newton's Method to computing the square root of a target number. In this scheme, the method is also guaranteed to eventually get the right answer, though it might take varying amounts of time.
The basic idea is straightforward: suppose that we have a number Z, and we want to compute . We start off by setting . Then, to improve our estimate, we repeatedly set . Of course, if this is all we do, we'll run into an infinite loop. To determine a stopping point, we'll set some arbitrary cutoff. If is within the cutoff distance to , then we stop.
While the basic idea is simple, the details can be a little tricky: when do you calculate if the condition has been met? If you do this wrong, you might never enter the loop! How do you update the estimate? What other information should you keep track of?
Assignment
Your program should ask the user for a number and interpret their input as a float
. It should then
compute the square root of that number using Newton's method. You should also count how many times the loop ran
while computing the square root.
Once you have computed the square root with Newton's method, your program should also compute the square root
separately using the sqrt
function from math
. You can import this function by putting
from math import sqrt
at the top of your file. We will call this the "correct" value.
Finally, you should calculate how far your Newton estimate was from the correct value: you can use abs
to calculate this.
Once you have done all this, print the Newton estimate, the correct value, and the distance between the two. On a second line, print how many times the loop ran during Newton's Method.
Use 0.00001
as your cutoff value for the Newton estimate.
Examples
> python Newton.py
Please enter a number: 2.7
sqrt (Newton): 1.643, sqrt (math): 1.643, diff: -2.7835058924452483e-08
Performed 3 steps.
Insight
Try several different numbers to see how many steps Newton's method needs to take. Is there a pattern you see with number of steps needed compared with the input number?
In your submission header, include four of inputs that you tried, and how many steps each one took. You can simply
list these as pairs, e.g. (2.7, 3), (10.0, 4), (100, 10), (1000, 20)
. The first number is the input and
the second number is how many iterations (steps) it took.
In addition, write a 1 or 2 sentence description of any patterns that you notice in the number of steps needed. You may need to run more than four inputs in order to spot this pattern, and you may need to try some large numbers.
Submission
Submit a single file named Newton.py
(pay attention to capitalization!) on Canvas. Your file needs to
compile and run. It should also have a header with the following information (this goes in your source file, not in
the program output):
# File: Newton.py
# Student:
# Course: Intro to Programming
#
# Date:
# Description of Program:
#
# Four inputs you tried, and how many steps each took:
# Any pattern you notice in the number of steps needed:
The description should be a short (1-3 sentence) description of what the program does. Do not describe how it's written!