How to use lsqnonlin with multiple constraints?
15 views (last 30 days)
Show older comments
Hello,
I want to run non-linear regression with multiple constraints. I found that I should use lsqnonlin function.
My function is
((b(1) + b(2)*log(CBd(:,2))).*(CBd(:,4).^(b(3)+b(4).*((CBd(:,6)-T0)/T0))).*(CBd(:,5).^b(5)))+((b(6) + b(7)*log(CBd(:,3))).*(CBd(:,4).^(b(8)+b(9).*((CBd(:,7)-T0)/T0))).*(CBd(:,5).^b(10)))
and CBd is data.
I want to have b(3) & b(8) as negative value and b(5) & b(10) as positive.
How can I do that?
Thanks in advance.
0 Comments
Accepted Answer
John D'Errico
on 23 Apr 2023
Edited: John D'Errico
on 23 Apr 2023
These are not so much constraints, as bounds. They are simpler things. lsqnonlin allows bounds, but not equality or inequality constraints.
help lsqnonlin
The third and 4th arguments to lsqnonlin are LB and UB, respectively, as you can see in the help.
If a variable is constrained to be positive, then use a lower bound for that variable of 0. If negative, then the upper bound is zero. And if a variable has no bounds at all, then supply -inf and inf for those variables. But don't think you can just pass in a scalar value of zero, and hope that lsqnonlin will apply that value to ALL of your unknowns. For example, of LB and UB are:
LB = [-inf, 0 , 3, -inf, -inf];
UB = [inf, inf, 5, 0, inf];
Then you have 5 variables. The bounds would be:
Variable 1 has no bounds at all.
Variable 2 is constrained to be positive.
Variable 3 Must lie in the interval [3,5].
Variable 4 Is constrained yto be negative.
Variable 5 has no bounds at all.
Finally, you need to understand that just because you tell lsqnonlin to bound a variable to be strictly positive, that the solver will only obey that to within a constraint tolerance. SO you might see something on the order of -1e-12, even if the number was bounded above zero.
If you truly, absolutely cannot tolerate a slightly nevegative result, ever, then use a bound that is just a wee bit higher than zero, so sufficiently larger than the constraint tolerance. (I don't recall the default there.)
Even at that, if you want something to be strictly positive, then it might be best to use a transformation of the variable. Thus if a MUST be positve in the function a*x, then consider using a form like exp(a)*x. Now the solver can estimate a as any value, positive or negative. All that matters is exp(a) is indeed always positive. Then when the solver returns a result, exponentiate that parameter. Another transformation that can work as well is a^2 to force a variable to be non-negative.
0 Comments
More Answers (1)
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!