MATLAB: Fit a model to data using lsqnonlin()

lsqnonlinoptimization

We are trying to fit a model :
=> f(x; alpha, beta) = alpha*exp(-x / beta)
with beta > 0 on some data (x, z).
The goal is therefore to compute the values of coefficients alpha and beta corresponding to the minimum squared error.
Here is the data generator used to generate (x, z) :
randn('seed',3); % always the same source of randomness
theta_1 = 5; % ground truth

theta_2 = 1; % ground truth
x = 0:0.3:19;
y = exp(-x/theta_1)-0.8*exp(-x/theta_2);
N = length(x);
noise = 0.03; % noise level (can be changed)
z = y + noise*randn(1, N);
figure(1), clf, plot(x,z,'o','linewidth',2); grid on;
save 'data0.mat'
We have used lsqnonlin
% Optimization of alpha and beta coefficients
fun = @(v)v(1) * exp(-x / v(2)) - z;
lb = [-Inf, 0.0001];
ub = [Inf, Inf];
x0 = [0, 0.0001];
[ab, f_val] = lsqnonlin(fun, x0, lb, ub);
But it seems like the coefficients we are finding are not the right one.
Would you have an idea about what's wrong ?
Thanks a lot.

Best Answer

Your initial guess is way off. With x0=[1,1] I get something very reasonable looking.
untitled.png