1

Suppose I have two functions $p(x,y)$ and $q(x,y)$. I would like to check if there is any pair $(x,y)$ within a range $(0,0)$ to $(\hat{x},\hat{y})$ for which $p(x,y)=q(x,y)$. And if such pair exists, I would like to print them. Is it possible to do with Mathematica?

Update: As per the link provided by "bbgodfrey", I tried the following input:

f[x_, y_] := -Cos[y] + 2 y Cos[y^2] Cos[2 x];

g[x_, y_] := -Sin[x] + 2Sin[y^2] Sin[2 x]; 

pts = FindCrossings2D[{f, g}, {x, -7/2, 4}, {y,-9/5, 21/5}] 

And got the following output:

FindCrossings2D[{f, g}, {x, -(7/2), 4}, {y, -(9/5), 21/5}]

I wish I could get the list of all crossings as $(x,y)$ coordinates within the specified range.

Quantum_Oli
  • 7,964
  • 2
  • 21
  • 43
usr109876787
  • 225
  • 1
  • 6
  • It certainly is possible, and a few earlier questions have addressed this issue. Basically, you are searching for the zeroes of p[x, y] - q[x, y]. – bbgodfrey Jan 09 '17 at 04:54
  • Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory [tour] now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Jan 09 '17 at 04:55
  • @ bbgodfrey, Thanks, I will remember that. – usr109876787 Jan 09 '17 at 05:06
  • Check here for a solution. – bbgodfrey Jan 09 '17 at 05:16
  • You need to define the FindAllCrossings2D function, it is not a built in MMA function. It's definition (in terms of other built in MMA functions) is given in the first grey cell of the answer linked to by bbgodfrey; copy that code, evaluate it, then your above code should work. – Quantum_Oli Jan 09 '17 at 06:31
  • Also be sure to obey the syntax required by FindAllCrossings2D. Spell its name correctly, FindAllCrossings2D, not FindCrossings2D, and it needs {f[x,y], g[x,y]}, not just {f,g} in its argument list. (Side note, undefined functions (and variables) will appear blue in your notebook, once defined they will turn black). – Quantum_Oli Jan 09 '17 at 06:34

1 Answers1

8

You can visualize the crossings:

f[x_, y_] := -Cos[y] + 2 y Cos[y^2] Cos[2 x];
g[x_, y_] := -Sin[x] + 2 Sin[y^2] Sin[2 x];
Plot3D[{f[x, y], g[x, y]}, {x, -7/2, 4}, {y, -9/5, 21/5}, 
 MeshFunctions -> (f[#1, #2] - g[#1, #2] &), Mesh -> {{0.}}, 
 MeshStyle -> Directive[Red, Thick], PlotStyle -> Opacity[0.5], 
 PlotRange -> All]
ContourPlot[f[x, y] - g[x, y] == 0, {x, -7/2, 4}, {y, -9/5, 21/5}]

enter image description here

In response to comment:

In the following finding zeroes in a range given x (or y):

zgivenx[x0_, 
  r_] := {x0, y} /. 
    Quiet@FindRoot[
      f[x0, y] - g[x0, y], {y, #}] & /@ (Range[##, (#2 - #1)/20] & @@ 
    r)
zgiveny[y0_, 
  r_] := {x, y0} /. 
    Quiet@FindRoot[
      f[x, y0] - g[x, y0], {x, #}] & /@ (Range[##, (#2 - #1)/20] & @@ 
    r)
ptsx = zgivenx[1, {-3, 5}]
ptsy = zgiveny[1, {-3, 3}]
ContourPlot[f[x, y] - g[x, y] == 0, {x, -7/2, 4}, {y, -9/5, 21/5}, 
 Epilog -> {PointSize[0.02], Red, Point[ptsx], Green, Point[ptsy]}]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • Thanks! Is it possible to print all the coordinates $(x,y)$ that satisfies $f(x,y)-g(x,y)==0$ within the given range? And also where I can find literature about what algorithm/numerical method Mathematica is using to solve this problem? – usr109876787 Jan 09 '17 at 07:02
  • @usr109876787 see my update. You may need to refine to suit whatever your needs are. I hope, however, this is a useful start. :) – ubpdqn Jan 09 '17 at 07:17
  • Indeed it is great to start and thanks a lot! – usr109876787 Jan 09 '17 at 07:21