Group Abstract Group Abstract

Message Boards Message Boards

Defining a function to estimate a function's inverse?

Posted 4 years ago
POSTED BY: Steve Kilner
5 Replies
POSTED BY: Henrik Schachner

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]]}]
POSTED BY: Gianluca Gorni

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}]
POSTED BY: Henrik Schachner
Posted 4 years ago

You are a life saver. Thank you!

POSTED BY: Steve Kilner
Posted 4 years ago

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
 ]
POSTED BY: Steve Kilner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard