Exponential Fit


(J Wallace Dev) #1

Hi There

I’ve recently used Fit.Polynomial to calculate poly lines - and this worked really well.

I’m just wondering if there is something similar to calculate an Exponential fit?

I.e. fitting Y = Ae^rX to a set of given a set of x,y points?

Many Thanks


Curve fitting: Power
(Christoph Rüegg) #2

You can, by transforming it, similar to Linearizing non-linear models by transformation. Something along the lines of the following should work:

double[] Exponential(double[] x, double[] y,
    DirectRegressionMethod method = DirectRegressionMethod.QR)
{
    double[] y_hat = Generate.Map(y, Math.Log);
    double[] p_hat = Fit.LinearCombination(x, y_hat, method, t => 1.0, t => t);
    return new[] {Math.Exp(p_hat[0]), p_hat[1]}; 
}

Example usage:

double[] x = new[] { 1.0, 2.0, 3.0 };
double[] y = new[] { 2.0, 4.1, 7.9 };
double[] p = Exponential(x,y); // a=1.017, r=0.687
double[] yh = Generate.Map(x,k => p[0]*Math.Exp(p[1]*k)) // 2.02, 4.02, 7.98

(J Wallace Dev) #3

Thank you Christoph I will give that a go


(J Wallace Dev) #4

Yes that is working, thank you.

Just one more related question.

If the original data set has quite a large gap in x values that particular section of the resulting line is very straight (as you would imagine).

Have you any ideas or tips for extending the solution above so that the resulting line follows a smooth curve from beginning to end?

I’m thinking of some kind of interpolation perhaps? But not sure how to fit it in with the solution.

Many Thanks


(J Wallace Dev) #5

I managed to solve the above issue by putting the results of the exponential through an IInterpolation


#6

Hi Christoph,
in my case I have to fit (x, y) data to y = a + bExp(cx).
So far I was not able to transform it in a way it can be used. Could you please help me?

Thank you in advance,
Alexander