2
$\begingroup$

Considering a well defined function

f[x_]:=Tan[Sqrt[x]]/Sqrt[x] (*x>= 0*) 

I tried to evaluate the inverse function and get an unexpected result

Plot[ InverseFunction[Function[{x}, Tan[Sqrt[x]]/Sqrt[x]]][z] , {z, -5, 5}, PlotRange -> {0, Automatic}]

in the range x<0. Is this a plotting problem or a bug in InverseFunction? Thanks! enter image description here

$\endgroup$
3
  • $\begingroup$ f[x_]:=Tan[Sqrt[x]/Sqrt[x] is not well defined, as it has a syntax error :) $\endgroup$
    – ktm
    Feb 1, 2018 at 15:22
  • $\begingroup$ Now it's well defined. Thanks. $\endgroup$ Feb 1, 2018 at 15:24
  • $\begingroup$ Interestingly Wolframalpha does plot the inverse correctly ! $\endgroup$ Feb 1, 2018 at 16:41

3 Answers 3

4
$\begingroup$

The problem is simply that your inverse is not single valued

FindRoot[Tan[Sqrt[x]]/Sqrt[x] == -4 , {x, 2700}]
FindRoot[Tan[Sqrt[x]]/Sqrt[x] == -4 , {x, 23}]
FindRoot[Tan[Sqrt[x]]/Sqrt[x] == -4 , {x, 3}]

2371.67

22.7034

2.94296

InverseFunction doesn't always find the lowest..

 InverseFunction[Function[{x}, Tan[Sqrt[x]]/Sqrt[x]]][-4] // N

22.7034 + 0. I

There are actually an infinity of inverses each where Sqrt[x] is approximately n Pi / 2, n odd.

Note if you just want to make the plot you can use

ParametricPlot[{Tan[Sqrt[x]]/Sqrt[x], x}, {x, -5, 7},
     AspectRatio -> 1/GoldenRatio, Exclusions -> {x == Pi^2/4}, 
     PlotRange -> {{-10, 10}, Automatic}] 
$\endgroup$
1
  • $\begingroup$ Thank You, I didn't see it... $\endgroup$ Feb 2, 2018 at 9:19
1
$\begingroup$

There seems to be an issue with InverseFunction, might be worth reporting. A workaround is to use a black box function instead:

f[x_Real]:=Tan[Sqrt[x]]/Sqrt[x]
if = InverseFunction[f[#]&];

Plot[if[x], {x, -5, 5}]

enter image description here

Addendum

The OP asked about the "missing" inverse when $x<0$. The issue here is that the inverse has multiple branches. The "missing" inverse actually belongs to a different branch. This can be seen by looking at ContourPlot:

ContourPlot[
    Tan[Sqrt[y]]/Sqrt[y] == x, {x, -5, 5}, {y, -5, 30},
    ContourShading->False
]

enter image description here

Rather than trying to come up with an inverse that switches branches, I think it makes more sense to have a different inverse function for each branch. A simple way to do this is to use NDSolveValue:

(* equation *)
eq[x_] = Tan[Sqrt[y[x]]]/Sqrt[y[x]] == x

(* initial point *)
y2 = y[2] /. First @ NSolve[eq[2] && 15<y[2]<25]

(* NDSolve *)
sol = NDSolveValue[{eq'[x], y[2] == y2}, y, {x, -5, 5}];

And a visualization:

Plot[sol[t], {t, -5, 5}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks: But I'm missing the branch x<0... $\endgroup$ Feb 2, 2018 at 8:31
0
$\begingroup$
f[x_] := Tan[Sqrt[x]]/Sqrt[x]

f[0] = f[0.] = Limit[f[x], x -> 0]

(* 1 *)

FunctionDomain[f[x], x]

(* NotElement[1/2 + Sqrt[x]/π, Integers] && x > 0 *)

s = x /. Solve[1/2 + Sqrt[x]/π == 1, x][[1]]

(* π^2/4 *)

if[y_?NumericQ] := x /. NSolve[{f[x] == y, 0 <= x < 10}, x][[1]]

Plot[if[y], {y, -5, 5},
 AxesLabel -> (Style[#, 14, Bold] & /@ {y, x}),
 Epilog -> {Gray, Dashed, Line[{{-5, s}, {5, s}}]}]

enter image description here

EDIT:

$Version

(* "11.2.0 for Mac OS X x86 (64-bit) (September 11, 2017)" *)

if (which is to say NSolve) fails for some values

if[1.00001]

(* Part::partw: Part 1 of {} does not exist.

ReplaceAll::reps: {{}[[1]]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

x /. {}[[1]] *)

Redefine f as

f2[x_] = Sinc[Sqrt[x]]/Cos[Sqrt[x]];

f and f2 are equivalent except that f2[0] is defined without adding a separate definition.

f[x] == f2[x] // FunctionExpand

(* True *)

f2[0]

(* 1 *)

if2[y_?NumericQ] :=
 x /. NSolve[{f2[x] == y, 0 <= x < 10}, x][[1]]

Plot[if2[y], {y, -5, 5},
 AxesLabel -> (Style[#, 14, Bold] & /@ {y, x}), 
 Epilog -> {Gray, Dashed, Line[{{-5, s}, {5, s}}]}]

enter image description here

if2[1.00001]

(* 0.0000299996 *)
$\endgroup$
2
  • $\begingroup$ Looks fine, thanks. But near y=1 I get unexplainable results: if[1.0001]==0.000299967 (*ok*), but if[1.00001]==20.1907 (* ??? *) $\endgroup$ Feb 2, 2018 at 9:05
  • $\begingroup$ @UlrichNeumann - Yes. On my Mac with version 11.2.0, NSolve fails for if[1.00001] and result is x /. {}[[1]] along with associated error messages. $\endgroup$
    – Bob Hanlon
    Feb 2, 2018 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.