HTC One
HTC One

Reputation: 23

Python, square root function?

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.

Upvotes: 1

Views: 7018

Answers (6)

tom10
tom10

Reputation: 69182

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).

Upvotes: 0

arshajii
arshajii

Reputation: 129497

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

Upvotes: 1

Torgrim Brochmann
Torgrim Brochmann

Reputation: 269

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

Upvotes: 0

gg349
gg349

Reputation: 22671

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.

Upvotes: 2

Steve Barnes
Steve Barnes

Reputation: 28370

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.

Upvotes: 0

Noah Hafner
Noah Hafner

Reputation: 351

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

Upvotes: 0

Related Questions