polykin.math¤
root_secant ¤
root_secant(
f: Callable[[float], float],
x0: float,
x1: float,
xtol: float = 1e-06,
ftol: float = 1e-06,
maxiter: int = 50,
) -> RootResult
Find the root of a scalar function using the secant method.
Unlike the equivalent method in scipy, this method also terminates based on the function value. This is sometimes a more meaningful stop criterion.
PARAMETER | DESCRIPTION |
---|---|
f
|
Function whose root is to be found.
TYPE:
|
x0
|
Inital guess.
TYPE:
|
x1
|
Second guess.
TYPE:
|
xtol
|
Absolute tolerance for
TYPE:
|
ftol
|
Absolute tolerance for function value. The algorithm will terminate
when
TYPE:
|
maxiter
|
Maximum number of iterations.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RootResult
|
Dataclass with root solution results. |
Examples:
Find a root of the Flory-Huggins equation.
>>> from polykin.math import root_secant
>>> from math import log
>>> def f(x, a=0.6, chi=0.4):
... return log(x) + (1 - x) + chi*(1 - x)**2 - log(a)
>>> sol = root_secant(f, 0.3, 0.31)
>>> print(f"x= {sol.x:.3f}")
x= 0.213
Source code in src/polykin/math/solvers.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|