Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Working with functions defined by Interpolation in Mathematica

  1. Oct 13, 2011 #1
    Working with functions defined by "Interpolation" in Mathematica


    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!

    Best regards,
  2. jcsd
  3. Oct 13, 2011 #2
    Re: Working with functions defined by "Interpolation" in Mathematica

    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}

    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]}}


    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).
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook