Question

In: Computer Science

Write a module that contains a function sqrt(y, tol=1e-6), which computes a square root of a...

Write a module that contains a function sqrt(y, tol=1e-6), which computes a square root of a number using Heron’s algorithm with guaranteed relative error less then tol. The module should run as a program that asks for user input and prints output when executed using run sqrt.py. Heron’s algorithms for finding x such that y = x^2 works as follows. First, you come up with an initial guess for x; think what it should be. Then, you update x using the following formula: xnew = 1/2 (xold + y/xold) . Computation ends once the relative deviation between x^2 and y is less then the required value.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

#required method
def sqrt(y, tol=1e-6):
    #starting with x=y/2
   
x=y/2
    #looping until difference between x*x and y is less than tol
   
while abs((x*x)-y)>tol:
        #finding new value for x, applying heron's equation
       
x=0.5*(x+(y/x))
   #returning the value for x
   
return x


#main program
if __name__ == '__main__':
    #reading a number
   
n=float(input('Enter a number: '))
    #printing the number and its square root
   
print('Square root of {} is {}'.format(n,sqrt(n)))

#output

Enter a number: 2

Square root of 2.0 is 1.4142135623746899


Related Solutions

Write a function double mysqrt( double x ) which computes the value of the square root...
Write a function double mysqrt( double x ) which computes the value of the square root of x using the bisection method. First you need to set the left and right bounds for x. If 0<x<1 then lt = x and rt = 1 and the sqrt(x) is somewhere in between. If x > 1 then lt = 1 and rt = x and sqrt(x) is somewhere in between. In a loop you need to compute the mid value between...
write a function to determine the square root of a number. The square root of a...
write a function to determine the square root of a number. The square root of a number can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for the next guess and LG stands for the last guess. The loop should repeat until the difference between NG and LG is less than 0.00001. Use an initial guess of 1.0. Write a driver program to test your square root function. I WANT THIS PROGRAM...
Java please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
Graph the square root of the following function. State the domain and range of the square root of the function.
Graph the square root of the following function. State the domain and range of the square root of the function.y = -2x + 4
find the y-intercept of the tangent line to ( y= (-1.4)/square root 6+8x) (note 6+8x all...
find the y-intercept of the tangent line to ( y= (-1.4)/square root 6+8x) (note 6+8x all of it under square root) at (3,−0.255603860169077) The y-intercept = ?????????
Assume that a machine includes a floating point (FP) unit, which contains an FP square root...
Assume that a machine includes a floating point (FP) unit, which contains an FP square root (FPSQR) instruction. Suppose we have the following measurements. Frequency of FP operations = 20%, Frequency of FPSQR = 4% Average CPI of FP operations = 4.0, CPI of FPSQR = 20 Average CPI of other instructions (non-FP instructions) = 1.4 What is the average CPI of the machine? If the average CPI of FP is decreased to 3, what is the average CPI of...
why are the domain and range of square root function restricted to [0,∞) ?
why are the domain and range of square root function restricted to [0,∞) ?
Which option below provides the best description of the relationship between a quadratic parent function and a square root parent function?
Which option below provides the best description of the relationship between a quadratic parent function and a square root parent function?  A. The square root function is the quadratic function reflected across the y-axis.  B. The quadratic function and square root functions have no inverse.  C. The square root function is the quadratic function reflected across the line y = x, with a limited domain.  D. The square root function is the quadratic function reflected across the x-axis
A2_1 Function and modular a) Write a module fun_math.py that contains three functions: cal_factorial(x) – receives...
A2_1 Function and modular a) Write a module fun_math.py that contains three functions: cal_factorial(x) – receives a positive integer “x” and returns the factorial of that number. list_multiples(number, length) – takes a non-negative integer “number” and a positive integer “length”, and returns a list of multiples of “number” up to “length”. For instance (2,3) should return [2,4,6], and (7,5) should return [7,14,21,28,35]. find_max(a_list) – takes a list of integers and returns the largest number. Note that no built-in functions can...
Find the domain of the function g(x) = square root of 3x+3
Find the domain of the function g(x) = square root of 3x+3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT