View source code
Display the source code in std/numeric.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.numeric.findLocalMin

Find a real minimum of a real function f(x) via bracketing. Given a function f and a range (ax .. bx), returns the value of x in the range which is closest to a minimum of f(x). f is never evaluted at the endpoints of ax and bx. If f(x) has more than one minimum in the range, one will be chosen arbitrarily. If f(x) returns NaN or -Infinity, (x, f(x), NaN) will be returned; otherwise, this algorithm is guaranteed to succeed.

Tuple!(T,"x",Unqual!(ReturnType!DF),"y",T,"error") findLocalMin(T, DF) (
  scope DF f,
  in T ax,
  in T bx,
  in T relTolerance = sqrt(T.epsilon),
  in T absTolerance = sqrt(T.epsilon)
)
if (isFloatingPoint!T && __traits(compiles, () { T _ = DF.init(T.init); } ));

Parameters

NameDescription
f Function to be analyzed
ax Left bound of initial range of f known to contain the minimum.
bx Right bound of initial range of f known to contain the minimum.
relTolerance Relative tolerance.
absTolerance Absolute tolerance.

Preconditions

ax and bx shall be finite reals.
relTolerance shall be normal positive real.
absTolerance shall be normal positive real no less then T.epsilon*2.

Returns

A tuple consisting of x, y = f(x) and error = 3 * (absTolerance * fabs(x) + relTolerance).

The method used is a combination of golden section search and successive parabolic interpolation. Convergence is never much slower than that for a Fibonacci search.

References

"Algorithms for Minimization without Derivatives", Richard Brent, Prentice-Hall, Inc. (1973)

See Also

findRoot, isNormal

Example

import std.math : approxEqual;

auto ret = findLocalMin((double x) => (x-4)^^2, -1e7, 1e7);
assert(ret.x.approxEqual(4.0));
assert(ret.y.approxEqual(0.0));

Authors

Andrei Alexandrescu, Don Clugston, Robert Jacques, Ilya Yaroshenko

License

Boost License 1.0.