2
$\begingroup$

I am trying to create the interpolating function for a function of two variables, over a finite area. Just for consistency we can think of a function:

MyFunc[a_,b_]:=Sin[a*b]/Sqrt[1+a^2+b^2]

I've read the documentation and, in order to get the interpolating function I use:

MyTable=Table[MyFunc[a,b],{a, -5, 5, 0.1}, {b, -5, 5, 0.1}]
MyApproximateFunc = ListInterpolation[MyTable]

this seem to work, but when I try to plot MyFunc and MyApproximateFunc they look very different: I must have missed some detail...

Plot3D[MyFunc[a, b], {a, -5, 5}, {b, -5, 5}]
Plot3D[MyApproximateFunc[a, b], {a, -5, 5}, {b, -5, 5}]

Thanks in advance for your kind help!

$\endgroup$
  • 2
    $\begingroup$ its because your MyTable does not know about the corresponding values of a and b; try MyTable = Table[{a, b, MyFunc[a, b]}, {a, -5, 5, 0.1}, {b, -5, 5, 0.1}] // Flatten[#, 1] &; MyApproximateFunc = Interpolation[MyTable] $\endgroup$ – chris Nov 5 '12 at 17:34
  • $\begingroup$ If you read the docs, notice that ListInterpolation[] supports a domain specification; try MyApproximateFunc = ListInterpolation[MyTable, {{-5, 5}, {-5, 5}}]. $\endgroup$ – J. M. is in limbo Nov 5 '12 at 17:40
  • $\begingroup$ chris: works like a charm! Too bad it's only a comment and I can't accept it... :-) $\endgroup$ – zakk Nov 5 '12 at 17:41
  • $\begingroup$ @zakk you can always upvote my comment ;-) $\endgroup$ – chris Nov 5 '12 at 18:40
  • $\begingroup$ C'mon @chris move your comment to an answer, I promise I won't tell. $\endgroup$ – Sjoerd C. de Vries Nov 5 '12 at 19:13
5
$\begingroup$

OK then after Sjoerd C. de Vries request ;-), you can either use (less thinking)

MyTable = Table[{a, b, MyFunc[a, b]}, {a, -5, 5, 0.1}, {b, -5, 5, 0.1}] // Flatten[#, 1] &; 
MyApproximateFunc = Interpolation[MyTable];

or (from @J.M.'s comment, less memory)

 MyTable = Table[MyFunc[a, b], {a, -5, 5, 0.1}, {b, -5, 5, 0.1}] // Flatten[#, 1] &; 
 MyApproximateFunc = ListInterpolation[MyTable, {{-5, 5}, {-5, 5}}];

to get

  GraphicsRow[{Plot3D[MyFunc[a, b], {a, -5, 5}, {b, -5, 5}],
               Plot3D[MyApproximateFunc[a, b], {a, -5, 5}, {b, -5, 5}]}]

Mathematica graphics

$\endgroup$
4
$\begingroup$

Another possibility is FunctionInterpolation. Straight from the Documentation:

   MyApproximateFunc = 
    FunctionInterpolation[
     Evaluate[Table[D[MyFunc[a, b], {{a, b}, k}], {k, 0, 2}]], {a, -5, 
      5}, {b, -5, 5}]

We have to provide derivatives to get a better interpolation.

GraphicsRow[{Plot3D[MyFunc[a, b], {a, -5, 5}, {b, -5, 5}], 
  Plot3D[MyApproximateFunc[a, b], {a, -5, 5}, {b, -5, 5}]}]

enter image description here

$\endgroup$
  • $\begingroup$ @Markus.Roellig ah! I knew that there was a way to tell interpolation to use the derivative as well. Thanks for the tip! $\endgroup$ – chris Nov 6 '12 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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