From a868efa661afd24c233b0ffcffd2830a7e9ea367 Mon Sep 17 00:00:00 2001 From: joemoorhouse Date: Tue, 12 Nov 2013 22:24:31 +0000 Subject: [PATCH] Add more comments. --- src/Numerics/Optimization/PowellMinimizer.cs | 31 +++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Numerics/Optimization/PowellMinimizer.cs b/src/Numerics/Optimization/PowellMinimizer.cs index 2d062d04..b3602c3b 100644 --- a/src/Numerics/Optimization/PowellMinimizer.cs +++ b/src/Numerics/Optimization/PowellMinimizer.cs @@ -39,6 +39,15 @@ namespace MathNet.Numerics.Optimization public readonly PowellOptions Options = new PowellOptions(); + /// + /// Non-Linear Least-Squares fitting the points (x,y) to a specified function of y : x -> f(x, p), p being a vector of parameters. + /// returning its best fitting parameters p. + /// + /// + /// + /// + /// Initial guess of parameters, p. + /// public double[] CurveFit(double[] x, double[] y, Func f, double[] pStart) { @@ -56,25 +65,31 @@ namespace MathNet.Numerics.Optimization return Minimize(function, pStart); } - public double[] Minimize(Func function, double[] p) + /// + /// Find the minimum of the supplied function using the Powell method. + /// + /// + /// + /// + public double[] Minimize(Func function, double[] pInitialGuess) { BrentMinimizer brentMinimizer = new BrentMinimizer(); + int n = pInitialGuess.Length; // number of dimensions // used in closure: - double[] point = new double[p.Length]; - double[] startingPoint = new double[p.Length]; - double[] direction = new double[p.Length]; + double[] point = new double[n]; + double[] startingPoint = new double[n]; + double[] direction = new double[n]; double lineMiniumum = 0; int functionCalls = 0; - Func functionAlongLine = (u) => + Func functionAlongLine = (p) => { for (int i = 0; i < point.Length; ++i) - point[i] = startingPoint[i] + direction[i] * u; + point[i] = startingPoint[i] + direction[i] * p; lineMiniumum = function(point); functionCalls++; return lineMiniumum; }; - int n = p.Length; // number of dimensions double fval; int iterations = 0; @@ -88,7 +103,7 @@ namespace MathNet.Numerics.Optimization direc[i] = new double[n]; direc[i][i] = 1.0; } - double[] x = p; + double[] x = pInitialGuess; double[] x1 = (double[])x.Clone(); brentMinimizer.Options.FunctionTolerance = Options.PointTolerance * 100;