2

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.

1
  • 1
    What exactly do you expect will happen when, on the first iteration of the loop, return x (with x = 1.5) is hit? Commented Nov 6, 2012 at 23:00

1 Answer 1

9

You need to place your return statement outside your loop, otherwise it will always return on the first iteration:

def newton(x):
    while abs(x**(1/2)- k) >= 10**(-10):
        if k >= 0:
            x = (x+k/x)/(2)
        elif k < 0:
            raise ValueError ("Cannot take the square root of a negative number")
    return x

Note that using variables from the global scope like that is a recipe for disaster, it's unclear and means you can't use your function as easily. Pass it as a parameter instead.

It's also impossible for k to change inside the loop so you can do the check once at the beginning, rather than on every iteration:

def newton(x, k):
    if k < 0:
            raise ValueError ("Cannot take the square root of a negative number")
    while abs(x ** (1 / 2) - k) >= 10 ** (-10):
        x = (x + k / x) / 2
    return x
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.