# fsolve doesn't find a solution when the value of the objective function is obtained through a Montecarlo simulation

4 views (last 30 days)

Show older comments

Hi all,

I am trying to solve a system of two non-linear equations using fsolve. The two equations are complex functions of the the two unknown (x,y). In particular, given a guess for (x,y), I have to approximate a distribution using a Montecarlo simulation and the two equations in my system are functions of this approximated distribution and other stuff. My problem is that fsolve is struggling in reaching a solution due to the fact that the two objectives do not change in a deterministic way from one iteration to another. I fixed the seed of the random number generator so to have always the same realizations from the simulations but this didn't fix the problem. Probably using a bisection would be better but I am not sure I can apply bisection to a system of equations. Do you know of any function doing that? The second solution I have in mind is to force fsolve to take larger steps from one guess to another in two subsequent iterations (which is to say: instead of increasing/decreasing the guess by a factor of 1.5e-8 across iterations like the solver does, increasing/decreasing it by a 1.5e-5 factor). How can I change this sensitivity?I tried with the 'TolX' option but I doesn't seem to work.

##### 0 Comments

### Accepted Answer

### More Answers (3)

Matt J
on 11 Sep 2021

Edited: Matt J
on 12 Sep 2021

Does your Monte Carlo simulation also compute an approximation to the Jacobian, or are you relying on fsolve's default finite difference approximation (SpecifyObjectiveGradient=false)? The latter is not a good idea when your objective function evaluations are noisy or coarsely accurate. If you cannot supply your own Jacobian calculation, it would be better to use a non-derivative based solver like fminsearch() or ga().

It should also help to apply variance reduction operations of some kind to the output of your Monte-Carlo simulation. Is there any smoothing you can do? Or could you run more random seeds?

Finally, you could loosen the FunctionTolerance parameter (TolFun if you are using fminsearch) to make the stopping criteria more tolerant to random noise in your objective. You could also substitute your own stopping criteria using the OutputFcn option.

Jeff Miller
on 13 Sep 2021

##### 0 Comments

John D'Errico
on 13 Sep 2021

Edited: John D'Errico
on 13 Sep 2021

Is your objective function a differentiable result of the parameters? If not, then fsolve cannot solve the problem. Fsol ve requires a differentiable (SMOOTH) function. It PRESUMES that if you evaluate the function twice at the same poiint, then you would get the same answer. Since that must fail for your problem, then you cannot use fsolve.

If you must optimize this objective, you might consider approximating your objective with a polynomial of sorts. As long as it is a good approximation. Then optimize the function you have approximated.

##### 0 Comments

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!