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.
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.
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}
}
*)
g[x] < -5
rather than g[x] > 5
.
$\endgroup$
Commented
May 1, 2015 at 3:50
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.
$\endgroup$
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).