Home > Back-end >  How to get the slope and intercept when using Fit.LineFunc from Math.net
How to get the slope and intercept when using Fit.LineFunc from Math.net

Time:05-06

I'm using Math.net to generate a lineal function from a set of data points, the code is simple:

Func<double, double> linetrend = MathNet.Numerics.Fit.LineFunc(x, y);

I can evaluate later in any point using linetrend, but I want to extract the slope and intercept calculate by Fit.LineFunc. In the debug I can see the values in the Target method but can get to extract them to a string variable.

any help would be appreciated. Thank you.

regards, Carlos

CodePudding user response:

That's the definition of the function:

/// <summary>
/// Least-Squares fitting the points (x,y) to a line y : x -> a b*x,
/// returning a function y' for the best fitting line.
/// </summary>
public static Func<double, double> LineFunc(double[] x, double[] y)
{
    (double intercept, double slope) = SimpleRegression.Fit(x, y);
    return z => intercept   slope * z;
}

It doesn't seem to store these values anywhere, but you can get them by calling the same function:

(double intercept, double slope) = SimpleRegression.Fit(x, y);
  • Related