Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
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. –  gpap Mar 26 '13 at 11:00
    
@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. –  Mark McClure Mar 26 '13 at 12:47
    
Making these changes I lose continuity or differentiability in my functions, I wonder how that can affect the behavior of FindRoot (if any). –  Fabrizio Mar 26 '13 at 13:30
add comment

1 Answer

up vote 2 down vote accepted

The basic pattern is this:

Module[{findRootTag},
  Catch[FindRoot[{x^2 + y^2, x}, {x, 5}, {y, 2},
      StepMonitor :> If[x <= y, Throw[$Failed, findRootTag]]],
    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];
 Show[
  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

share|improve this answer
    
I would like to receive the message with Check, how can I obtain that? –  Fabrizio Mar 28 '13 at 13:41
    
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. –  Federico Mar 28 '13 at 13:59
    
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!) –  Fabrizio Mar 28 '13 at 14:26
    
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. –  Federico Mar 28 '13 at 14:34
    
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. –  Fabrizio Mar 28 '13 at 15:17
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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