1,105,625 Community Members

Newton Function

Member Avatar
siaosituimoloaublood
Newbie Poster
12 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i am in need of help with this homework. The instructions are as followed: Package Newton's method for approximating square roots in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute the square roots of inputs until the user presses the enter/return key. This is what I have so far:

import math

#Initialize tolerance
tolerance = 0.000001

def newton(x):
    """ Returns the square root of x """
    #Performs the successive approximations
    estimate = 1.0
    while True:
        estimate = (estimate + x / estimate) / 2
        difference = abs(x - estimate ** 2)
        if difference <= tolerance:     # Break out of loop if difference is less than tolerance
            break           
        return estimate     # While the difference value is > TOLERANCE, the process continues



def main():
    """Allows the user to obtain square roots."""
    while True:
        #Receive the input number from the user
        x = input("Enter a positive number or enter/return to quit: ")
        if x == "":     #if user presses "Enter" then exit the program
            break       # Otherwise, continue the process of allowing new numbers
        x = float(x)
        #Output the result
        print("The programs estimate of the square root of ", x, "is ", round(newton(x),2))
        print("Python's estimate: ", math.sqrt(x))
main()

I tested it out, but it seems like the calculation is not accurate, can anyone help me?

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

return should be in place of break, I think. Also rounding to 2 decimals does not make sense after so small tolerance for error, generally you also do not round, you just format the printing.

Member Avatar
sneekula
Nearly a Posting Maven
2,496 posts since Oct 2006
Reputation Points: 917 [?]
Q&As Helped to Solve: 263 [?]
Skill Endorsements: 5 [?]
 
2
 

line 15
return estimate
should be outside the loop

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
2
 

I would write the code like this ...

def newton_approx(x):
    """
    Newton's method to get the square root of x
    using successive approximations 
    """
    tolerance = 0.000001
    estimate = 1.0
    while True:
        estimate = (estimate + x / estimate) / 2
        difference = abs(x - estimate ** 2)
        if difference <= tolerance:
            break           
    return estimate

# test
x = 2
print("newton = %0.15f" % newton_approx(x))
print("x**0.5 = %0.15f" % (x**0.5))

'''
newton = 1.414213562374690
x**0.5 = 1.414213562373095
'''
Member Avatar
siaosituimoloaublood
Newbie Poster
12 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you everyone for your helpful inputs in solving my problem. The code now works since I moved the return outside of the loop. Now for phase 2 of my homework converting the current function to a recursive function. Any suggestions?

Question Answered as of 1 Year Ago by vegaseat, sneekula and pyTony
Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

I would add parameter for current difference and return value from function which reaches the level of tolerance (which also could be a parameter).

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article