# How do I solve this equation?

I want to solve the equation $$f(f(-1)) = \sqrt{2} + g(f(2)),$$ where $$f(x) = \dfrac{a}{x^2} - 3x + 2, \quad g(x) = a\cdot \sin 2x$$ $a$ is unknown. The solution is $a \approx{-5.81}$. I tried

Clear[f];
Clear[g];
f[x_] := a/x^2 - 3 x + 2;
g[x_] := a*Sin[2 x];
Solve[f[f[-1]] == Sqrt[2] + g[f[2]], a]


and

Clear[f];
Clear[g];
f[x_] := a/x^2 - 3 x + 2;
g[x_] := a*Sin[2 x];
Reduce[f[f[-1]] == Sqrt[2] + g[f[2]], a]


but, I got the following error:

"Reduce::nsmet: This system cannot be solved which the methods available to Reduce."

This is a picture of the desired solution that I drew in GeoGebra

How can I solve this?

-

Oftentimes, it is the case that Solve and Reduce cannot find a general solution in $\mathbb{C}$, but if you know a priori that your solution set is in $\mathbb{R}$, then you can call these functions with the domain specification and it will give you the solution (if solvable).

NSolve[f[f[-1]] == Sqrt[2] + g[f[2]], a, Reals]
(* {{a -> -5.81218}} *)


Here's how you can reproduce your GeoGebra plot:

With[{sol = a /. NSolve[f[f[-1]] == Sqrt[2] + g[f[2]], a, Reals] // First},
Plot[{g@f@2, f@f@-1}, {a, -10, 10}, PlotRange -> {-10, 10},
Epilog -> {Red, PointSize[Large], Point[{sol, g@f@2 /. a -> sol}]}]]


-
I am sorry. I ask SOlVE many times, but I don't notice. I will try. –  minthao_2011 Dec 24 '12 at 2:52
I repaired f[x_] := a/x^2 - x + 2; and I get NSolve::nsmet: This system cannot be solved with the methods available to NSolve. –  minthao_2011 Dec 24 '12 at 3:44
@minthao_2011 In that case, you have multiple (possibly infinite) solutions which don't have a general formula, which is why NSolve doesn't return a solution. Restrict the range of a by adding a condition &&amin ≤ a ≤ amax and you'll get the solutions in that range –  rm -rf Dec 24 '12 at 3:56

First of all it is a good programming style is to use functions with their explicit (independent) variables, i.e. if one has to solve an equation with respect to a parameter, it should be treated as an explicit argument. Thus we define the functions this way :

f[x_, a_] := a/x^2 - 3 x + 2
g[x_, a_] := a Sin[2 x]


The question doesn't imply that the solution must be necessarily real. In fact, there is only one real solution, you can find it this way :

Solve[ f[ f[-1, a], a] == Sqrt[2] + g[ f[ 2, a], a], a, Reals]


but since there are (most likely infinitely) many complex solutions and since Mathematica is to find them all, we should tell the system to restrict somehow the search for solutions to a region where it may have only finite numbers of solutions. In order to provide a completion to algorithms behind equation-solving functionality we need to restrict the search for roots to a finite complex range (e.g. square - like ). For the sake of brevity we define a new function :

funcfg[a_] := f[ f[ -1, a], a] - Sqrt[2] - g[ f[ 2, a], a]


Let's find all roots of funcfg e.g. in a circle of radius 51 with the center in 0, i.e. where complex numbers satisfy Abs[a] < 51 :

sol = Solve[ funcfg[a] == 0 && Abs[a] < 51, a];


There are

Length @ sol

21


solutions, so write here only 3 of them, e.g. (from 8-th to 10-th) :

sol[[8 ;; 10, 1, 2]]

{Root[{325 + 25 Sqrt[2] + 204 #1 + 10 Sqrt[2] #1 + 25 Sin[2 (-4 + #1/4)] #1 + 43 #1^2
+ Sqrt[2] #1^2 + 10 Sin[2 (-4 + #1/4)] #1^2 + 3 #1^3 + Sin[2 (-4 + #1/4)] #1^3 &,
-11.953414742816708318902832339138612161461271256190 +
2.454413085381160585476672761493761068260205214286 I}],
Root[{325 + 25 Sqrt[2] + 204 #1 + 10 Sqrt[2] #1 + 25 Sin[2 (-4 + #1/4)] #1 + 43 #1^2
+ Sqrt[2] #1^2 + 10 Sin[2 (-4 + #1/4)] #1^2 + 3 #1^3 + Sin[2 (-4 + #1/4)] #1^3 &,
-5.81217740175980465827061286458}],
Root[{325 + 25 Sqrt[2] + 204 #1 + 10 Sqrt[2] #1 + 25 Sin[2 (-4 + #1/4)] #1 + 43 #1^2
+ Sqrt[2] #1^2 + 10 Sin[2 (-4 + #1/4)] #1^2 + 3 #1^3 + Sin[2 (-4 + #1/4)] #1^3 &,
- 4.131337899047017233724977013012392200221448674862
- 0.658093853165542882211799231745725310996904431942 I}]}


You can see there is that only one real near -5.8121.

roots = {Re @ #, Im @ #}& /@ sol[[All, 1, 2, 1, 2]];


Now we'd like to plot only 3 roots of the function in an especially more restricted region to to discover its trefoil-quatrefoil like structure :

GraphicsRow[
Table[ Show[
ContourPlot @@@ {
{ h[ funcfg[x + I y]], ##, Contours -> 11, ColorFunction -> "DeepSeaColors",
Epilog -> {PointSize[0.025], Red, Point[roots]}},
{ Re[ funcfg[x + I y]] == 0, ##, ContourStyle -> {Darker @ Green, Thick}},
{ Im[ funcfg[x + I y]] == 0, ##, ContourStyle -> {Cyan, Thick}}},
AspectRatio -> Automatic], {h, {Re, Im}}]& @ Sequence[{x, -6, -3.8}, {y, -1.1, 1.1}]]


-
nice colours... –  acl Dec 24 '12 at 13:10