/// <summary> /// Least-Squares fitting the points (x, y) to a logarithm /// y : x -> a + b * ln(x). /// </summary> public static IModelledFunction LogarithmFunc(double[] xArray, double[] yArray) { Tuple <double, double> parameters = Fit.Logarithm(xArray, yArray); double a = parameters.Item1; double b = parameters.Item2; Func <double, double> function = t => a + b * System.Math.Log(t); return(new LogarithmFunction(function, parameters)); }
public void FitsToLogarithmSameAsExcelTrendLine() { // X Y // 1 0.2 // 2 0.3 // 4 1.3 // 6 4.2 // -> y = -0.4338 + 1.9981*ln(x) var x = new[] { 1.0, 2.0, 4.0, 6.0 }; var y = new[] { 0.2, 0.3, 1.3, 4.2 }; var resp = Fit.Logarithm(x, y); Assert.AreEqual(-0.4338, resp.Item1, 1e-3); Assert.AreEqual(1.9981, resp.Item2, 1e-3); var resf = Fit.LogarithmFunc(x, y); foreach (var z in Enumerable.Range(-3, 10)) { Assert.AreEqual(-0.4338 + 1.9981 * Math.Log(z), resf(z), 1e-2); } }