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!
Answer
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}]}]
Comments
Post a Comment