The ancient Greeks knew that the iteration x_n+1 = 1/2 (x_n + 2/x_n) would conve
ID: 3784117 • Letter: T
Question
The ancient Greeks knew that the iteration x_n+1 = 1/2 (x_n + 2/x_n) would converge to squareroot2 if x_0 = 1. Write a Python program that takes a starting value as input and determines it the sequence converges or not. The sequence converges if |x_n+1 - x_n| lessthanorequalto 10^-7. If it doesn't converge in 100 iterations, then we'll say the sequence doesn't converge. The definition statement of your program should be def squareroot2(x0) Your output statement should be of the form: The sequence starting at X0X0X0 converges to XXXX in NNNN iterations. Or The sequence starting at X0X0X0 doesn't converge. where X0X0X0 is the initial value x_0, XXXX is the value the sequence converges to and NNNN is the number of iterations it takes to converge.Explanation / Answer
def sqrt2(x0):
eps = 0.0000001
ctr = 0
while True:
if ctr > 100:
print "The sequence starting at ", x0, " doesnt converge"
break
else:
ctr +=1
x1 = 1/2 * (x0 + (2/x0))
if abs(x1 - x0) <= eps:
print "The sequence starting at ", x0, " converges to ", abs(x1 - x0), " in ", ctr, " iterations"
break
x1 = x0
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.