Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Newtons method python loop
New Posts  All Forums:Forum Nav:

Newtons method python loop

post #1 of 13
Thread Starter 
Hey guys so ive been working on a program that solves for zeroes using newtons method, but i cant get the while loop working, im confused on how i could actually do it. Using a while loop could i set the value of newtons method to x1 then loop it again and that will equal x2, if x1 == x2 print x1, if not then redo the loop etc. please help and thanks for all input biggrin.gif
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
post #2 of 13
Post whatever code you got so far and I'll be glad to help.

Meanwhile I will try to dig up my Python implementation of Newton's method, I used it in Numeric Methods class about a year ago.

Edit: Also remember that even 32 bit computers are very precise, so X2 == X1 is going to take a long time to happen (if it even does), its much better to compare
Code:
if X2 - X1 < precision

where you specify the precision that suits your needs, generally something like 0.0000001 should be pretty much on the spot for any academic stuff.
Edited by Chris++ - 11/24/11 at 10:41pm
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #3 of 13
Thread Starter 
thank you, i have to get my code off of my other computer so ill post it in a bit, and im using the round function because of that reason
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
post #4 of 13
Thread Starter 
Ok so im not sure how to actually write the code for the loop
Code:
import sympy 

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) 


   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})
   func1dsub = func1d.subs({'x':x})

   func1sub = round(func1sub)
   func1dsub = round(func1sub)
   func1sub = float(func1sub)
   func1dsub = float(func1dsub) 

   round(func1sub,a)
   round(func1dsub,a)
   

   x1 = 0
   x2 = 0 

    
   while x != x1:

      
      func1sub = func1.subs({'x':x})
      func1dsub = func1d.subs({'x':x})
      n = x - (func1sub/func1dsub)
      
      

      break  

main()
]
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
post #5 of 13
Like I said before, its going to take an EXTREMELY long time for X1 to be equal to X2, so try adding a precision limit.
Code:
if abs(X2 - X1) < precision

Also, in your loop, you're putting the latest result in the variable n, a variable that is never checked in the loop (use X2).

Give it another try and if you really can't sort it out I'll post you my code.
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #6 of 13
Thread Starter 
ok so for the persision what do you think would be a good number that wouldnt be too far off but wouldnt take a long time to computer i was thinking something like .3, also i cant get the loop to work could you help me out please? thank you
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
post #7 of 13
Thread Starter 
hmm actually i think i got something working my only problem is it takes a long time
Code:
import sympy 

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) 


   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})
   func1dsub = func1d.subs({'x':x})

   func1sub = round(func1sub)
   func1dsub = round(func1sub)
   func1sub = float(func1sub)
   func1dsub = float(func1dsub) 

   round(func1sub,a)
   round(func1dsub,a)
   

   x1 = 0
   x2 = 0

   num1 = 0
   num2 = 0

    
   while x != x1:

      
      func1sub = func1.subs({'x':x})
      func1dsub = func1d.subs({'x':x})
      n = x - (func1sub/func1dsub)
      n = x1
      n = x

      func1sub = func1.subs({'x':x})
      func1dsub = func1d.subs({'x':x})
      n = x - (func1sub/func1dsub)
      n = x2
      
      
      if abs(x1 - x2) < .03:
         print x1 

main()

Edited by UnrealX - 11/30/11 at 11:04am
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
post #8 of 13
For the precision, .0001 is realistic, its more than enough for academic use and its fast enough to be done pretty much instantly (to us humans at least).

I'll help you out by fixing your code since its been a couple of days wink.gif
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #9 of 13
Thread Starter 
ohh ok and thank you very much i really appreciate it =)
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
Muh Build
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 955 Black Edition [3.2 GHz] GIGABYTE GA-990FXA-UD3 AM3+ Asus GTX 460 1 gig G.Skill 460 4 GB DDR3 
Hard DriveOSMonitorPower
Deskstar 320 gig Ubuntu 11.04 & Windows 7 HP w1907 Antec 620 Watt 
Case
NZXT PHANTOM 
  hide details  
Reply
post #10 of 13
Digging in my laptop I found this one that is just for square roots, I'm sure I have one where I make the f(x) and df(x) functions but I need to search for it (my laptop is a mess ehehe).
Code:
def newton(n):
    x = n/2.0
    dx = (x + n/x)/2.0
    while dx != x:
        x = dx
        dx = (x + n/x)/2.0
    return x
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Newtons method python loop