# Getting a list of x coordinates of intersection points of two functions

I have two functions,

f[x_] := 5 Sin[2 x]
g[x_] := (5 x + 2)/(x + 2)


I am trying to find the x coordinates that intersect between these two functions. I am new to Mathematica and everything I have tried isn't working.

• Welcome to Mathematica.SE! I suggest that: 1) You 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. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey May 1 '15 at 2:03
• @Tdogg What have you tried so far? In what ways have your attempts failed? – MarcoB May 1 '15 at 2:04

Your function f[x] is bounded to the interval $[-5, 5]$. On the other hand, $g[x] > 5$ for $x < -2$, so they never intersect there.

For $x>-2$, your functions intersect at infinitely many points. You can convince yourself of this by plotting them together using your definition:

Plot[
{Legended[f[x], "f(x)"], Legended[g[x], "g(x)"]},
{x, -2, 20}, Exclusions -> {x == -2},
PlotRange -> {-7, 6}
]


Finding a closed form representing them all may be a major undertaking, if one exists at all.

On the other hand, numerical solutions can be obtained for any one of these intersections with a variety of techniques. For instance, the following finds the solution closest to 5:

FindRoot[f[x] == g[x], {x, 5}]

(* {x -> 4.2916} *)


More generally, you can find an arbitrary number of x values for which your equation holds using the FindInstance function:

FindInstance[f[x] == g[x], x, Reals, 10] // N

(*{
{x->69.7947},{x->500.259},{x->528.612},
{x->296.147},{x->299.185},{x->233.205},
{x->261.482},{x->443.708},{x->318.036},
{x->239.604}
}
*)

• Surely you meant to write g[x] < -5 rather than g[x] > 5. – m_goldberg May 1 '15 at 3:50
• @m_goldberg I think I did actually mean g[x]>5. g[x] has a discontinuity at $x=-2$; on its other side, which is not shown in the plot, g[x] is always larger than 5. I will widen the plot range to make that point more clear. – MarcoB May 1 '15 at 4:38

If you restrict your interest to a finite range, say -2 <= x <= 20, you can use NSolve or Solve

f[x_] = 5 Sin[2 x];
g[x_] = (5 x + 2)/(x + 2);

soln = NSolve[{f[x] == g[x], -2 <= x <= 20}, x]


{{x -> -1.10963}, {x -> 0.124782}, {x -> 1.30019}, {x -> 3.53708}, {x -> 4.2916}, {x -> 6.76161}, {x -> 7.36518}, {x -> 9.94844}, {x -> 10.4663}, {x -> 13.1197}, {x -> 13.5804}, {x -> 16.2826}, {x -> 16.7017}, {x -> 19.4406}, {x -> 19.8276}}

Solve will return Root objects which can be converted to values with N

soln == Solve[{f[x] == g[x], -2 <= x <= 20}, x] // N


True

NSolve and FindRoot as already shown are the most relevant.

Just for fun. Noting that for x>0, g[x] approaches 5 from below as x approaches infinity. This implies an infinite number of zeroes for f[x]-g[x]. Just looking at [-1.9,2] for visualization and extraction of approximations from plot:

plt = Plot[{f[x], g[x]}, {x, -1.9, 5}, Exclusions -> All,
MeshFunctions -> (f@#1 - g@#1 &), Mesh -> {{0.}},
MeshStyle -> {Red, PointSize[0.02]}]
ext = Union[
Extract[plt[[1, 1]], List /@ Cases[plt, Point[x__] :> x, -1]][[1,
All, 1]], SameTest -> (Abs[#1 - #2] < 0.0001 &)]


The approximate roots: {-1.10963, 0.124784, 1.30019, 3.53708, 4.2916}.

Threshold for SameTest just a quick guess (modify as desired).

• This is very cool! +1 – MarcoB May 1 '15 at 7:50
• @MarcoB thank you :) – ubpdqn May 1 '15 at 8:15