Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The function sqrt from the header file cmath can be used to find the square root

ID: 3814191 • Letter: T

Question

The function sqrt from the header file cmath can be used to find the square root of a nonnegative real number. Using Newton’s method, you can also write an algorithm to find the square root of a nonnegative real number within a given tolerance as follows: Suppose x is a nonnegative real number, a is the approximate square root of x, and epsilon is the tolerance. Start with a = x.

If |a2– x| <= epsilon, then a is the square root of x within the tolerance, otherwise:

Replace a with (a2 + x) / (2a) and repeat Step a in which |a2– x| denotes the absolute value of a2– x.

Write a recursive function to implement this algorithm to find the square root of a nonnegative real number. Also, write a program to test your function.

Explanation / Answer

solution--

save the file as test.py and run it from command line as python test.py


x=100 # input number
a=x
eps=1 # you can change the error
while (a**2-x)>eps:
   a=(a**2+x)/(2*a)
print(a)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote