| | |
Beginners Problem
![]() |
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
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)))
Program text:
from math import *
def newton(x):
for i in range (20):
x = x - ((x-cos(x))/(1 + sin(x)))
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.
break and continue might be what you need
if x==0 break --> this stops the Loop
y==1 continue --> this makes loop running
this might help:
http://www.network-theory.co.uk/docs...tatements.html
if x==0 break --> this stops the Loop
y==1 continue --> this makes loop running
this might help:
http://www.network-theory.co.uk/docs...tatements.html
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
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.
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
ok this is what i have;
still not working..advice??
python Syntax (Toggle Plain Text)
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
Last edited by vegaseat; Dec 17th, 2008 at 8:58 pm. Reason: added code tags!!!!
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.
![]() |
Similar Threads
- Good Book for beginners in C# (C#)
- Class Module VB.net Problem (VB.NET)
- help w/ problem (C++)
- vb.net NEWBIE!! Triangle problem!! (VB.NET)
- Symbian help (C++)
- BlueJ problem (Java)
- Prog to list ASCII codes (beginners' stuff) (C)
Other Threads in the Python Forum
- Previous Thread: Avoiding multiple openings of the same file in python
- Next Thread: urllib - check if web page exists
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input itunes java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite ssh statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






