226k views
5 votes
Restructure Newton's method (Case Study: Approximating Square Roots) by decomposing it into three cooperating functions: newton, limitReached, and improveEstimate.

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.
An example of the program input and output is shown below:
Enter a positive number or enter/return to quit: 2
The program's estimate is 1.4142135623746899
Python's estimate is 1.4142135623730951
Enter a positive number
0.00
out of
10.00
The program's newton, limitReached, and improvedEstimate functions work as expected.
0 out of 3 checks passed. Review the results below for more details.
Checks
Unit TestIncomplete
The newton function returns the square root of 'x'
Test Output
Traceback (most recent call last):
File "/root/sandboxee2e1795/ ", line 2, in
from newton import *
File "/root/sandboxee2e1795/ ", line 10
val = improvateEstimate(val,n) if limitReached(val,last):
^
IndentationError: unindent does not match any outer indentation level
Test Contents
class UnitTests(unittest.TestCase):
def test_unit_test_1(self):
self.assertEqual(newton(2), 1.4142135623746899)
def test_unit_test_2(self):
self.assertEqual(newton(13), 3.6055513629176015)
Unit TestIncomplete
The limitReached function returns True if the estimate is within the tolerance, if not, False
Test Output
Traceback (most recent call last):
File "/root/sandboxee2e1795/ ", line 2, in
from newton import *
File "/root/sandboxee2e1795/ ", line 10
val = improvateEstimate(val,n) if limitReached(val,last):
^
IndentationError: unindent does not match any outer indentation level
Test Contents
class UnitTests(unittest.TestCase):
def test_unit_test_1(self):
ans = limitReached(2,1)
if ans != False:
ans = limitReached(1,2)
if ans != False:
raise AssertionError(f"Incorrect return value. Expected `False`, was {ans}")
def test_unit_test_2(self):
ans = limitReached(16,1)
if ans != False:
ans = limitReached(1,16)
if ans != False:
raise AssertionError(f"Incorrect return value. Expected `False`, was {ans}")
def test_unit_test_3(self):
self.assertEqual(limitReached(1,1), True)
Unit TestIncomplete
The improveEstimate function returns an improved estimate
Test Output
Traceback (most recent call last):
File "/root/sandboxee2e1795/ ", line 2, in
from newton import *
File "/root/sandboxee2e1795/ ", line 10
val = improvateEstimate(val,n) if limitReached(val,last):
^
IndentationError: unindent does not match any outer indentation level
Test Contents
class UnitTests(unittest.TestCase):
sys.tracebacklimit = 0
def test_unit_test_1(self):
ans = improveEstimate(2,1)
if ans != 1.5:
ans = improveEstimate(1, 2)
if ans != 1.5:
raise AssertionError(f"Incorrect return value: {ans} != 1.5")
def test_unit_test_2(self):
ans = improveEstimate(9,1)
if ans != 5.0:
ans = improveEstimate(1, 9)
if ans != 5.0:
raise AssertionError(f"Incorrect return value: {ans} != 5.0")

1 Answer

4 votes

Final answer:

The question requires implementing three functions to approximate square roots using Newton's method: newton, limit Reached, and improve Estimate. The main purpose is to create an efficient alternative to solving quadratic equations for roots directly, with attention to Python's indentation in code structure.

Step-by-step explanation:

The task described involves implementing Newton's method for approximating square roots. This method presents an efficient mathematical approach that sidesteps the complexities of directly solving roots of quadratic equations, which can be computationally expensive. Instead, the method iteratively improves an initial guess until the desired accuracy is reached. Newton's method is to be decomposed into three functions:

  • newton function: Either recursively or iteratively calculates the square root of a given number.
  • limit Reached function: Determines whether the current estimate is within the acceptable tolerance.
  • improve Estimate function: Calculates a better estimate based on the current guess and the number we want the square root of.

The tracebacks provided indicate an Indentation Error, suggesting that the code structure needs correction to match Python's strict indentation rules for blocks.

User Artem Vyshniakov
by
7.3k points