
Fit a model to data using lsqnonlin()
4 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Matt J
on 19 Nov 2018
Your initial guess is way off. With x0=[1,1] I get something very reasonable looking.

2 Comments
Matt J
on 22 Nov 2018
Edited: Matt J
on 22 Nov 2018
There is no general method for choosing the initial guess, but it shouldn't be random. In this case, you know that the thetas are on the order of 1 or 5, not .0001. A value of 0.0001 is, in fact, so far away that it causes the exp() calculation to underflow for the values x = 0:0.3:19 that you are considering,
>> exp(-19/.0001)
ans =
0
>> isequal(0,ans)
ans =
logical
1
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!