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