I need to write a program that implements Newton's method ((guess + x/guess) / (2)). The program should prompt the user for the value to find the squareroot of (x) and the number of times to improve the guess. Starting with a guess value of x/2, your program should loop the specified number of times applying newton's method and report the final value of guess. You should also subtract your estimate from the value of math.sqrt() to show how close it is.

So far.. I got:

import math

def main():
    value, guess = input("Please enter the value, and the number of times to loop: ")

    while(
        top = guess + value / guess
        final_value = float(top) / 2

    close = final_value - math.sqrt(x)
    close_two = math.sqrt(x)

    print "The guess is", final_value, "which is", close, "away from", close_two

main()

I'm so lost... could someone please help me?

Recommended Answers

All 4 Replies

I don't know if The formula is right (check and change it), but my aim is to show you how to do it rather than accurate answer. So try to poke around :)

import math

def main():
     value = float(raw_input("Please enter the Number: \n "))
     freq = float(raw_input("Please the number of times to loop: \n"))
     # I assume the formula is formula is ((freq + value/freq) / (2))
     container = freq
     t = True
     while t:
          final = (freq + value/freq) /2
          print final         
          container = container - 1
          print container
          if container == 0:
               t = False
     math_value = math.sqrt(value)     
     print "final guess answer is: %f Compared to %f from math.sqrt()" %(final, math_value)

main()
Member Avatar for leegeorg07

i agree evstevemd however there is a problem with your code. if you look at carboniacid81's code you will see that he is using input() wich is an idicator that he is using python30 which doesnt use raw_input(). also it only loops to a single number e.g i used 69 and it only came out with 50.345 on 100 loops. other than that the code is good.

This program is faulty, after about 10 times it will go over original actual value and then cause incorrect sqrt.

You can use the more exact Newton's Method Python version from:
http://www.daniweb.com/software-development/python/code/444930/newtons-method-example-python

Here is the example for a squareroot solution ...

''' Newton_method2.py
find value for x in f(x) = 0 using Newton's method
'''

def derivative(f):
    def compute(x, dx):
        return (f(x+dx) - f(x))/dx
    return compute

def newtons_method(f, x, dx=0.000001, tolerance=0.000001):
    '''f is the function f(x)'''
    df = derivative(f)
    while True:
        x1 = x - f(x)/df(x, dx)
        t = abs(x1 - x)
        if t < tolerance:
            break
        x = x1
    return x

def f(x):
    '''
    here solve x for ...
    x*x - 7 = 0
    same as x = 7**0.5 = sqrt(7)
    '''
    return x*x - 7

x_approx = 1  # rough guess
# f refers to the function f(x)
x = newtons_method(f, x_approx)

print("Solve for x in x*x - 7 = 0  or  x = sqrt(7)")
print("%0.12f (x via Newton)" % x)
# compare with math.sqrt(7)
import math
print("%0.12f (x via sqrt(7))" % math.sqrt(7))

''' result ...
Solve for x in x*x - 7 = 0  or  x = sqrt(7)
2.645751311114 (x via Newton)
2.645751311065 (x via sqrt(7))
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.