polykin.math.optimization¤
fmin_secant ¤
fmin_secant(
f: (
Callable[[float], float]
| Callable[[complex], complex]
),
x0: float,
x1: float,
*,
tolx: float = 1e-10,
tolg: float = 1e-05,
maxiter: int = 50,
epsf: float | None = None,
diff_scheme: Literal[
"centered", "complex"
] = "centered",
callback: (
Callable[
[int, float, float, float], tuple[bool, bool]
]
| None
) = None
) -> OptimumResult
Find the minimum of a scalar function using the secant method.
The secant method starts from two initial guesses and applies the secant update to the first derivative \(f'(x)\) to generate the next iterate:
The derivative \(f'(x)\) is approximated numerically using either centered finite
differences or complex-step differentiation. The complex-step scheme is usually
more accurate, but requires that f accepts complex-valued inputs.
This is an efficient local method for smooth functions, but it is less robust than bracketed methods such as Brent's algorithm and may fail if the initial guesses are poor.
| PARAMETER | DESCRIPTION |
|---|---|
f
|
Objective function to be minimized.
TYPE:
|
x0
|
First initial guess.
TYPE:
|
x1
|
Second initial guess.
TYPE:
|
tolx
|
Absolute tolerance for
TYPE:
|
tolg
|
Absolute tolerance for the function gradient. This is the primary convergence
criterion. The algorithm will terminate when
TYPE:
|
maxiter
|
Maximum number of iterations.
TYPE:
|
epsf
|
Machine precision of the function values. If
TYPE:
|
diff_scheme
|
Numerical differentiation scheme used to approximate
TYPE:
|
callback
|
Optional callback with signature
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OptimumResult
|
Dataclass with the results of the optimization. |
See Also
fmin_brent: More robust derivative-free minimization method for bounded intervals.
Examples:
Find the minimum of the function f(x) = x^4 - x + 1.
>>> from polykin.math import fmin_secant
>>> f = lambda x: x**4 - x + 1
>>> fmin_secant(f, 2.0, 1.0)
method: Secant
success: True
message: |f'(x)| ≤ tolg
nfeval: 16
niter: 6
x: 6.29961354e-01
f: 5.27529606e-01
df: 3.94747093e-06
Source code in src/polykin/math/optimization/secant.py
19 20 21 22 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 | |