I'm trying to return the smallest value for the square root of k using Newton's method.
k=float(input("Number? "))
x = k/2
def newton(x):
while abs(x**(1/2)- k) >= 10**(-10):
if k >= 0:
x = (x+k/x)/(2)
return x
elif k < 0:
raise ValueError ("Cannot take the square root of a negative number")
print ("The approximate square root of", k, "is", newton(k))
print ("The error is", abs(x**(1/2)- k))
However, the above code only returns the first iteration. For example, if k is 2, the accurate square root by Newton's method should be 1.41422, which is the third iteration. However, the code is currently returning 1.5, the first iteration. How do I return the more accurate square root of 1.41422 instead of 1.5? Likewise, the error needs to reflect this change.
return x(withx = 1.5) is hit?