def newton(func, x0, fprime=None, args=(), tol=<NUM_LIT>, maxiter=<NUM_LIT:50>,<EOL>fprime2=None):
if tol <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>" % tol)<EOL><DEDENT>if fprime is not None:<EOL><INDENT>p0 = <NUM_LIT:1.0> * x0<EOL>fder2 = <NUM_LIT:0><EOL>for iter in range(maxiter):<EOL><INDENT>myargs = (p0,) + args<EOL>fder = fprime(*myargs)<EOL>if fder == <NUM_LIT:0>:<EOL><INDENT>msg = "<STR_LIT>"<EOL>warnings.warn(msg, RuntimeWarning)<EOL>return p0<EOL><DEDENT>fval = func(*myargs)<EOL>if fprime2 is not None:<EOL><INDENT>fder2 = fprime2(*myargs)<EOL><DEDENT>if fder2 == <NUM_LIT:0>:<EOL><INDENT>p = p0 - fval / fder<EOL><DEDENT>else:<EOL><INDENT>discr = fder ** <NUM_LIT:2> - <NUM_LIT:2> * fval * fder2<EOL>if discr < <NUM_LIT:0>:<EOL><INDENT>p = p0 - fder / fder2<EOL><DEDENT>else:<EOL><INDENT>p = p0 - <NUM_LIT:2>*fval / (fder + sign(fder) * sqrt(discr))<EOL><DEDENT><DEDENT>if abs(p - p0) < tol:<EOL><INDENT>return p<EOL><DEDENT>p0 = p<EOL><DEDENT><DEDENT>else:<EOL><INDENT>p0 = x0<EOL>if x0 >= <NUM_LIT:0>:<EOL><INDENT>p1 = x0*(<NUM_LIT:1> + <NUM_LIT>) + <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>p1 = x0*(<NUM_LIT:1> + <NUM_LIT>) - <NUM_LIT><EOL><DEDENT>q0 = func(*((p0,) + args))<EOL>q1 = func(*((p1,) + args))<EOL>for iter in range(maxiter):<EOL><INDENT>if q1 == q0:<EOL><INDENT>if p1 != p0:<EOL><INDENT>msg = "<STR_LIT>" % (p1 - p0)<EOL>warnings.warn(msg, RuntimeWarning)<EOL><DEDENT>return (p1 + p0)/<NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>p = p1 - q1*(p1 - p0)/(q1 - q0)<EOL><DEDENT>if abs(p - p1) < tol:<EOL><INDENT>return p<EOL><DEDENT>p0 = p1<EOL>q0 = q1<EOL>p1 = p<EOL>q1 = func(*((p1,) + args))<EOL><DEDENT><DEDENT>msg = "<STR_LIT>" % (maxiter, p)<EOL>raise RuntimeError(msg)<EOL>
Find a zero using the Newton-Raphson or secant method.

Find a zero of the function `func` given a nearby starting point `x0`.
The Newton-Raphson method is used if the derivative `fprime` of `func`
is provided, otherwise the secant method is used.  If the second order
derivate `fprime2` of `func` is provided, parabolic Halley's method
is used.

Parameters
----------
func : function
    The function whose zero is wanted. It must be a function of a
    single variable of the form f(x,a,b,c...), where a,b,c... are extra
    arguments that can be passed in the `args` parameter.
x0 : float
    An initial estimate of the zero that should be somewhere near the
    actual zero.
fprime : function, optional
    The derivative of the function when available and convenient. If it
    is None (default), then the secant method is used.
args : tuple, optional
    Extra arguments to be used in the function call.
tol : float, optional
    The allowable error of the zero value.
maxiter : int, optional
    Maximum number of iterations.
fprime2 : function, optional
    The second order derivative of the function when available and
    convenient. If it is None (default), then the normal Newton-Raphson
    or the secant method is used. If it is given, parabolic Halley's
    method is used.

Returns
-------
zero : float
    Estimated location where function is zero.

See Also
--------
brentq, brenth, ridder, bisect
fsolve : find zeroes in n dimensions.

Notes
-----
The convergence rate of the Newton-Raphson method is quadratic,
the Halley method is cubic, and the secant method is
sub-quadratic.  This means that if the function is well behaved
the actual error in the estimated zero is approximately the square
(cube for Halley) of the requested tolerance up to roundoff
error. However, the stopping criterion used here is the step size
and there is no guarantee that a zero has been found. Consequently
the result should be verified. Safer algorithms are brentq,
brenth, ridder, and bisect, but they all require that the root
first be bracketed in an interval where the function changes
sign. The brentq algorithm is recommended for general use in one
dimensional problems when such an interval has been found.