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

4 views (last 30 days)
sarah zoi on 11 Sep 2021
Commented: Matt J on 14 Sep 2021
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.

sarah zoi on 14 Sep 2021
Thanks to all your answer!! I finally solved the issue by fixing the sequence of random numbers and doing a grid search on the parameter space (I could still do it due to the low-dimensionality of the problem, i.e. 2 parameters). It was slow but safe!:)
Matt J on 14 Sep 2021
Very good, but you should Accept-click the answer to indicate that this is the solution you've reached.

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.
sarah zoi on 14 Sep 2021
Thanks Matt! I tried with fminsearch as you suggested but it didn't solve the problem. iIcould finally get the solution using a grid search.

Jeff Miller on 13 Sep 2021
I don't know if it can be done in the context of your simulation, but sometimes it is helpful to pick the "random" variables systematically rather than randomly. For example, instead of a Monte Carlo sampling 100 uniform 0-1 random numbers, deterministically use the 100 values 0.005:0.001:0.995 that provide a good and fixed representation of that distribution every time the objective function is evaluated.

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.