I have a two-variable function:

f[x_, y_] := (4 y^2 - 6) (Cos[y π] Cos[x] - Cos[2 y π] Sin[x])

How can I find all the numerical solutions for $x$ and $y$ such that $f(x,y)=0$? The domains are $0<x<\pi$ and $0<y<5$.

More precisely, I want to ask Mathematica to consider $x$ with the step $\frac{\pi }{5}$ and provide the values of $y$ for them.

  • 1
    $\begingroup$ Please have a look at FindRoot. $\endgroup$ Feb 24, 2020 at 15:13
  • $\begingroup$ @b.gates.you.know.what I want to obtain a list of solutions, not for a specific value! $\endgroup$
    – user67849
    Feb 24, 2020 at 15:21
  • 2
    $\begingroup$ Do you want the blue curves in @Artes' answer or really the red dots? That is, is the stepping of x by $\pi/5$ just for convenience or is it essential to what you want? $\endgroup$
    – Chris K
    Feb 24, 2020 at 20:32
  • $\begingroup$ @Chris K The function I used is just an example of what I wanted to know. Stepping could be different. I wanted to know a command in Mathematica that I can use to obtain numerical solutions of a two-variable function. $\endgroup$
    – user67849
    Feb 25, 2020 at 9:18

2 Answers 2


There are infinitely many (continuum) solutions and basically one cannot list them all e.g. because of the Cantor theorem. This is the reason why numerical approach is unsatisfactory. However we can make use of FindRoot to find a finite numerical subset of the solution space.

Numerical solutions

First we set up a net of values of x and starting point of y (we denote them by k in Table)

nsol = Flatten[ Table[{x, y}/.FindRoot[(4y^2 - 6)(Cos[π y]Cos[x] - Cos[2π y] Sin[x]) == 0,
                                          {y, k}], 
                       {x, π/5, 4 π/5, π/5}, {k, 1/3, 14/3, 1/3}], 
                1] // DeleteDuplicates;

Show[ ContourPlot[(4 y^2 - 6) (Cos[π y] Cos[x] - Cos[2π y] Sin[x]) == 0,
                  {x, -π/2, 2π}, {y, -0.1, 5.1}, 
                  PlotPoints -> 50, MaxRecursion -> 3, 
                  Epilog -> {Red, PointSize[0.023], Point[nsol]}], 
      RegionPlot[ Not[0 < x < π && 0 < y < 5], {x, -π/2, 2π}, {y, -0.1, 5.1}]]

enter image description here

Numerical solutions are contained in nsol, we write only a few and hiding 38 of them:

Short[N @ nsol, 3]
{{0.628319, -0.645834}, {0.628319, 0.645834}, <<38>>, {2.51327, 4.35417},
 {2.51327, 5.64583}}

One can see that the first point in nsol doesn't belong to the domain of our interest. We can get rid of such points setting a different net of starting points in FindRoot or using another tools of the system, however this approach might be more involved for more sophisticated transcendental equations. An example of more detailed discussion how to deal with FindRoot can be found at e.g. First positive root.

Exact (symbolic) solution

More interesting is finding an exact descripton of the blue curves which represent symbolic solutions. The form of our equation $f(x,y)=0$ can be factorized into two algebraic expressions and the roots of the original equation is the set-theoretic sum of solutions of two equations: 4 y^2 - 6 == 0 and (Cos[π y] Cos[x] - Cos[2π y] Sin[x]) == 0.

The straight line is the solution of this equation (for x in the range 0 < x < π)

Solve[(4 y^2 - 6) == 0 && 0 < y < 5, y]
 {{y -> Sqrt[3/2]}}

While another equation gives us those periodic curves, moreover it appears that one can solve equation for x obtaining it as a function of y.

Reduce[(Cos[π y] Cos[x] - Cos[2π y] Sin[x]) == 0 && 0 < x < π, x, Reals]
  (Cos[π y] != 0 && Cos[π y] != 0 && 
x == 2 ArcTan[ Sec[π y] (-Cos[2π y] + Cos[π y] Sqrt[(Cos[π y]^2 + 
         Cos[2 π y]^2) Sec[π y]^2])]) ||
((-π + x)/(2 π) \[NotElement] Integers && Cos[π y] == 0 && 
 Cos[2 π y] == 0 && 0 < x < π)

This expression could be further simplified however the main point is the parametrization of curves with

FullSimplify[ x == 2 ArcTan[ Sec[π y] (-Cos[2π y] + Cos[π y] Sqrt[(Cos[π y]^2       
                             + Cos[2π y]^2) Sec[π y]^2])], 
              0 < x < π && 0 < y < 5] // TraditionalForm

enter image description here

For some reasons the standard approach with Reduce on the original equation takes a long time returning quite an involved expression even though the equation is not hard to solve when we deal with different factors of the original function. Finially we plot the graph of the above symbolic solutions with red points denoting numerical ones, yet another time we exploit ContourPlot

  ContourPlot[{x == -2ArcTan[ Cos[2π y]Sec[π y] - Sqrt[1 + Cos[2π y]^2 Sec[π y]^2]], 
               y == Sqrt[3/2]}, {x, -π/2, 2π}, {y, -0.3, 5.3}, 
              PlotPoints -> 50, MaxRecursion -> 3, 
              ContourStyle -> {{Thick, Darker@Cyan}, Thick, Magenta}, 
              Epilog -> {Red, PointSize[0.016], Point[nsol]}], 
  RegionPlot[ Not[0 < x < π && 0 < y < 5], {x, -π/2, 2π}, {y, -0.3, 5.3}]]

enter image description here

  • $\begingroup$ Thanks for the answer. But I want to know which command I can use to obtain numerical data for general types of functions. Do you have any idea? $\endgroup$
    – user67849
    Feb 24, 2020 at 18:46
  • 1
    $\begingroup$ @Baran FindRoot is the function you are looking for. I've presented how to use it, red points on the graph are found with FindRoot. However those blue curves can be found with a symbolic tool Reduce. Just evaluate nsol from my answer to find out numerical solutions. $\endgroup$
    – Artes
    Feb 25, 2020 at 19:11
  • $\begingroup$ @Baran Could you explain what did you expect by "numerical solutions", and why FindRoot did not satisfied your expectation? I'm just curious about it. $\endgroup$
    – Artes
    Mar 16, 2020 at 20:44
  • $\begingroup$ Now, I am satisfied with FindRoot and NSolve. thanks. $\endgroup$
    – user67849
    Mar 17, 2020 at 22:55

Reduce also seems to work on the original f, though the answer it gives is somewhat long and involves a collection of Tan and ArcTan.

f[x_, y_] := (4 y^2 - 6) (Cos[y \[Pi]] Cos[x] - Cos[2 y \[Pi]] Sin[x]);
Reduce[f[x, y] == 0, {x, y}]
  • 1
    $\begingroup$ Thanks for the answer. My question is: is there a command to obtain numerical solutions for these types of two-variable function s? $\endgroup$
    – user67849
    Feb 24, 2020 at 18:52
  • $\begingroup$ It seems to me that Reduce gives an exact and explicit set of solutions. It's complicated, but so is your function. $\endgroup$
    – bill s
    Feb 24, 2020 at 22:19

Your Answer

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