### Highlights from Exponential fit, without start-guess

3.7
3.7 | 11 ratings Rate this file 18 Downloads (last 30 days) File Size: 8.41 KB File ID: #21959 Version: 1.3

# 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

File Information
Description

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 ----
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*(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.

t=linspace(1,4,100)*1e-9;
noise=0.02;
f=0.1+2*exp(-t/3e-9)+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)*1e-9;
w=1e9;
f=1+3*exp(-t/5e-9).*sin(w*(t-2e-9));

%--- 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)*1e-9;
ff=fun(s,tt);
figure(1), clf;plot(t,f,'.',tt,real(ff));

%% By Per Sundqvist january 2009.

Required Products Curve Fitting Toolbox
MATLAB release MATLAB 7 (R14)
12 Nov 2012 Galindo-Rosales

### Galindo-Rosales (view profile)

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
f=s2*exp(-t/s3)
(No constant term)
Thanks!

Comment only
13 Mar 2012 Nam

### Nam (view profile)

Thanks, it helps for my job!

Comment only
13 May 2011 Tung Le

### Tung Le (view profile)

10 Mar 2011 Gaszton

### Gaszton (view profile)

14 Jul 2010 Matthias

### 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
A2 = second amplitude
yo = offset

Kind regards,
Matt

14 Feb 2010 Gordon Judd

### 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.

15 Jun 2009 Nitipat Pholchai

### 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.

23 Mar 2009 Gavrilo Bozovic

### 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.

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

### 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.

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 6.5.2.202935 (R13) ?

thanx
matej

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:
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

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.