I have a 3D surface given in data-points of the form ${x,y,z}$. What is the easiest way to get the interpolated value $z=f(X,Y)$ for given coordinates ${X,Y}$ (which are of course not in the data list)?
Answer
Mathematica's interpolation function, Interpolation, works on multidimensional data. For example,
data = Flatten[Table[{x, y, x^2 + y^2}, {x, -10, 10}, {y, -10, 10}], 1];
int = Interpolation[data];
Then, you can extract the values for values between the data points:
int[1.1, 1.1]
(* ==> 2.42 *)
And Plot3D, or whatever else you want.
Plot3D[int[x, y], {x, -10, 10}, {y, -10, 10}]

Note, that the interpolation is pretty good:
exact[x_, y_] := x^2 + y^2
int[1.1, 1.1] == exact[1.1, 1.1]
(* => True *)
Or better yet (thanks @rcollyer):
(int[1.1, 1.1] - exact[1.1, 1.1])/exact[1.1, 1.1]
(* 1.83508*10^-16 *)
Update Leonid's comment below pointed out that the accuracy of Interpolation will be worse with an unstructured grid. For example:
dataDelete = Delete[data, RandomInteger[{1, Length[data]}]]
intD = Interpolation[dataDelete]
Then,
(intD[1.1, 1.1] - exact[1.1, 1.1])/exact[1.1, 1.1]
(* ==> 0.0743802 *)
which is worse. It seems particularly bad close to the origin:
Plot3D[(intD[x, y] - exact[x, y])/ exact[x, y], {x, -10, 10}, {y, -10, 10}]

Comments
Post a Comment