Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of the square root should be passed as a second argument to the function.) An example of the program

Respuesta :

Answer:

import math

tolerance=0.00001

approximation=1.0

x = float(input("Enter a positive number: "))

def newton(number,approximation):

   approximation=(approximation+number/approximation)/2

   difference_value =abs(number-approximation**2)

   if difference_value<= tolerance:

       return approximation

   else:

       return newton(number,approximation)

print("The approximation of program = ", newton(x, approximation))

print("The approximation of Python = ", math.sqrt(x))

Explanation:    

  • Create a recursive function called newton that calls itself again and again to approximate square root for the newton technique.
  • Apply the formulas to find the estimate value  and the difference value .
  • Check whether the difference_value is less than the tolerance value  and then return the value of approximation  else make a recursive call to the newton function by passing the  user input and the new approximation value .
  • Finally display all the results using the print statement.
ACCESS MORE
EDU ACCESS