Mathematica Working with functions defined by Interpolation in Mathematica

AI Thread Summary
In Mathematica, when working with a function f(a, b) defined through interpolation, determining the value of one variable given the other and the function's output requires numerical methods. Specifically, if you know f[x, y] = z for a fixed y and z, you can use the FindRoot function to solve for x by setting up the equation f[x, y] - z = 0. An example provided demonstrates this with a test function based on exponential data, where for y = 3 and z = 140, the solution for x is approximately 1.93157. The accuracy of this solution is noted to be reasonable given the limited data points used for interpolation. Additionally, the InverseFunction can be utilized to construct a general solution for x in terms of z and y, yielding consistent results with the numerical approach.
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).
 

Similar threads

Replies
4
Views
3K
Replies
4
Views
1K
Replies
5
Views
3K
Replies
2
Views
2K
Replies
1
Views
3K
Replies
13
Views
2K
Back
Top