Question: Restructure Newton's method (Case Study: Approximating Square Roots) by decomposing it into three cooperating functions. The newton function can use either the recursive strategy of
Restructure Newton's method (Case Study: Approximating Square Roots) by decomposing it into three cooperating functions.
The newton function can use either the recursive strategy of Project 2 or the iterative strategy of the Approximating Square Roots Case Study. The task of testing for the limit is assigned to a function named limitReached, whereas the task of computing a new approximation is assigned to a function named improveEstimate. Each function expects the relevant arguments and returns an appropriate value.
The code that needs modified is below:
# Modify the code below
"""
Program: newton.py
Author: Ken
Compute the square root of a number.
1. The input is a number.
2. The outputs are the program's estimate of the square root
using Newton's method of successive approximations, and
Python's own estimate using math.sqrt.
"""
import math
# Receive the input number from the user
x = float(input("Enter a positive number: "))
# Initialize the tolerance and estimate
tolerance = 0.000001
estimate = 1.0
# Perform the successive approximations
while True:
estimate = (estimate + x / estimate) / 2
difference = abs(x - estimate ** 2)
if difference <= tolerance:
break
# Output the result
print("The program's estimate is", estimate)
print("Python's estimate is ", math.sqrt(x))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
