0

I'm stuck in a very stupid point while reading Numeric Analysis.

So I have the following program in python. And I can't figure why I get these results.

Where do I use the i in heron(x,y) to get these results?

Because only the first one makes sense for me. Why are the numbers decreasing if the i isn't used at all at the function?

def heron(x,y):
    x=(x+y/x)*0.5
    return x

x=1
y=2
for i in range(5):
   x=heron(x,y)
   print('Approximation of square root : %.16f'%x)

And the results:

Approximation of square root :1.5000000000000000
Approximation of square root :1.4166666666666665
Approximation of square root :1.4142156862745097
Approximation of square root :1.4142135623746899
Approximation of square root :1.4142135623730949

Edit: The code was given by my professor in class and I guess the only use of it was to explain few basic things of Python?

10
  • 5
    Because the value of x is changing within your loop. Commented Nov 1, 2016 at 17:43
  • Don't use global variable names for input arguments for heron Commented Nov 1, 2016 at 17:43
  • @kiran.koduru what do you mean? Commented Nov 1, 2016 at 17:44
  • What is the purpose of this code? What is it meant to do? Commented Nov 1, 2016 at 17:44
  • 1
    @kiran.koduru I'm pretty sure that's not true. I tested a similar function in interactive mode def foo(x): x = x**2 return x and it doesn't mutate the value of a global x. Commented Nov 1, 2016 at 17:51

3 Answers 3

3

The line

for i in range(5):

only means:

Do the following five times.

The actual work is done in

x = heron(x,y)

which uses x as part of the arguments of heron and assigns the changed value back to it. So while y stays unchanged, x is changed with each call to heron. The changed x is then used as an argument to the next call.

Edit: I can't decide if this is a correct implementation because I don't know what algorithm you are trying to implement. But you only asked:

Why are the numbers decreasing if the i isn't used at all at the function?

Sign up to request clarification or add additional context in comments.

3 Comments

Better to use for _ in range(5) since i is never used
Oh I see! So at first I have x=1 but after the first loop it the x equals 1.5, is that correct?
Yes, that's the result of the first call to heron. 1.5 is assigned to x and used in the 2nd call.
0

You are trying to implement Heron's algorithm to find the root square of a number.

It is an iterative algorithm where you increase precision of the result at each step.

In your implementation, x is an initial solution and y is the number you want to find the root square.

You are doing 5 iterations and the variable i is not required to do your iterations. You can use _ to declare a non needed variable.

You could define a wanted precision and iterate a various number of times to reach the wanted precision.

def heron(x,y):
    x=(x+y/x)*0.5
    return x

x=1
y=2
numberOfIterations = 5
for _ in range(numberOfIterations):
    x=heron(x,y)
    print('Approximation of square root : %.16f'%x)

Comments

-1

I think, you must modify your code as follow :-

def heron(x,y):
    x=(x+y/x)*0.5
    return x

x=1
y=2
for i in range(5):
   z=heron(x,y)
   print 'Approximation of square root :%.16f'%z

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.