Working with functions defined by Interpolation in Mathematica

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 4K views
evgenx
Messages
14
Reaction score
0
Working with functions defined by "Interpolation" in Mathematica

Hello,

Just perhaps a simple question for a Mathematica expert:

I have a function of two variables f(a,b) defined using Interpolation option
in Mathematica. I am wondering how to determine the value of one of the
variable if I know the value of the other variable and the value of the function.
Many thanks!


Evgeniy
 
Physics news on Phys.org


So you know f[x,y]==z for some y and z
Then to find x you need to use a numerical method to find when f[x, y] - z == 0.
Note that, depending on the function, this solution won't necessarily be unique.

Anyway, here's a test function:

data = Flatten[Table[{{x, y}, E^(x + y)}, {x, 4}, {y, 4}], 1]

f = Interpolation[data]

which you can visualize using

ContourPlot[f[x, y], {x, 1, 4}, {y, 1, 4}]

Then if, e.g., y=3, z=140, what does x = ?

In[]:= FindRoot[f[x, 3] == 140, {x, 2.5}]
Out[]= {x -> 1.93157}

Check:
In[]:= Exp[4.93157]
Out[]= 138.597

It's not exactly right, but pretty good considering how few points were used for the interpolation.

In general you can construct a function that gives the solution using InverseFunction:

In[26]:= Solve[g[x, y] == z, x]
Out[26]= {{x -> InverseFunction[g, 1, 2][z, y]}}

So

In[28]:= InverseFunction[f, 1, 2][140, 3] // N
Out[28]= 1.93157

which matches the FindRoot approach (and probably uses the same or similar algorithm internally).