Defining a function to estimate a function's inverse?

I am trying to create a function that can be used to estimate the values of a function’s inverse, in particular when the inverse function cannot be determined explicitly. The estimated values of this function will be used on plotting the graph of another function.

For simplicity suppose the function I am trying to construct was inv[m]. I would then need to plot something roughly like:

Plot[m*inv[m]+NIntegrate[ f[t], {t,0,inv[m]}],{m,0,4}]

I have attempted to use NSolve to define the function inv, but have not had any luck.

Any advice is appreciated.

Steve, you can use InverseFunction; here is an example:

(* define some invertible - not necessarily analytic - function 'f': *)
pts = Table[N[{x, x + Sin[x]}], {x, 0, 10}];
f = Interpolation[pts];
(* define the inverse function: *)
inv = InverseFunction[f];
(* plot the respective expression: *)
Plot[m*inv[m] + NIntegrate[f[t], {t, 0, inv[m]}], {m, 0, 4}]

You are a life saver. Thank you!

No need for InverseFunction. Simply reverse the pairs of points to interpolate:

Plot[x + Sin[x], {x, 0, 10}]
ptsReversed = Table[N[{x + Sin[x], x}], {x, 0, 10, 1/10}];
ListPlot@ptsReversed
inv = Interpolation[ptsReversed, InterpolationOrder -> 1]
Plot[inv[x], {x, 0, inv["Domain"][[1, 2]]}]

I almost have everything working as intended, but have run into an issue. My code is below (some of the items like l and u are not being used yet). I want to be able to enter a function f[x] in an input field and then inv is supposed to be the inverse of its derivative (the function is Cos[x] by default) obtained numerically. In creating pts using Table, when i use f’[x] for the derivative (or even D[f[x],x]), it doesn’t work. However, when I replace it with -Sin[x], it does. I need pts to be generated based on whatever function is entered in the Input Field (associated with the variable p).

I am fairly new to Mathematica Programming which may be evident in my coding. Thanks

Manipulate[
 ClearAll[f, g];
 f[x_] := p;
 pts = Table[N[{x, f'[x]}], {x, a, b, .01}];
 fn = Interpolation[pts];
 inv = InverseFunction[fn];
 l = Min[{f'[a], f'[b]}];
 u = Max[{f'[a], f'[b]}];
 Plot[{NIntegrate[inv[t], {t, 0, m}]}, {m, -.99, .99}, 
  PlotStyle -> {Yellow, Dashed}, AxesOrigin -> {0, 0}],
 {{p, Cos[x], 
   Text[Style[TraditionalForm["f(x)= "], Blue, Italic, 18]]}, 
  ControlType -> InputField},
 Row[
  {
   Control[{{a, -1.57, 
      Text[Style[TraditionalForm["a= "], Blue, Italic, 12]]}, 
     ControlType -> InputField, ImageSize -> Tiny}],
   Control[{{b, 1.57, 
      Text[Style[TraditionalForm["b= "], Blue, Italic, 12]]}, 
     ControlType -> InputField, ImageSize -> Tiny}]
   }
  ],
 TrackedSymbols :> Manipulate
 ]

Dear @Steve Kilner, dear @Gianluca Gorni,

in my first reply above I used interpolation only to generate some (as I said) not necessarily analytic function. (Then reversing the points here would be somewhat “pointless”.) Of course there is no need to do it like so in general when an inverse is wanted. And as you input an analytic expression it is certainly not necessary here. Without having looked at the code too closely, this modification seems to point into the right direction:

ClearAll[f, g, xx];
Manipulate[
 f[xx_] := Evaluate[p /. x -> xx];
 inv = InverseFunction[f];
 l = Min[{f'[a], f'[b]}];
 u = Max[{f'[a], f'[b]}];
 Plot[NIntegrate[inv[x], {x, 0, m}], {m, -.99, .99}, 
  PlotStyle -> Directive@{Yellow, Dashed}, 
  AxesOrigin -> {0, 0}], {{p, Cos[x], 
   Text[Style[TraditionalForm["f(x)= "], Blue, Italic, 18]]}, 
  ControlType -> InputField}, 
 Row[{Control[{{a, -1.57, 
      Text[Style[TraditionalForm["a= "], Blue, Italic, 12]]}, 
     InputField, ImageSize -> Tiny}], 
   Control[{{b, 1.57, 
      Text[Style[TraditionalForm["b= "], Blue, Italic, 12]]}, 
     ControlType -> InputField, ImageSize -> Tiny}]}], 
 SynchronousUpdating -> True]

The problem now basically is the invertibility of Cos[x] - for comparison try e.g. simply x.