8 Downloads
Updated 09 Jan 2009
No License
exp2fit solves the nonlinear 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 
f=s1+s2*exp(t/s3)
 caseval = 2 (general case, two exponentials) 
f=s1+s2*exp(t/s3)+s4*exp(t/s5)
 caseval = 3 
f=s1*(1exp(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)
t=linspace(1,4,100)*1e9;
noise=0.02;
f=0.1+2*exp(t/3e9)+noise*randn(size(t));
% solve without startguess
s=exp2fit(t,f,1)
% plot and compare
fun = @(s,t) s(1)+s(2)*exp(t/s(3));
tt=linspace(0,4*s(3),200);
ff=fun(s,tt);
figure(1), clf;plot(t,f,'.',tt,ff);
% Example 2, Damped Harmonic oscillator:
% Note: sin(x)=(exp(ix)exp(ix))/2i
t=linspace(1,12,100)*1e9;
w=1e9;
f=1+3*exp(t/5e9).*sin(w*(t2e9));
% solve without startguess
s=exp2fit(t,f,2,'no')
% plot and compare
fun = @(s,t) s(1)+s(2)*exp(t/s(3))+s(4)*exp(t/s(5));
tt=linspace(0,20,200)*1e9;
ff=fun(s,tt);
figure(1), clf;plot(t,f,'.',tt,real(ff));
%% By Per Sundqvist january 2009.
1.3.0.0  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. 

1.2.0.0  Improvement in numerical integration, giving perfect agreement also for small number of datapoints. nov08 
Create scripts with code, output, and formatted text in a single executable document.
Elham Baranizadeh (view profile)
Dear Per,
I am using your great function exp2fit. it fits my data really nice most of the time, but it gives me complex values (i.e. complex s values) for some of my experiments. I noticed that when I give less number of data (for the same case from which I get complex number) it gives me real values but I intend to input the whole data ()about 4000 data points) not cutting off the end of them. any idea how I can fix this? Also, when getting complex values the function nlparci gives an error. Thanks again.
Elham Baranizadeh (view profile)
winita sulandari (view profile)
Gordon Judd (view profile)
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));
function?
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.
GalindoRosales (view profile)
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
f=s2*exp(t/s3)
(No constant term)
Thanks!
Nam (view profile)
Thanks, it helps for my job!
Tung Le (view profile)
Gaszton (view profile)
Matthias (view profile)
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*exp(x/t1)+A2*exp(x/t2)+y0
where:
A1 = first amplitude
t1 = first lifetime
A2 = second amplitude
t2 = second lifetime
yo = offset
Kind regards,
Matt
Gordon Judd (view profile)
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.
Nitipat Pholchai (view profile)
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.
Gavrilo Bozovic (view profile)
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 (view profile)
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 6.5.2.202935 (R13) ?
thanx
matej
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:
t=t(:);
f=f(:);
[t,ix]=sort(t);
f=f(ix);
%to make time and function f vectors sorted column vectors in case they have a funny formate like matrices.
/Per
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.