As documented

FindRoot[f[x], {x, xs, min, max}] 

searches for a solution, stopping the search if x ever gets outside the range min to max and returning the error FindRoot::reged.

Now I have

FindRoot[{f[x, y], g[x, y]}, {{x, xs}, {y, ys}}]

and I want to obtain the same behavior but for the constrain x > y.

Is this possible?

In particular, I'm interested in the case in which there are many solutions that satisfy the constrain, and I want to get as many as possible of them by giving several starting points.

  • 1
    $\begingroup$ Can you please give examples of f and g. Is there always (one, many, infinite?) solution(s) for x>y? Do you need one/many/all of them? Your problem is too general the way it is posed. In any case, perhaps it might be of help that you can restrict evaluating your functions in certain domains like so f[x_, y_] /; (x > y) := Sin[x y] ; If you Plot3D you'll see it's only evaluated for x>y. This wouldn't help very much here as FindRoot will spit out an error every time the function is not defined but it's a start. $\endgroup$ – gpap Mar 26 '13 at 11:00
  • $\begingroup$ @gpap Rather than restrict the functions, as you suggest, perhaps you could redefine the functions via reflection. That way you could simply recover the correct value by re-reflection, if necessary. $\endgroup$ – Mark McClure Mar 26 '13 at 12:47
  • $\begingroup$ Making these changes I lose continuity or differentiability in my functions, I wonder how that can affect the behavior of FindRoot (if any). $\endgroup$ – psmith Mar 26 '13 at 13:30

The basic pattern is this:

  Catch[FindRoot[{x^2 + y^2, x}, {x, 5}, {y, 2},
      StepMonitor :> If[x <= y, Throw[$Failed, findRootTag]]],

A more complex example:

Module[{f, g, cond, sol, pts, findRootTag},
 {f, g} = {x^2 + y^2 - 1, x - 2 y^2};
 cond = x <= y;
 {sol, {pts}} = Reap@Catch[FindRoot[{f, g}, {x, 1.8}, {y, 1.5},
     StepMonitor :> If[cond, Throw[$Failed, findRootTag], Sow[{x, y}]],
     EvaluationMonitor :> Sow[{x, y}]], findRootTag];
  RegionPlot[cond, {x, 0, 2}, {y, 0, 2}, PlotStyle -> LightRed],
  ContourPlot[{f == 0, g == 0}, {x, 0, 2}, {y, 0, 2}, ContourStyle -> Blue],
  Graphics[{Gray, Line[pts], Black, Point[pts], Red, Text[Style["Stay out!", 24], {.5, 1.5}]}],
  PlotRange -> {{0, 2}, {0, 2}}, AspectRatio -> Automatic, PlotLabel -> sol]]

enter image description here

Try changing the condition (cond) to something else, like x + y < 2. Then sol should become $Failed.

enter image description here

| improve this answer | |
  • $\begingroup$ I would like to receive the message with Check, how can I obtain that? $\endgroup$ – psmith Mar 28 '13 at 13:41
  • $\begingroup$ Do you mean this: define FindRoot::outside = "A step fell out of the allowed region." and then Check[FindRoot[..., StepMonitor :> If[x<y, Message[FindRoot::outside]], ...], $Failed]? However, the main problem is that you cannot interrupt FindRoot but with Throw. $\endgroup$ – Federico Mar 28 '13 at 13:59
  • $\begingroup$ Yes, exactly what I mean. Then, as I use all this in a Table, how can I use Throw to bring back nothing, when the condition is not met? (I mean not adding any item to the list, not to add the item NULL!) $\endgroup$ – psmith Mar 28 '13 at 14:26
  • $\begingroup$ You have many choices to accomplish that: Table[If[EvenQ[i], i], {i, 7}] /. Null -> Sequence[], or Table[If[EvenQ[i], i, Unevaluated@Sequence[]], {i, 7}], or using Reap/Sow again. $\endgroup$ – Federico Mar 28 '13 at 14:34
  • $\begingroup$ Ok, thanks. But If I want that Throw returns a Rule for x and y, how can I obtain that? If I simply write Throw[{x->q,y->p}], it replaces for x and y the last computed values, so instead of x->q I obtain something like 2.34->q. $\endgroup$ – psmith Mar 28 '13 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.