/// <summary> /// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, p2, x), /// returning its best fitting parameter p0, p1 and p2. /// </summary> public static Tuple <double, double, double> Curve(double[] x, double[] y, Func <double, double, double, double, double> f, double initialGuess0, double initialGuess1, double initialGuess2, double tolerance = 1e-8, int maxIterations = 1000) { return(FindMinimum.OfFunction((p0, p1, p2) => Distance.Euclidean(Generate.Map(x, t => f(p0, p1, p2, t)), y), initialGuess0, initialGuess1, initialGuess2, tolerance, maxIterations)); }
/// <summary> /// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x), /// returning its best fitting parameters as (a, b) tuple. /// </summary> public static Tuple <double, double> Logarithm(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR) { double[] lnx = Generate.Map(x, Math.Log); double[] p = LinearCombination(lnx, y, method, t => 1.0, t => t); return(Tuple.Create(p[0], p[1])); }
/// <summary> /// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p, x), /// returning its best fitting parameter p. /// </summary> public static double Curve(double[] x, double[] y, Func <double, double, double> f, double initialGuess, double tolerance = 1e-8, int maxIterations = 1000) { return(FindMinimum.OfScalarFunction(p => Distance.Euclidean(Generate.Map(x, t => f(p, t)), y), initialGuess, tolerance, maxIterations)); }
/// <summary> /// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x), /// returning its best fitting parameters as (a, b) tuple. /// </summary> public static (double A, double B) Logarithm(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR) { double[] lnx = Generate.Map(x, Math.Log); double[] p = LinearCombination(lnx, y, method, _ => 1.0, t => t); return(p[0], p[1]); }