1
$\begingroup$

I have a system of two equations in two unknowns that I would like to solve with NSolve within a specified region.

However, while Mathematica solves the system if I omit the domain specification, it fails to do so when I include it.

Here is an example:

eq1 = (-0.9781476007338057` + Cos[x] Cos[y])^2 + 
    Cos[y]^2 Sin[x]^2 + (-0.20791169081775931` + Sin[y])^2 == 
    0.04370479853238872`;

eq2 = -0.058944236842231254` (-0.9781476007338057` + Cos[x] Cos[y]) - 
    0.20040259242104053` Cos[y] Sin[x] - 
    0.008317236704697833` (-0.20791169081775931` + Sin[y]) == 0;

NSolve returns four solutions:

NSolve[{eq1, eq2}, {x, y}]

{{x -> -3.13939, y -> 3.14158}, {x -> -3.12983, 
  y -> 2.72301}, {x -> 0.0022017, y -> 0.0000114031}, {x -> 0.0117594,
   y -> 0.418582}}

Among these four solutions, I am only interested in the one that has positive $x$ and is sufficiently different from $(0,0)$, i.e., the last one. Hence I try to solve

NSolve[{eq1, eq2, x \[Element] Interval[{0.01, 0.5}]}, {x, y}]

which Mathematica returns unevaluated.

Can anybody help?

$\endgroup$
7
  • 1
    $\begingroup$ FindRoot[{eq1, eq2}, {{x, .5}, {y, .5}}] $\endgroup$
    – Derek H
    Commented Sep 30, 2021 at 13:44
  • $\begingroup$ First, there is more than one solution that has positive $x$ and is sufficiently different from $(0,0)$. Second, the equation is transcendental, not algebraic, although perhaps you didn't mean algebraic in its mathematical sense but meant "symbolic." Perhaps this: Solve[Rationalize[Rationalize@{eq1, eq2, 0.01 < x}, 0], {x, y}, Method -> Reduce] /. s_?NumericQ /; ! IntegerQ[s] && FreeQ[s, Pi] :> N@s or with the condition 0.01 < x || x > 0 && Abs[y] > 0.01 $\endgroup$
    – Michael E2
    Commented Sep 30, 2021 at 14:45
  • $\begingroup$ @MichaelE2: In view of the periodicity of $\sin$ and $\cos$ it's enough to consider the solutions on $(-\pi,\pi]\times(-\pi,\pi]$. $\endgroup$
    – user64494
    Commented Sep 30, 2021 at 15:01
  • $\begingroup$ @MichaelE2: Can you elaborate and ground " Your suggestion leaves the problem unfinished"? TIA. $\endgroup$
    – user64494
    Commented Sep 30, 2021 at 15:24
  • $\begingroup$ NSolve[{eq1, eq2, x > -Pi, x <= Pi, y > -Pi, y <= Pi}, {x, y}] results in {{x -> -3.13939, y -> 3.14158}, {x -> -3.12983, y -> 2.72301}, {x -> 0.0022017, y -> 0.0000114031}, {x -> 0.0117594, y -> 0.418582}} the same as NSolve[{eq1, eq2}, {x, y}]. $\endgroup$
    – user64494
    Commented Sep 30, 2021 at 15:29

2 Answers 2

1
$\begingroup$

Restricting y too, we succeed:

NSolve[{eq1, eq2, x >= 0.01, x <= 0.5, y >= -5, y <= 5}, {x, y}, Reals]

or

NSolve[{eq1, eq2, x \[Element] Interval[{0.01, 0.5}], y >= -5, y <= 5}, {x, y}, Reals]

{{x -> 0.0117594, y -> 0.418582}}

$\endgroup$
3
  • $\begingroup$ I am on Win7, Mma12.2 and it repeats the input back with both these variations. Is there some other option I have to use or is this new functionality? The functionality shown by OP works however. $\endgroup$
    – Syed
    Commented Sep 30, 2021 at 14:04
  • $\begingroup$ It's strange. The documentation says (at the bottom) NSolve was updated latest time in 2014 ( version 10.0). Works for me in 12.3.1 on Windows 10. Try it in cloud. $\endgroup$
    – user64494
    Commented Sep 30, 2021 at 14:16
  • $\begingroup$ @Syed - They also are unevaluated with v12.2 on a Mac $\endgroup$
    – Bob Hanlon
    Commented Sep 30, 2021 at 15:51
1
$\begingroup$

Combining the FindRoot usage suggested by @DerekH with manipulate:

Manipulate[
 sol = FindRoot[{eq1, eq2}, {{x, xcoord}, {y, ycoord}}];
 Show[{ContourPlot[Evaluate@{eq1, eq2}, {x, -10, 10}, {y, -10, 10}, 
    PlotLabel -> Style[sol, Red, Bold], GridLines -> Automatic],
   Graphics[{Red, PointSize[Medium], Point[{x, y} /. sol]}]
   }],
 {{xcoord, 0}, 0, 10, 0.2},
 {{ycoord, 0}, 0, 10, 0.2}
 ]

enter image description here

If the OP wants to choose something sufficiently removed from the origin, there seem to be a lot of choices.

EDIT A more satisfying result would however be all the solutions in a bounded region.

solreduce = 
 Reduce[{x, y} \[Element] Disk[{0, 0}, 5] && eq1 && eq2, {x, y}]
sol3 = {ToRules@solreduce}

gives the ten closest results around origin bounded by the disk used to demarcate the region.

$\endgroup$
2
  • $\begingroup$ The periodicity of Sin and Cos implies the periodicity of the solutions. It's enough to consider {x,-Pi,Pi} and {y,-Pi,Pi}. $\endgroup$
    – user64494
    Commented Sep 30, 2021 at 14:58
  • $\begingroup$ Agreed and the output of the Reduce shows that too. I have included this to show usage. $\endgroup$
    – Syed
    Commented Sep 30, 2021 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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