# Solve an interpolating function [duplicate]

I have been working with interpolating functions recently and have two functions as a result of an interpolation of two co-ordinate lists. The code I used:

    tp = 27250
xAvpTot = Transpose[{xALit, pTotLitPa}];
interpolxAvpTotb = Interpolation[xAvpTot];
NSolve[tp == interpolxAvpTotb[xA], xA]
Plot[interpolxAvpTotb[x],{x,0,1}]


Gives an output:

{{xA -> 0.387577}}


I am trying to get the other solution to this equation as visually, there are two solutions. Thanks so much!

• It is a bit difficult without the specific code you used. Have you tried FindRoot for example? Dec 12 '20 at 10:48
• @DiSp0sablE_H3r0 Thanks for the advice, this is my first post here. I have edited the post and inserted my code. Thanks so much! Dec 12 '20 at 11:04
• Hi again, the quantities xALit, pTotLitPa are undefined and when I run the code on my laptop it just generates errors. Let me give you some further suggestions. If the code to generate the said quantities is too large you can still include it. Another alternative is to use a simplified variant of the problem you are facing. Finally, since I cannot try it on my laptop, can you have a look here for the use of FindRoot and check if it solves the task at hand? reference.wolfram.com/language/ref/FindRoot.html Dec 12 '20 at 11:11
• Hi, I did not include the lists due to their leangth, I am working on making a dataset which reflects mine. I have also realized that the synthax I used for FindRoot was incorrect and Now it works :-) Should I delete this question since I have figured out the problem? @DiSp0sablE_H3r0 Dec 12 '20 at 11:25
• No no. No need to delete the question if you don't want to. You can leave it as it is Dec 12 '20 at 11:27

Since you did not give your lists I generate one below for the sake of example. You may operate the same way with your original lists.

This is the list for the sake of example:

lst = Table[{x, x^2*Exp[-x]}, {x, 0, 7, 0.1}];


Let us make its interpolation:

f = Interpolation[lst, InterpolationOrder -> 2]


We will later look for the solution of the equation f[x]==0.4. Let us draw the list, the interpolation function and y=0.4 together:

Show[{
ListPlot[lst],

Plot[{f[x], 0.4}, {x, 0, 7}, PlotStyle -> {Red, Green}]
}]


Here the points show the list, the red solid line shown the interpolation function, and the green one displays y=0.4. One can see that there are two solutions: one close to x=1 and the other - close to x=3.5. Let us find them:

FindRoot[f[x] == 0.4, {x, 1}]
FindRoot[f[x] == 0.4, {x, 3.5}]

(*  {x -> 1.09162}

{x -> 3.31045}   *)


Have fun!

NSolve does not deal well with InterpolatingFunction, and for simple equations, it seems to return only the first root, which is the output of InverseFunction[iFN][1/2] in the example iFN below. Here's a way to find the roots, from my answer here (see also @CarlWoll's answer to the same question):

iFN = Interpolation@Table[{x, Sin[x]}, {x, 0., Pi, 0.1}];

nsol[f_, {x_, a_, b_}] := Module[{pf, cand, rts},
pf = Evaluate[f /. Equal -> Subtract /. x -> #] &;
ReduceAnalyticRootIsolation;
cand = Quiet@SystemTRootsDumpGuessRealRoots[pf, {a, b}];
rts = Flatten@Replace[cand, {x0_Real :> FindRoot[pf, {x0}]}, 1];
rts = Select[rts, Chop[pf[#]] == 0 && a <= # <= b &];
rts = Union[rts, SameTest -> (Chop[#1 - #2] == 0 &)];
{x -> #} & /@ rts
];

nsol[iFN[x] == 1/2, {x, 0., 3}]

(*  {{x -> 0.5236}, {x -> 2.61799}}  *)
`