I have written a code that will exponentially fit a given set of data-points.(Yfit=c*e^(a*x))

The procedure is based on least square approximation, which, in simple words,works by finding a line that is at a minimum distance possible from all the points.

Let’s say you have the x-axis points stored in a matrix, ‘x’ & the y-axis points stored in a matrix ‘y’. Then the following code returns the value of the coefficient, ‘c’ and ‘a’ such that the equation of the fitted line is y=c*e^(a*x), and also the fitted points which are basically the points of the y-axis obtained from the equation of the best fit-line.

CODE:

//Exponential Fitting
//To exponentially fit a given set of data-points.
//Written By: Manas Sharma(www.bragitoff.com)
funcprot(0);
function [f,a,c]=expofit(x,y)
n=size(x);
if n(2)>n(1) then
n=n(2)
else
n=n(1);
end
for i=1:n
Yln(i)=log(y(i));
end
xsum=0;
ysum=0;
xysum=0;
x2sum=0;
for i=1:n
xsum=x(i)+xsum;
ysum=Yln(i)+ysum;
x2sum=x(i)*x(i)+x2sum;
xysum=x(i)*Yln(i)+xysum;
end
a=(n*xysum-xsum*ysum)/(n*x2sum-xsum*xsum);
b=(x2sum*ysum-xsum*xysum)/(x2sum*n-xsum*xsum);
c=exp(b);
for i=1:n
f(i)=c*exp(a*x(i));
end
endfunction

c =
1.0433996
a =
0.6808532
yfit =
1.0433996
2.0613012
4.0722295
8.044944

We can now even plot the observed data-points and the fitted points(yfit) and compare the two.

plot2d(x,y,-5)
plot2d(x,yfit,5)

I have created a module in SCILAB which contains the above macro, and once installed can be used as an in-built function. You can download it from here: https://atoms.scilab.org/toolboxes/curvefit

Leave your questions/suggestion/corrections in the comments section down below and I’ll get back to you soon.

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.