# Hello, I am trying to fit some data points with an exponential equation.

16 views (last 30 days)
Krish on 13 Dec 2012
Hi,
I have a set of experimental X and Y values. I am trying to fit an exponential equation through these data points by minimizing the sum of the squares between the experimental Y and Y from the equation. The exponential function has two constants that need to changed to minimize the error. It is very similar to using the solver function in excel, where you give a starting guess value and the solver tries to find a solution hat satisfies the minimal error / objective.
Sorry, I am a first time Matlab user, hence need some extra help. Thanks in advance.

Matt Kindig on 13 Dec 2012
Hi Krish,
There are a couple of different ways to do this. Do you have the Optimization Toolbox or Curve Fitting Toolbox? If so, type
ver
at the Matlab prompt '>>' and see if the Optimization Toolbox or Curve Fitting Toolbox are listed. Both make this sort of thing quite a bit easier, and I would use these toolboxes if available.
If all you have is basic Matlab, you can do it like this:
%x and y are your variables
expfn = @(p,xd) p(1)*exp(p(2)*xd); %define exponential function
errfn = @(p) sum((expfn(p,x)-y).^2); %define sum-squared error
pfit = fminsearch( errfn, [0 0]); %run the minimizer
plot(x,y,'bo'); hold on; %plot your raw data
plot(x, expfn(pfit, x), 'r-'); %plot the fit data
To understand this, you should search the documentation on anonymous functions and the 'fminsearch' function.
Krish on 14 Dec 2012
Matt,
Sorry I do not have Curve Fitting Tool/Optimization Tool. Hence have to go with the other option.Here is the function I am trying to fit:-
X=[10,20,40,80]; Y=[0.1,0.2,0.3,0.4]; ((C1*exp(-C2/(R*T)))*X)/(1+((C1*exp(-C2/(R*T)))*X))
C1 and C2 are the constants I need to find to fit a curve that will pass through the expt data points
I understood your code, but I need to get the values of C1 & C2 and then use that to plot with X ranging from 1 to 100 in 1 increments and get corresponding Y value, showing goof correlation between experimental and the model.
Again thanks for you time and effort.

Matt Fig on 14 Dec 2012
Edited: Matt Fig on 14 Dec 2012
>> X = sort(rand(1,10)*30); % Say this is our x data.
>> Y = 14*exp(3*X); % Say this is _unknown_ equation.
>> pp = polyfit(X,log(Y),1); % Get 14, 3 from data only.
>> pp(2) = exp(pp(2))
pp =
3.0000 14.0000
pp should look familiar!

Alan Weiss on 14 Dec 2012
The documentation has a solved example that is similar to what you are trying to do.
Alan Weiss
MATLAB mathematical toolbox documentation