Question

In: Computer Science

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 IN PYTHON AND WIT PSEUDOCODE!!! AS SIMPLE AS POSSIBLE!!!!

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

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code


PSEUDO-CODE FOR THE METHOD CALCULATING SQUARE ROOT

MODULE square_root(num)
   DECLARE LG=1.0
   DECLARE NG=(LG+(num/LG))/2
   WHILE ABS(LG-NG) >= 0.00001 DO
      LG=NG
      NG=(LG+(num/LG))/2
   END WHILE
   RETURN LG
END MODULE

#PYTHON CODE FOR THIS PROBLEM INCLUDING TESTING

#required method to estimate the square root of num
def square_root(num):
    #initializing 1 as initial guess
    LG=1.0
    #calculating second term from LG
    NG=(LG+(num/LG))/2
    #looping until the difference of prev and next is too small
    #(less than 0.00001)
    #note: use smaller values for more accurate results.
    while abs(LG-NG)>=0.00001:
        #storing current NG value as LG
        LG=NG
        #finding the next term
        NG = 0.5*(LG+ (num / LG))
    #returning the value of LG at the end.
    return LG


#main method for testing
def main():
    #reading a value
    n=float(input("Enter a number: "))
    #displaying square root
    print('Square root is',square_root(n))

#calling main()
main()

#OUTPUT

Enter a number: 5
Square root is 2.2360688956433634


Related Solutions

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
determine the range and domain of the following: the function k defined by k(x)= square root...
determine the range and domain of the following: the function k defined by k(x)= square root x-3
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...
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,∞) ?
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...
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
Deduce from the Completeness Axiom that there exists a square root of a real number a...
Deduce from the Completeness Axiom that there exists a square root of a real number a if and only if a ≥ 0
Estimate the area A between the graph of the function f(x)= square root of x and...
Estimate the area A between the graph of the function f(x)= square root of x and the interval [0,49]. Use an approximation scheme with n=2,5, and 10 rectangles. Use the right endpoints. Round your answers to three decimal places. A2= A5= A10= Click
A positive number is added to the reciprocal of its square root. Use Calculus methods to...
A positive number is added to the reciprocal of its square root. Use Calculus methods to answer the following questions:what if anything, is the smallest such sum? What, if any, positive x-values will yield the smallest sum? Give exact answers as well as answers accurate to Five decimal place. Be sure to give a mathematical justification that you answer represents a minimum and not a maximum.
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x....
An investor's utility function for money (Bernoulli utility function) is the square root of money: u(x)=√x. Her decision making can be modeled by assuming that she maximizes her expected utility. Her current wealth is 100. (All quantities are in hundreds of dollars.) She has the opportunity to buy a security that either pays 8 (the "good outcome") or loses 1 (the "bad outcome"). She can buy as many units as she wishes. For example, if she buys 5 units, she...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT