Ok, I am writing a program to find the root of f(x)= x - cos(x) using Newton-Raphson method. I implemented a loop and have calculated the root with no problems. However I want to break the loop when the difference between iterations = 10^-8. how exactly can I do this. I was thinking of using an if statement i.e. when x - x(previous) <= 10**8, break, print x but don't know how to refer to the seperate iterations!!! PLEASE HELP.
Program text:

from math import *

def newton(x):
for i in range (20):
x = x - ((x-cos(x))/(1 + sin(x)))

Recommended Answers

All 11 Replies

simply introduce another variable, e.g. "y". keep track of the current value and also the value of the previous iteration, just make sure you set each of the values at appropriate times.

Thanks for your advice, but could you be a bit more explicit? where exactly should I assign y?? i'm sorry but I really havent got alot of practice at this!!

perhaps you can make your life easier by naming your variables a little more meaningfully, e.g. "current", "previous". In any case, during your iterations, at some stage you want "previous" to store the value of the previous iteration's value, while "current" will get updated. This then allows you to compare the 2 values and assess whether to break, or continue looping.

guys I still cant figure it out...examples?

loop:
x = some calculation
if (x > y):
break
y = x
#endLoop

ok this is what i have;

from math import *

x=input("What value will you give x?:")
n=input("Please set a maximium amount of iterations?:")
for i in range (n):
    x = x - ((x-cos(x))/(1 + sin(x)))
    break
    y =x
    if (y-x) <= 10**-8:
        print y
    elif (y-x) > 10**-8:
        continue

still not working..advice??

Take your time and think through what is happening. You have a break, which has no condition, and so will ALWAYS be called. This means the code after the break is not going to be called.

ok this is what i have;
from math import *

x=input("What value will you give x?:")
n=input("Please set a maximium amount of iterations?:")
for i in range (n):
x = x - ((x-cos(x))/(1 + sin(x)))
break
y =x
if (y-x) <= 10**-8:
print y
elif (y-x) > 10**-8:
continue

still not working..advice??

Please use code tags when posting code in this forum. To use code tags wrap your code in these guys:
[code=python] # Code in here

[/code]

Just to be nice I'll wrap your code for you:

from math import *

x=input("What value will you give x?:")
n=input("Please set a maximium amount of iterations?:")
for i in range (n):
    x = x - ((x-cos(x))/(1 + sin(x)))
    break
    y =x
    if (y-x) <= 10**-8:
        print y
    elif (y-x) > 10**-8:
        continue

See how much nicer that looks? And indentation is not lost!

So first thing: remove the break and the continue statements.
Next, y = x should be assigned at the end of your loop.

That should get you a little closer

x = initial_value
x_last = x

threshold = minimum_change
MAXITER = maximum_iterations_desired
iterations = 0

while iterations < MAXITER and x - x_last > threshold:
    x_last = x
    x = iterate(x)

# x now holds your result

Using break and continue is overcomplicating things, IMO.

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.