Exponential Curve Fitting (y = -b*exp(-c*x) vs y = a - b*exp(-c*x)

1.3k Views Asked by At

I can solve this function : y = -bexp(-cx) with Exponential Model Regression Transformed for find coefficients and curve fitting

Like this video : https://www.youtube.com/watch?v=ielmPWcf1M4

But when i want to solve y = a - bexp(-cx) i cant find a value.

Also i tried :

ln(y - a) = ln(-b) - c*x and try same way with video and above equation, my final equation coefficients not fit with matlab or phyton equation and graphs.

I tried y = -bexp(-cx) solving c++ code is

//Exponential Fit
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
 int i, j, k, n;
 cout << "\nEnter the no. of data pairs to be entered:\n";        //To find the size of arrays
 cin >> n;
 double a, b, c;
 double* x = new double[n];
 double* y = new double[n];
 double* lny = new double[n];
 cout << "\nEnter the x-axis values:\n";                //Input x-values(observed)
 for (i = 0; i < n; i++)
     cin >> x[i];
 cout << "\nEnter the y-axis values:\n";                //Input y-values(observed)

 for (i = 0; i < n; i++)
     cin >> y[i];

 for (i = 0; i < n; i++)                        //Calculate the values of ln(yi)
     lny[i] = log(y[i]);
 double xsum = 0, x2sum = 0, ysum = 0, xysum = 0;                //variables for sums/sigma of xi,yi,xi^2,xiyi etc
 for (i = 0; i < n; i++)
 {
     xsum = xsum + x[i];                        //calculate sigma(xi)
     ysum = ysum + lny[i];                        //calculate sigma(yi)
     x2sum = x2sum + pow(x[i], 2);                //calculate sigma(x^2i)
     xysum = xysum + x[i] * lny[i];                    //calculate sigma(xi*yi)
 }
 a = -(n * xysum - xsum * ysum) / (n * x2sum - xsum * xsum);            //calculate slope(or the the power of exp)
 b = (x2sum * ysum - xsum * xysum) / (x2sum * n - xsum * xsum);            //calculate intercept
 c = -pow(2.71828, b);                        //since b=ln(c)

 double* y_fit = new double[n];    //an array to store the new fitted values of y    
 for (i = 0; i < n; i++)
     y_fit[i] = -c * pow(2.71828, -a * x[i]);                    //to calculate y(fitted) at given x points
 cout << "S.no" << setw(5) << "x" << setw(19) << "y(observed)" << setw(19) << "y(fitted)" << endl;
 cout << "-----------------------------------------------------------------\n";
 for (i = 0; i < n; i++)
     cout << i + 1 << "." << setw(8) << x[i] << setw(15) << y[i] << setw(18) << y_fit[i] << endl;//print a table of x,y(obs.) and y(fit.)    
 cout << "\nThe corresponding line is of the form:\n\nlny = " << a << "x + ln" << b << endl;
 cout << "\nThe exponential fit is given by:\ny = " << c << "e^" << a << "x\n";
 return 0;
}

Is anyone can help me understand solve y = a - bexp(-cx) and different with above 2 equation calculations.

Thanks.

2

There are 2 best solutions below

1
On

The three-parametered model can't be linearized. You need a more general tool, such as Levenberg-Marquardt.

Alternatively, you can set an arbitrary value to $a$ and fit the dataset $(x_i,y_i-a)$. From this you get a fitting error $\epsilon(a)$. Then use a 1D minimizer to find the best value of $a$.

0
On

You must take care that, by the end, you will need a nonlinear regression since what is measured is $y$ and not any of its possible tansforms.

The model is nonlinear because of $c$. So, consider that $c$ is fixed at a given value and define $t_i=e^{-c x_i}$ making the model to be $$y=a-b t$$ which is easily solved using linear regression. So, for this specific value of $c$, you have $a(c)$ and $b(c)$ and you can compute $SSQ(c)$ that you want to minimize.

If you have a solver for minimization, it is simple. If not, plot the graph of $SSQ(c)$ and try to see where is the minimum. Zoom more and more until you get the solution.

Another way could be to look for the zero of $\frac{d SSQ(c)}{dc}$ which you could solve using numerical derivatives