SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Newton–Raphson Method : algorithm for finding roots
  2. # ragulbalaji19
  3.  
  4. import math
  5.  
  6. def f(x): return pow(math.e,-x) + x * pow(math.e,-x) - 0.5 # function we want to study
  7. def fprime(x): return -(x * pow(math.e, -x)) # first derivative of said function
  8.  
  9. starting = [-0.5,0.0,0.5,3.0,3.449,3.5,5.0,6.0,7.0,10.0,40.0] # some starting points to start the process
  10.  
  11. for pt in starting:
  12.     print("starting @",pt)
  13.     ptn = pt
  14.     for i in range(100): # TIMEOUT ARBITRARILY SET AT 100
  15.         ptnold = ptn
  16.         try:
  17.             ptn = ptn - (f(ptn)/fprime(ptn))
  18.         except Exception as e:
  19.             print("DED", e)
  20.             break;
  21.         print(ptn)
  22.         if abs(ptnold - ptn) < 0.0000001:
  23.             print("CONVERGED.")
  24.             break
  25.     print("---\n")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top