Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a). a = 1 | my_sqrt(a) = 1 | math.sqrt(a) = 1.0 | diff = 0.0 a = 2 | my_sqrt(a) = 1.41421356237 | math.sqrt(a) = 1.41421356237 | diff = 2.22044604925e-16 a = 3 | my_sqrt(a) = 1.73205080757 | math.sqrt(a) = 1.73205080757 | diff = 0.0 a = 4 | my_sqrt(a) = 2.0 | math.sqrt(a) = 2.0 | diff = 0.0 a = 5 | my_sqrt(a) = 2.2360679775 | math.sqrt(a) = 2.2360679775 | diff = 0.0 a = 6 | my_sqrt(a) = 2.44948974278 | math.sqrt(a) = 2.44948974278 | diff = 0.0 a = 7 | my_sqrt(a) = 2.64575131106 | math.sqrt(a) = 2.64575131106 | diff = 0.0 a = 8 | my_sqrt(a) = 2.82842712475 | math.sqrt(a) = 2.82842712475 | diff = 4.4408920985e-16 a = 9 | my_sqrt(a) = 3.0 | math.sqrt(a) = 3.0 | diff = 0.0 Modify your program so that it outputs lines for a values from 1 to 25 instead of just 1 to 9. You should submit a script file and a plain text output file (.txt) that contains the test output. Multiple file uploads are permitted. Your submission will be assessed using the following Aspects. Does the submission include a my_sqrt function that takes a single argument and includes the while loop from the instructions? Does the my_sqrt function initialize x and return its final value? Does the test_sqrt function print a values from 1 to 25? Does the test_sqrt function print the values returned by my_sqrt for each value of a? Does the test_sqrt function print correct values from math.sqrt for each value of a? Does the test_sqrt function print the absolute value of the differences between my_sqrt and math.sqrt for each value of a? Does the my_sqrt function compute values that are almost identical to math.sqrt ("diff" less than 1e-14)?

Respuesta :

Answer:

See explaination

Explanation:

import math

# Receive the input number from the user

def my_sqrt(x):

# Initialize the tolerance and estimate

tolerance = 0.000001

estimate = 1.0

# Perform the successive approximations

while True:

estimate = (estimate + float(x) / estimate) / 2

difference = abs(float(x) - estimate ** 2)

if difference <= tolerance:

break

diff = abs(math.sqrt(float(x))-estimate)

# Output the result

print("a = ",x," | my_sqrt(a) = ",estimate," | math.sqrt(a) = ",math.sqrt(float(x))," | diff = ",diff)

#print("The program's estimate is", estimate)

#print("Python's estimate is ", math.sqrt(float(x)))

x = 1

while(x<=25):

my_sqrt(x)

x+=1

Check attachment for output and screenshot

Ver imagen kendrich
Ver imagen kendrich

The function is an illustration of while loops

While loops are used to perform repetitive operations; just like the for loop

The function in Python, where comments are used to explain each line is as follows:

#This imports the math module

import math

#This defines the test_sqrt function    

def test_sqrt():

   #This initializes variable a to 1

   a = 1

   #The following is repeated for the values of variable a from 1 to 25

   while a<=25

       #This prints the required output

       print('a =', a,'| my_sqrt(a) =',my_sqrt(a),'| math.sqrt(a) =', math.sqrt(a),'| diff =', abs(math.sqrt(a)-my_sqrt(a)))

       #This increases a by 1

       a = a + 1

Read more about while loop at:

https://brainly.com/question/12736327