Beginners Problem

Reply

Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Beginners Problem

 
0
  #11
Dec 17th, 2008
Originally Posted by mikewalsh89 View Post
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:
  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
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
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 18
Reputation: Nuez_Jr is an unknown quantity at this point 
Solved Threads: 1
Nuez_Jr's Avatar
Nuez_Jr Nuez_Jr is offline Offline
Newbie Poster

Re: Beginners Problem

 
0
  #12
Jan 6th, 2009
  1. x = initial_value
  2. x_last = x
  3.  
  4. threshold = minimum_change
  5. MAXITER = maximum_iterations_desired
  6. iterations = 0
  7.  
  8. while iterations < MAXITER and x - x_last > threshold:
  9. x_last = x
  10. x = iterate(x)
  11.  
  12. # x now holds your result

Using break and continue is overcomplicating things, IMO.
Last edited by Nuez_Jr; Jan 6th, 2009 at 3:15 pm. Reason: Apparently [icode] doesn't have a language parameter ; )
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