Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fsolve with solution bounds

Tags:

matlab

Is there a way to use fsolve in MATLAB, specifying a bound for the solution? i.e. all solution variables > 0

like image 495
Brian Avatar asked Mar 20 '26 10:03

Brian


2 Answers

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.

like image 103
Kena Avatar answered Mar 24 '26 10:03

Kena


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.

like image 30
Matt J Avatar answered Mar 24 '26 12:03

Matt J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!