0

I have been asking the community alot for help and i appreciate it all

So ive been working on a program that solves newtons method in python but for some reason its not working could someone look over it please? thanks you =)

import sympy
from collections import defaultdict

def main():
   dir(sympy)
   print ("NEWTONS METHOD")
   print ("Write your expression in terms of 'x' ")
   e = sympy.sympify(raw_input("input expression here: "))  
   f = sympy.Symbol('x')
   func1 = e
   func1d = sympy.diff(e,f) #takes the dirivative of the function
   print ("the dir of your function = "), func1d
   x = input("number to substitute for x: ")
   a = input("how many digits would you like to round to [recomended at least 4]") 
   func1sub = func1.subs({'x':x})   #substitutes the user given value of x into the equation
   func1dsub = func1d.subs({'x':x}) #substitutes the user given value of x into the equation
   func1sub = float(func1sub) 
   func1dsub = float(func1dsub)
   func1sub = round(func1sub)
   func1dsub = round(func1dsub)
   round(func1sub,a)
   round(func1dsub,a)
   n = x - (func1sub/func1dsub)
   x1 = 0
   x2 = 0 
   n = x - (func1sub/func1dsub)  
   x1 = n 
   x1 = round(x1) 
   n = x2 - (func1sub/func1dsub)
   x2 = n 
   x2 = round(x2)
   while 0 == 0:
      if abs(x1-x2) < .0001:
         print x1
         break
      else:
         n = x2 - (func1sub/func1dsub)
         x2 = n 
      if abs(x - n) < .03:
         print x
   if func1dsub == 0:  
      print ("ERROR CAN NOT DIVIDE BY 0") 
main()
Wilduck
  • 13,822
  • 10
  • 58
  • 90
Shantanu
  • 504
  • 5
  • 15
  • it hangs like its doing something after i run it, but nothing comes out. Ive been looking over it for a couple hours now and dont actually see what is wrong, im assuming its some syntax error or something im pretty new to python. – Shantanu Dec 05 '11 at 20:02
  • you should not substitute the value at each step of the loop? – Xavier Combelle Dec 05 '11 at 20:07
  • do you mean after every iteration add the substitute back in? – Shantanu Dec 05 '11 at 20:10
  • 1
    this seems to be better moved to [http://codereview.stackexchange.com/](http://codereview.stackexchange.com/) – moooeeeep Dec 05 '11 at 20:13
  • 1
    @moooeeeep, Code Review is strictly for working code. Any broken code posted there will be close or migrated back here. – Winston Ewert Dec 06 '11 at 03:46

1 Answers1

1

You're getting an infinite loop here:

while 0 == 0:

    if abs(x1-x2) < .0001:
        print x1
        break

    else:
        n = x2 - (func1sub/func1dsub)
        x2 = n 

    if abs(x - n) < .03:
        print x

The part of this loop which matters seems to be:

n = x2 - (func1sub/func1dsub)
x2 = n 

And your loop condition is abs(x1-x2) < .0001, so let's rewrite this:

while abs(x1 - x2) >= .0001:
    x2 -= (func1sub / func1dsub)
print x1

So maybe x2 -= (func1sub / func1dsub) is pushing x2 the wrong way. I'd add a print statement like this and make sure the values are actually converging:

while abs(x1 - x2) >= .0001:
    x2 -= (func1sub / func1dsub)
    print (x1, x2)

Also, I'm not that familiar with Newton's method, but in your code func1sub / func1dsub never changes, but shouldn't it change on every iteration?

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • @Shantanu also, use while True: instead of while 0 == 0: and take the dir(sympy) off your code - tis statement does nothing as it is. – jsbueno Dec 05 '11 at 20:12
  • @jsbueno - I think this should probably be a comment on the question. – Brendan Long Dec 05 '11 at 20:12
  • hm when i do this im getting wrong answers though, what does the -= do? is it like writing -x2 = (func1sub/func1dsub) – Shantanu Dec 05 '11 at 23:00
  • @Shantanu - `a -= b` is the same as `a = a - b` – Brendan Long Dec 06 '11 at 00:47
  • thanks for the help on the while loop, for some reason im getting some error with my code somewhere, when ever i run it now x1 is always equal to 2.0 no matter what equation and x2 just goes up by 1 everytime and doesnt stop – Shantanu Dec 06 '11 at 01:58
  • im not really understanding how the -= works with my equation, i have to put x into the equation to get n, set n to x and get n(2) etc. then i have to see what numbers i put in that is equal to the number i get out – Shantanu Dec 06 '11 at 02:20
  • @Shantanu - The loop in my answer is identical to your loop in the question. I suspect it's wrong though. The point of my answer was to show where to look to fix it. – Brendan Long Dec 06 '11 at 02:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5629/discussion-between-shantanu-and-brendan-long) – Shantanu Dec 06 '11 at 18:28