fsolve with solution bounds
Is there a way to use fsolv开发者_开发百科e in MATLAB, specifying a bound for the solution? i.e. all solution variables > 0
Not directly, but one solution to this problem is to add a term to your equation which constrains your problem.
I don't have the optimization toolbox, so I can't give you a specific example using fsolve, but here's how I would do it with fminsearch, which has the same issue.
myFun = @(args) abs( sin(args(1)) + cos(args(2)) )
fminsearch(myFun, [0, 0])
ans =
-0.8520 0.7188
But if I want to constrain my problem to positive solutions
myFun = @(args) abs(sin(args(1)) + cos(args(2))) + (args(1)<0) + (args(2)<0)
fminsearch(myFun, [0, 0])
ans =
0.0000 1.5708
There should be a way to tweak your equation similarly to solve your problem.
You should be using lsqnonlin, which is very much like fsolve, but allows you to specify bound constraints.
Another approach is to use a transformation of variables. For example, to enforce x>=0, then instead of solving F(x)=0 w.r.t. x, solve F(z.^2)=0 w.r.t. z and then use x=z.^2 as your solution. This has a few subtle hazards. For example, you must avoid using z(i)=0 as the initial guess, but it often works.
No. However, instead of solving for F(x)=0
, you can minimize abs(F(x))
using e.g. FMINBND.
EDIT
Unfortunately, fminbnd
does not seem to support array-valued arguments (which I assume is what you want). For this to work, you need to turn to FMINCON from the optimization toolbox.
精彩评论