Can't find all roots in Newtons-Raphson method (Python) -
the code using work fine 2 of 4 roots of function, can't seem make work 4 roots.
from numpy import * scipy import * import numpy np def f(x): return np.arctan(2*(x - 1)) - np.log(abs(x)) def fprime(x): return (16/5)*((x - (5/4))**2)-1 def newtonraphson(x0,e=1.0e-3): val in range(1, 15): dx = - f(x0)/fprime(x0) print(dx) x0 = x0 + dx if abs(dx) < e: return x0,val print('too many iterations\n') root,numiter = newtonraphson(-1.0) print ('root =',root) print ('number of iterations =',numiter)
the roots should -0.300098, 0.425412, 1, 4.09946
when use -1.0, root -0.300098, , when use 1, root 1, can't seem other 2 roots. need code work? thanks
your code good, problem function - it's complicated method. can either use more sophisticated root finding method or can decrease dx
, increase number of iterations. instance can use dx/1000
, 1.5 million maximum iterations. give roots. roots 1 , 4.0996 have use close guess. code works simple functions simple quadratic functions.
Comments
Post a Comment