Re: Interpolating data

• To: mathgroup at smc.vnet.net
• Subject: [mg64134] Re: [mg64086] Interpolating data
• From: Bruce Miller <brucem at wolfram.com>
• Date: Thu, 2 Feb 2006 00:06:25 -0500 (EST)
• References: <200602010934.EAA22968@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```On Feb 1, 2006, at 3:34 AM, Noga wrote:
> Hi,
> I'm trying to use the function "Interpolate" on a data of the kind:
>
> aaa = {{0,
>     3, -4.7202`}, {0, 7, 17.5902`}, {-20, 0, -0.418`}, {-10, 0,
> 17.3249`}, {0,
>        0, -9.9187`}, {5,
>         0, -46.7828`}, {17.5`, 0, -51.2055`}, {-15, 5, -5.1367`}, {
>       7.5`, 5, 17.2389`}, {-17.5`, 10, -4.0567`}, {-5, 10, 17.4435`},
> {0, 10,
>       16.4173`}, {5, 10, 9.0035`}, {0, 12.5`, 9.4378`}, {0, 15,
>         15.1786`}, {0, 17.5`, 15.3475`}, {0, -2, -54.6795`}, {20, 0, \
> 17.5902`}, {10, 0, 17.3249`}, {-5, 0, -9.9187`}};
>
> I couldn't understant why it can't do it.
>
> Noga
>

If I understand the problems you are encountering, here is a workaround.

In[1]:=
\$Version

Out[1]=
5.2 for Mac OS X (June 20, 2005)

In[2]:=
aaa = {{0, 3, -4.7202`}, {0, 7, 17.5902`}, {-20, 0, -0.418`},
{-10, 0, 17.3249`}, {0, 0, -9.9187`}, {5, 0, -46.7828`},
{17.5`, 0, -51.2055`}, {-15, 5, -5.1367`}, {7.5`, 5, 17.2389`},
{-17.5`, 10, -4.0567`}, {-5, 10, 17.4435`}, {0, 10, 16.4173`},
{5, 10, 9.0035`}, {0, 12.5`, 9.4378`}, {0, 15, 15.1786`},
{0, 17.5`, 15.3475`}, {0, -2, -54.6795`}, {20, 0, 17.5902`},
{10, 0, 17.3249`}, {-5, 0, -9.9187`}};

(problem 1: The function name is Interpolation.)

In[3]:=
?Interpolate

(problem 2: The data is not on a regular grid.  The interpretation of
the
message is in the Help Browser, Built-In Functions, Warning Messages.
It can be jumped to by clicking on the "inpts" and going to Help
Find Selected Function.)

In[4]:=
f = Interpolation[aaa]

Interpolation::inpts:
The total number 20 of data points is not a multiple of the number 3
of points in dimension 2. More?

Out[4]=
Interpolation[{{0, 3, -4.7202}, {0, 7, 17.5902}, {-20, 0, -0.418},

{-10, 0, 17.3249}, {0, 0, -9.9187}, {5, 0, -46.7828}, {17.5, 0,
-51.2055},

{-15, 5, -5.1367}, {7.5, 5, 17.2389}, {-17.5, 10, -4.0567},

{-5, 10, 17.4435}, {0, 10, 16.4173}, {5, 10, 9.0035}, {0, 12.5,
9.4378},

{0, 15, 15.1786}, {0, 17.5, 15.3475}, {0, -2, -54.6795}, {20, 0,
17.5902},

{10, 0, 17.3249}, {-5, 0, -9.9187}}]

(This shows the x-y layout a little better.  For {x,y,z} data,
Interpolation
needs the data to be a filled-in n by m grid.)

In[5]:=
Sort[aaa]

Out[5]=
{{-20, 0, -0.418}, {-17.5, 10, -4.0567}, {-15, 5, -5.1367},

{-10, 0, 17.3249}, {-5, 0, -9.9187}, {-5, 10, 17.4435}, {0, -2,
-54.6795},

{0, 0, -9.9187}, {0, 3, -4.7202}, {0, 7, 17.5902}, {0, 10, 16.4173},

{0, 12.5, 9.4378}, {0, 15, 15.1786}, {0, 17.5, 15.3475}, {5, 0,
-46.7828},

{5, 10, 9.0035}, {7.5, 5, 17.2389}, {10, 0, 17.3249}, {17.5, 0,
-51.2055},

{20, 0, 17.5902}}

(One workaround - fit the data to a formula/expression.  Knowing the
shape
of the data helps in creating a good function  FindFit returns a list of
parameters, which can be substituted back into the expression.  One
does not
always need to make a function of the result.)

In[6]:=
q = FindFit[aaa, const+a*x+b*x^2+d*y+e*y^2, {a,b,const,d,e},{x,y}]

Out[6]=
{a -> 0.0434479, b -> 0.0122894, const -> -15.194, d -> 4.8771, e ->
-0.19973}

In[7]:=
faaa[x_,y_] = const+a*x+b*x^2+d*y+e*y^2 /. q

Out[7]=
2                       2
-15.194 + 0.0434479 x + 0.0122894 x  + 4.8771 y - 0.19973 y

(This loads enough information on functions from the Graphics` group of
Standard AddOn Packages that they are available if called.
DisplayTogether
from Graphics`Graphics` is a convenient way to combine plots.
ScatterPlot3D is from Graphics`Graphics3D`. )

In[8]:=
<<Graphics`

In[9]:=
DisplayTogether[
Plot3D[ faaa[x,y],{x,-20,20},{y,0,20},
ViewPoint->{2.859, -1.626, 1.231}],
ScatterPlot3D[aaa,BoxRatios->{1,1,1},
PlotStyle->{Hue[0],PointSize[0.03]} ]
];

(Here is another way to compare the data and the fitted function - the
differences between the z data and values of the function at the same
{x,y}
values.  The next line calculates the function at each {x,y} value in
the
original data; the last line subtracts the data's z values from the
function's.)
(The "Apply[faaa,#]&" is a "pure function" that takes a list {x,y} and
turns
it into a function call faaa[x,y].  "Part[aaa,All,{1,2}]" take the
first two
elements from each sublist in aaa.)

In[10]:=
Map[ Apply[faaa,#]&, Part[aaa,All,{1,2}]]

Out[10]=
{-2.36028, 9.15889, -11.1472, -14.3995, -15.194, -14.6695, -10.67,
6.31163,

5.21536, 16.6072, 13.6939, 13.6039, 14.1284, 14.5618, 13.0231,
8.98773,

-25.7471, -9.40927, -13.5306, -15.104}

In[11]:=
% - Part[aaa,All,3]

Out[11]=
{2.35992, -8.43131, -10.7292, -31.7244, -5.2753, 32.1133, 40.5355,
11.4483,

-12.0235, 20.6639, -3.74958, -2.81338, 5.1249, 5.12402, -2.15551,
-6.35977,

28.9324, -26.9995, -30.8555, -5.1853}