File Exchange

image thumbnail

Exponential fit, without start-guess

version 1.3 (8.41 KB) by

Fits 1) f=s1+s2*exp(-t/s3) or 2) f=s1+s2*exp(-t/s3)+s4*exp(-t/s5) to numerics, without startguess

13 Ratings



No License

exp2fit solves the non-linear least squares problem exact
and using it as a start guess in a least square method
in cases with noise, of the specific exponential functions:
 --- caseval = 1 ----
 --- caseval = 2 (general case, two exponentials) ----
 --- caseval = 3 ----
   f=s1*(1-exp(-t/s2)) %i.e., constraints between s1 and s2
  Syntax: s=exp2fit(t,f,caseval) gives the parameters in the fitting function specified by the choice of caseval (1,2,3). t and f are (normally) vectors of the same size, containing the data to be fitted.
s=exp2fit(t,f,caseval,lsq_val,options), using lsq_val='no' gives the analytic solution, without least square approach (faster), where options (optional or []) are produced by optimset, as used in lsqcurvefit.

This algorithm is using analytic formulas using multiple integrals. Integral estimations are used as start guess in lsqcurvefit. Note: For infinite lengths of t, and f, without noise the result is exact.

%--- Example 1: (see also help exp2fit)

%--- solve without startguess

%--- plot and compare
fun = @(s,t) s(1)+s(2)*exp(-t/s(3));
figure(1), clf;plot(t,f,'.',tt,ff);

%--- Example 2, Damped Harmonic oscillator:
%--- Note: sin(x)=(exp(ix)-exp(-ix))/2i
%--- solve without startguess

%--- plot and compare
fun = @(s,t) s(1)+s(2)*exp(-t/s(3))+s(4)*exp(-t/s(5));
figure(1), clf;plot(t,f,'.',tt,real(ff));

%% By Per Sundqvist january 2009.

Comments and Ratings (17)

Gordon Judd

what is the structure of lsq_val when you want to start with initial estimates for the s values and get an initial guess to the @(s,t) s(1)+s(2)*exp(-t/s(3))+s(4)*exp(-t/s(5));

setting that lsq_val to 'no' as shown in your example does not work when you have a number of damped cycles in the data.

Very simple and easy to use.


Shelby (view profile)

Is there an easy way to adjust the fitting function? In particular, I'd like to fit
(No constant term)


Nam (view profile)

Thanks, it helps for my job!

Tung Le



Hi thanks for supplying this script.

After reading the help file and trying to following the examples (only #2 seemed to work btw), my understanding how this script works is still a little vague. I have three questions:

1) What is the purpose of the variable 'tt' and how is it derived?

2) Why does the script only work when lsq_val is set to 'no'

3) What exactly are the parameters 's' returns - e.g., if I'm running a double exponent, does it return, in the following order:
A1 = first amplitude
t1 = first lifetime
A2 = second amplitude
t2 = second lifetime
yo = offset

Kind regards,

Gordon Judd

This is just the fit I have been looking for, but I find I get poor fits on experimental data (y positions of oscillating cantilever beam) depending on the time limits used in the data.

Is there any constraint requiring that an integer number of vibration cycles be included in the limits for the time data? I get a reasonably good fit for 1 cycle but a poor fit when several cycles are included.

Works fine. Can you also please include any reference for the theory/method you used in this code? that should be helpful for any practitioner.

The intent is good, but the code is very messy, which made that, after getting initially bad results, I didn't even try to check where the error came from.


Bass (view profile)

Works fine for the simple fits I have tried. It would be great if it had confidence intervals on fit values


Bass (view profile)

Works fine for the simple fits I have tried

Long Zhao

It really helps a lot. However, what if I want to set the condition that the parameters s(i)>0? Look, t1={384,1186,1471,2236,2772,2967,3812,4880,6104} and y1={13,18,26,34,40,48,61,75,84}, I got that S(i) are a+bi like stuff, what I really want to get is the real number. Again, t2={254,788,1054,1393,2216,2880,3593,4281,5180} and y2={1,3,8,9,11,16,19,25,27}, then S(5)<0 rather than the positive I want to obtain.


Matej (view profile)

any idea why is fun = @(s,t) giving error?
somehow function_handle (@) is not working as it should. is this because of older matlab version (R13) ?


Per Sundqvist

Per Sundqvist (view profile)

HI, B. Roossien. I'm not sure what kind of help you want. The code is standard but assumes perhaps that you are a bit familiar with matlab. nargin<4 means that your input arguments are 3 and you will then get options as specified, later on, at the end of the program used in the matlab function lsqcurvefit. Maby I should tell that t and f should be vectors of the same size? Or do you want me to explain the theoretical model? Its lengthy though...

As well I realize now that I in the beginning of the program should have wrote:
%to make time and function f vectors sorted column vectors in case they have a funny formate like matrices.


Bart (view profile)

The help is a bit cryptic, making it difficult to understand. It does have a partial H1 line. It also has some argument checking. However, the code is incomprehensible and the few lines of comments do not help understanding it in any way.



The function is generalized to handle two exponentials now: f=s1+s2*exp(-t/s3)+s4*exp(-t/s5) and also small fix and an option is included to choose if lsqcurvefit should be applied or not.


Improvement in numerical integration, giving perfect agreement also for small number of data-points. nov-08

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video