Reputation: 1335
I'm trying to do some fitting with lsqcurvefit. I have a function like that:
function F = cdf_3p_model(a,data)
F=1-((1-a(5)-a(6)).*(exp(-abs(data)./a(1)))+((1-a(4)-a(6)).*(exp(-abs(data)./a(2))))+((1-a(4)-a(5)).*(exp(-abs(data)./a(3)))));
and
function [a residual] = cdf_fit_3p(x,y)
a0 = [10 1 0.1 0.3 0.3 0.3];
lb = [0 0 0 0 0 0];
ub = [];
curvefitoptions = optimset('Display','final','MaxFunEvals',100000,'MaxIter',50000);
[a, residual] = fmincon(@cdf_3p_model,a0,x,y,lb,ub,curvefitoptions);
end
I set the initial parameters, ub, lb but how do I also declare that:
a(1) > a(2) > a(3)
a(5) + a(6) +a(7) = 1
Upvotes: 2
Views: 2432
Reputation: 283684
You will have to calculate the parameters you "want" from a set of parameters that's more usable to MatLab.
For example, you can rewrite
a(1) > a(2) > a(3)
a(5) + a(6) + a(7) = 1
as
a(3) = p(1)
a(2) = p(1) + p(2)
a(1) = p(1) + p(2) + p(3)
a(4) = p(4)
a(5) = p(5)
a(6) = p(6)
a(7) = 1 - p(5) - p(6)
with
lb = [0 0 0 0 0 0]
ub = [Inf Inf Inf Inf 1 1]
Well, it's not perfect, because it allows a(7) as low as -1 instead of 0. But it includes your other constraints.
Upvotes: 0
Reputation: 5359
I think you have better chance using one of the minimization routines such as fmincon which allows you to specify constraints you might otherwise be unable to do. You can easily incorporate least-squares by taking the L2-norm of the difference between model and data
Upvotes: 3
Reputation: 1003
Normally I would say, "make clauses in your function that gives really terrible 'scores' when those conditions are not met." However, your conditions make the range of allowable parameters such a tiny, tiny subset of the range of possible numbers that I think you would cause lsqcurvefit to never converge if you do that. I would say lsqcurvefit is not the right solution for you.
Upvotes: 0