polykin.math.roots¤
rootvec_qnewton ¤
rootvec_qnewton(
f: Callable[[FloatVector], FloatVector],
x0: FloatVector,
*,
tolx: float = 4e-11,
tolf: float = 6e-06,
sclx: FloatVector | None = None,
sclf: FloatVector | None = None,
maxiter: int = 100,
maxlenfac: float = 1000.0,
trustlen: float | None = None,
ndigit: int | None = None,
global_method: (
Literal["line-search", "dogleg"] | None
) = "line-search",
broyden_update: bool = False,
jac: Callable[[FloatVector], FloatMatrix] | None = None,
jac0: FloatMatrix | None = None,
verbose: bool = False
) -> VectorRootResult
Find the root of a system of nonlinear equations using a quasi-Newton method with optional global strategies.
This function implements a quasi-Newton solver for systems of nonlinear equations according to Dennis and Schnabel (1996). The user can choose the approach to calculate and update the Jacobian approximation, as well as the global strategy to improve convergence from remote starting points.
The default settings are meant to favor the likelihood of convergence at the expense of computational efficiency. For situations where maximum efficiency is desired and the initial guess is known to be close to the root, consider disabling the global method and using Broyden's update for the Jacobian.
Note
Solving systems of nonlinear equations is a surprisingly complex task — often more difficult than solving systems of differential equations or even multivariate optimization problems. Convergence is guaranteed only when the initial guess is sufficiently close to the root, which is rarely true in practice. The choice of a good initial guess, appropriate scaling factors, and a suitable global strategy is an essential part of solving the problem.
References
- J.E. Dennis Jr., R.B. Schnabel, "Numerical Methods for Unconstrained Optimization and Nonlinear Equations", SIAM, 1996.
| PARAMETER | DESCRIPTION |
|---|---|
f
|
Function whose root is to be found.
TYPE:
|
x0
|
Initial guess for the root. Moreover, if no user-defined scale
TYPE:
|
tolx
|
Tolerance for the scaled step size. The algorithm terminates when the
scaled distance between two successive iterates
TYPE:
|
tolf
|
Tolerance for the scaled residual norm. This is the main convergence
criterion. The algorithm terminates when the infinity norm of the scaled
function values
TYPE:
|
sclx
|
Positive scaling factors for the components of
TYPE:
|
sclf
|
Positive scaling factors for the components of
TYPE:
|
maxiter
|
Maximum number of outer quasi-Newton iterations.
TYPE:
|
maxlenfac
|
Factor determining the maximum allowable scaled step length
TYPE:
|
trustlen
|
Initial trust region radius for the
TYPE:
|
ndigit
|
Number of reliable digits returned by
TYPE:
|
global_method
|
Global strategy to improve convergence from remote starting points. With
TYPE:
|
broyden_update
|
If
TYPE:
|
jac
|
Function to compute the Jacobian of
TYPE:
|
jac0
|
Initial Jacobian approximation at
TYPE:
|
verbose
|
Print iteration information.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
VectorRootResult
|
Dataclass with root solution results. |
Examples:
Find the steady-state concentration of species A, B, and C at the outlet of a CSTR, assuming a consecutive scheme of type A+B→C, C+B→D.
>>> from polykin.math import rootvec_qnewton
>>> import numpy as np
>>> def f(x, A0=1.0, B0=2.0, C0=0.0, k1=1e-3, k2=5e-4, tau=1e3):
... "Steady-state mole balances: inflow - outflow ± reaction"
... A, B, C = x
... f = np.zeros_like(x)
... f[0] = (A0 - A)/tau - k1*A*B
... f[1] = (B0 - B)/tau - k1*A*B
... f[2] = (C0 - C)/tau + k1*A*B - k2*C*B
... return f
>>> sol = rootvec_qnewton(f, np.array([0.5, 1.0, 0.5]))
>>> sol.x
array([0.41421356, 1.41421356, 0.34314575])
Source code in src/polykin/math/roots/vector.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |