1

I have compiled multiple attempts at this and have failed miserably, some assistance would be greatly appreciated.

The function should have one parameter without using the print statement. Using Newton's method it must return the estimated square root as its value. Adding a for loop to update the estimate 20 times, and using the return statement to come up with the final estimate.

so far I have...

    from math import *

    def newton_sqrt(x):    
        for i in range(1, 21)
            srx = 0.5 * (1 + x / 1)
        return srx

This is not an assignment just practice. I have looked around on this site and found helpful ways but nothing that is descriptive enough.

HTC One
  • 23
  • 1
  • 6
  • 2
    Your basic problem appears to be that you don't understand how to use Newton's method to calculate a square root. I don't see any squaring in your function, and you're repeating exactly the same calculation twenty times with the same values. – kindall Sep 16 '13 at 22:25
  • http://stackoverflow.com/questions/12850100/finding-the-square-root-using-newtons-method-errors – Tyler Sep 16 '13 at 22:27
  • I'm trying to learn how to go about coding this type of square-root function. – HTC One Sep 16 '13 at 22:33

6 Answers6

2

This is an implementation of the Newton's method,

def newton_sqrt(val):
    def f(x):
        return x**2-val
    def derf(x):
        return 2*x
    guess =val
    for i in range(1, 21):
        guess = guess-f(guess)/derf(guess)
        #print guess
    return guess

newton_sqrt(2)

See here for how it works. derf is the derivative of f.

gg349
  • 21,996
  • 5
  • 54
  • 64
1

I urge you to look at the section on Wikipedia regarding applying Newton's method to finding the square root of a number.

The process generally works like this, our function is

f(x) = x2 - a

f'(x) = 2x

where a is the number we want to find the square root of.

Therefore, our estimates will be

xn+1 = xn - (xn2 - a) / (2xn)

So, if your initial guess is x<sub>0</sub>, then our estimates are

x1 = x0 - (x02 - x) / (2x0)

x2 = x1 - (x12 - x) / (2x1)

x3 = x2 - (x22 - x) / (2x2)

...

Converting this to code, taking our initial guess to be the function argument itself, we would have something like

def newton_sqrt(a):
    x = a  # initial guess 
    for i in range(20):
        x -= (x*x - a) / (2.0*x)  # apply the iterative process once
    return x  # return 20th estimate

Here's a small demo:

>>> def newton_sqrt(a):
...     x = a
...     for i in range(20):
...         x -= (x*x - a) / (2.0*x)
...     return x
... 
>>> newton_sqrt(2)
1.414213562373095
>>> 2**0.5
1.4142135623730951
>>>
>>> newton_sqrt(3)
1.7320508075688774
>>> 3**0.5
1.7320508075688772
arshajii
  • 127,459
  • 24
  • 238
  • 287
0

In your code you are not updating x (and consequently srx) as you loop.

Noah Hafner
  • 341
  • 2
  • 5
0

One problem is that x/1 is not going to do much and another is that since x never changes all the iterations of the loop will do the same.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
0

You probably want something more like:

def newton_sqrt(x):    
    srx = 1
    for i in range(1, 21):
        srx = 0.5 * (srx + x/srx)
    return srx

newton_sqrt(2.)
# 1.4142135623730949

This both: 1) updates the answer at each iteration, and 2) uses something much closer to the correct formula (ie, no useless division by 1).

tom10
  • 67,082
  • 10
  • 127
  • 137
  • 1
    When I try it the solution is correct. I pasted in an example. (In my first version, I forgot to include `srx - (...)`, maybe you tried that one?) – tom10 Sep 16 '13 at 22:34
  • Yes, rather than do a full implementation, I tried to do the math similar to the reduction you used in the question. In doing that, I originally made a sign error. (I thought it would be easiest to see where your approach went wrong if you saw an answer that was similar.) – tom10 Sep 16 '13 at 22:55
  • Yes, I knew I was close but just couldn't quite get around it. Very helpful response. – HTC One Sep 16 '13 at 23:01
0

Expanding on your code a bit, you could add a guess as a parameter

from math import *

def newton_sqrt(x, guess):
    val = x

    for i in range(1, 21):
        guess = (0.5 * (guess + val / guess));
    return guess

print newton_sqrt(4, 3) # Returns 2.0