Beginners Problem

Reply

Join Date: Dec 2008
Posts: 8
Reputation: mikewalsh89 is an unknown quantity at this point 
Solved Threads: 0
mikewalsh89 mikewalsh89 is offline Offline
Newbie Poster

Beginners Problem

 
0
  #1
Dec 14th, 2008
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)))
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Beginners Problem

 
0
  #2
Dec 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: mikewalsh89 is an unknown quantity at this point 
Solved Threads: 0
mikewalsh89 mikewalsh89 is offline Offline
Newbie Poster

Re: Beginners Problem

 
0
  #3
Dec 14th, 2008
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!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,356
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 125
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Beginners Problem

 
0
  #4
Dec 14th, 2008
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
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Beginners Problem

 
0
  #5
Dec 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: mikewalsh89 is an unknown quantity at this point 
Solved Threads: 0
mikewalsh89 mikewalsh89 is offline Offline
Newbie Poster

Re: Beginners Problem

 
0
  #6
Dec 14th, 2008
guys I still cant figure it out...examples?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Beginners Problem

 
0
  #7
Dec 14th, 2008
loop:
x = some calculation
if (x > y):
break
y = x
#endLoop
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: mikewalsh89 is an unknown quantity at this point 
Solved Threads: 0
mikewalsh89 mikewalsh89 is offline Offline
Newbie Poster

Re: Beginners Problem

 
0
  #8
Dec 14th, 2008
ok this is what i have;
  1. from math import *
  2.  
  3. x=input("What value will you give x?:")
  4. n=input("Please set a maximium amount of iterations?:")
  5. for i in range (n):
  6. x = x - ((x-cos(x))/(1 + sin(x)))
  7. break
  8. y =x
  9. if (y-x) <= 10**-8:
  10. print y
  11. elif (y-x) > 10**-8:
  12. continue
still not working..advice??
Last edited by vegaseat; Dec 17th, 2008 at 8:58 pm. Reason: added code tags!!!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Beginners Problem

 
0
  #9
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,356
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 125
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Beginners Problem

 
0
  #10
Dec 17th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC