No License

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

» Watch video

Highlights from
Exponential fit, without start-guess

3.8 | 13 ratings Rate this file 17 Downloads (last 30 days) File Size: 8.41 KB File ID: #21959 Version: 1.3
image thumbnail

Exponential fit, without start-guess


Per Sundqvist (view profile)


31 Oct 2008 (Updated )

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

| Watch this File

File Information

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.

Required Products Curve Fitting Toolbox
MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
18 Feb 2017 winita sulandari

01 Aug 2016 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.

12 Nov 2012 Galindo-Rosales

Very simple and easy to use.

16 Aug 2012 Shelby

Shelby (view profile)

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

Comment only
13 Mar 2012 Nam

Nam (view profile)

Thanks, it helps for my job!

Comment only
13 May 2011 Tung Le

10 Mar 2011 Gaszton

14 Jul 2010 Matthias

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,

14 Feb 2010 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.

15 Jun 2009 Nitipat Pholchai

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.

23 Mar 2009 Gavrilo Bozovic

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.

18 Mar 2009 Bass

Bass (view profile)

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

18 Mar 2009 Bass

Bass (view profile)

Works fine for the simple fits I have tried

17 Mar 2009 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.

05 Feb 2009 Matej

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) ?


Comment only
03 Nov 2008 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.

Comment only
02 Nov 2008 Bart

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.

12 Nov 2008 1.2

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

09 Jan 2009 1.3

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.

Contact us