3
$\begingroup$

I measured a parameter over time and get this List: (where fisrt value (x) is time and second the value (y) the vale of the parameter)

j = List[{0, 0.205004}, {0.1, 0.259237}, {0.2, 1.059125}, {0.3, 0.832184},
         {0.4, 0.587992}, {0.5, 0.565537}, {0.6, 0.527323}];

I did Interpolation:

f = Interpolation[j, InterpolationOrder -> 3, Method -> "Spline"]

and got this Plot:

Plot[f[x], {x, 0, 0.6}]

enter image description here

I want to get the x value for a certain y point.

I tried:

f[0.4]

and

InverseFunction[f][0.4]

But it's giving me the opposite.

Thanks.

$\endgroup$
2
  • $\begingroup$ Works fine for me: f[0.4] gives 0.587992. What is your $Version? $\endgroup$
    – Roman
    Jan 17, 2022 at 13:02
  • $\begingroup$ Sorry, I mistaked. I want to get x value for a certain y value. $\endgroup$
    – Zarabu
    Jan 17, 2022 at 13:04

2 Answers 2

2
$\begingroup$
Clear["Global`*"]

j = {{0, 0.205004}, {0.1, 0.259237}, {0.2, 1.059125}, {0.3, 0.832184}, 
     {0.4, 0.587992}, {0.5, 0.565537}, {0.6, 0.527323}};

f = Interpolation[j, InterpolationOrder -> 3, Method -> "Spline"];

The function range is

{fmin, fmax} = (#[{f[x], 0 <= x <= 0.6}, x] & /@ {MinValue, MaxValue})

(* {-0.0275476, 1.08927} *)

The range of x for this function range is

{xmin, xmax} = (#[{f[x], 0 <= x <= 0.6}, x] & /@ {ArgMin, ArgMax})

(* {0.0424198, 0.219855} *)

To restrict the function such that its inverse is single-valued, require xmin < x < xmax

f2[x_?NumericQ] :=
 ConditionalExpression[f[x], xmin < x < xmax]

Plotting,

Plot[{f[x], f2[x]}, {x, 0, 0.6},
 Frame -> True,
 PlotStyle -> {AbsoluteThickness[0.75], {Red, Dashed}},
 PlotLegends -> Placed[
   {StringForm[
     "``, multi-valued inverse",
     HoldForm[f[x]]],
    StringForm["``, single-valued inverse",
     HoldForm[f2[x]]]},
   {0.6, 0.3}]]

enter image description here

The inverse of f2 is

g[y_?NumericQ] :=
 x /. FindRoot[f2[x] == y, {x, 0.15}]

Plotting the inverse function,

Legended[
 Show[
  ParametricPlot[{f[x], x}, {x, 0, 0.6},
   PlotStyle -> AbsoluteThickness[0.75]],
  Plot[g[y], {y, fmin, fmax},
   PlotStyle -> {Red, Dashed}],
  Frame -> True,
  AspectRatio -> GoldenRatio,
  ImageSize -> 252],
 Placed[
  LineLegend[
   {Directive[ColorData[97][1], AbsoluteThickness[0.75]],
    Directive[Red, Dashed]}, {"multi-valued inverse",
    StringForm["``, single-valued inverse",
     HoldForm[g[y]]]}],
  {0.45, 0.39}]]

enter image description here

$\endgroup$
1
$\begingroup$

Look at your function:

Plot[f[x], {x, 0, 0.52}]

As you can see, for some y values there are several x values. This makes the inverse function multivalued. And MMA seems to take randomly one of several values as you can see:

Plot[InverseFunction[f][x], {x, 0, 1}]

enter image description here

Therefore, using InverseFunction is not a good idea.

Instead try e.g. FindInstance with restrictions on x like:

FindInstance[{f[x] == 0.4, 0 < x, x < 0.2}, x]
(* {{x -> 0.118044}} *)
$\endgroup$
7
  • $\begingroup$ NSolve[{f[x] == 0.4 , 0 < x < 0.6}, x] should give all solutions ( but doesn't evaluate in MMA v12.2 ) $\endgroup$ Jan 17, 2022 at 14:03
  • $\begingroup$ @Ulrich Neumann I think NSolve uses inverse functions. See above. $\endgroup$ Jan 17, 2022 at 14:29
  • $\begingroup$ @DanialHuber Might be, but NSolve[{Sin[x] == .8, 0 < x < 6 Pi}, x] for example works!?! $\endgroup$ Jan 17, 2022 at 14:46
  • $\begingroup$ @Ulrich Neumann "Sin" is not an a numerical interpolated function. $\endgroup$ Jan 17, 2022 at 15:23
  • $\begingroup$ Understood, but InverseFunction of Sin also isn't unique $\endgroup$ Jan 17, 2022 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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