scipy 1.8.0 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
root_scalar(f, args=(), method=None, bracket=None, fprime=None, fprime2=None, x0=None, x1=None, xtol=None, rtol=None, maxiter=None, options=None)

Notes

This section describes the available solvers that can be selected by the 'method' parameter.

The default is to use the best method available for the situation presented. If a bracket is provided, it may use one of the bracketing methods. If a derivative and an initial value are specified, it may select one of the derivative-based methods. If no method is judged applicable, it will raise an Exception.

Parameters

f : callable

A function to find a root of.

args : tuple, optional

Extra arguments passed to the objective function and its derivative(s).

method : str, optional

Type of solver. Should be one of

  • 'bisect' (see here) <optimize.root_scalar-bisect>

  • 'brentq' (see here) <optimize.root_scalar-brentq>

  • 'brenth' (see here) <optimize.root_scalar-brenth>

  • 'ridder' (see here) <optimize.root_scalar-ridder>

  • 'toms748' (see here) <optimize.root_scalar-toms748>

  • 'newton' (see here) <optimize.root_scalar-newton>

  • 'secant' (see here) <optimize.root_scalar-secant>

  • 'halley' (see here) <optimize.root_scalar-halley>

bracket: A sequence of 2 floats, optional :

An interval bracketing a root. :None:None:`f(x, *args)` must have different signs at the two endpoints.

x0 : float, optional

Initial guess.

x1 : float, optional

A second guess.

fprime : bool or callable, optional

If fprime is a boolean and is True, f is assumed to return the value of the objective function and of the derivative. fprime can also be a callable returning the derivative of f. In this case, it must accept the same arguments as f.

fprime2 : bool or callable, optional

If fprime2 is a boolean and is True, f is assumed to return the value of the objective function and of the first and second derivatives. fprime2 can also be a callable returning the second derivative of f. In this case, it must accept the same arguments as f.

xtol : float, optional

Tolerance (absolute) for termination.

rtol : float, optional

Tolerance (relative) for termination.

maxiter : int, optional

Maximum number of iterations.

options : dict, optional

A dictionary of solver options. E.g., k , see show_options() for details.

Returns

sol : RootResults

The solution represented as a RootResults object. Important attributes are: root the solution , converged a boolean flag indicating if the algorithm exited successfully and flag which describes the cause of the termination. See RootResults for a description of other attributes.

Find a root of a scalar function.

See Also

root

Find a root of a vector function.

show_options

Additional options accepted by the solvers

Examples

Find the root of a simple cubic

>>> from scipy import optimize
... def f(x):
...  return (x**3 - 1) # only one real root at x = 1
>>> def fprime(x):
...  return 3*x**2

The brentq method takes as input a bracket

>>> sol = optimize.root_scalar(f, bracket=[0, 3], method='brentq')
... sol.root, sol.iterations, sol.function_calls (1.0, 10, 11)

The newton method takes as input a single point and uses the derivative(s)

>>> sol = optimize.root_scalar(f, x0=0.2, fprime=fprime, method='newton')
... sol.root, sol.iterations, sol.function_calls (1.0, 11, 22)

The function can provide the value and derivative(s) in a single call.

>>> def f_p_pp(x):
...  return (x**3 - 1), 3*x**2, 6*x
>>> sol = optimize.root_scalar(f_p_pp, x0=0.2, fprime=True, method='newton')
... sol.root, sol.iterations, sol.function_calls (1.0, 11, 11)
>>> sol = optimize.root_scalar(f_p_pp, x0=0.2, fprime=True, fprime2=True, method='halley')
... sol.root, sol.iterations, sol.function_calls (1.0, 7, 8)
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.optimize scipy.optimize._optimize.show_options scipy.optimize._root_scalar.root_scalar scipy.optimize._zeros_py.newton

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/optimize/_root_scalar.py#61
type: <class 'function'>
Commit: