Re: Interpolation[] problem
- To: mathgroup at smc.vnet.net
- Subject: [mg18561] Re: Interpolation[] problem
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Sat, 10 Jul 1999 02:18:51 -0400
- Organization: Wolfram Research, Inc.
- References: <7m14um$pls@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Attico Nicola wrote: > > hi all, > i've an incomprehensible problem with > Interpolation[]. > I need to interpolate a two dimensional > function z(x,y) and i've in a file > the values of the functions in different > points in the form: > > x_i y_i z_i > > (ive copied my file in attchmnt) > > [contact the author to obtain the attachment -moderator] > > Is important that the my grid is homogeneous > in the y direction, but inhomogeneous along x. > Im able to read data in a list with the > command > > ReadList["nomefile",Number,RecordLists->True] > t=% > > and now t is a list of the form: > > {{x1,y1,z1},{x2,y2,z2},,....,{xN,yN,zN}} > > So, i trie to interpolate 2-dimensionally > with: > > f=Interpolation[t] > > as is specified in the manual but Mathematica refuse > to work with the error: > > Interpolation::indim: > -6 -6 -6 > The coordinates {0., 1. 10 , 3. 10 , 6. 10 , 0.000011, <<24>>, > 3.553326, 5.196734} in dimension 1 > are not consistent with other coordinates in this dimension. > > maybe mathematica does not work with inhomogeneous grid. > What can i do otherwise? > > Thanks > > Nicola > > ----- > Nicola Attico > (attico at cibs.sns.it) [data file was obtained from author by e-mail attachment] The problem is that your coordinates do not form a grid, at least in the sense that I understand that term. You use different sets of x-values for the various y-values (maybe this is what you meant by "non-rectangular"). There are a few approaches to get a useful result. One is that, since your sets of x coordinates are close to one another for different values of y, you could simply adjust them. I am fairly certain that the difference in quality of the interpolating function will be unnoticeable. You could, for example, write code to average these. The main impediment is that for some y-values you let x range past 7 while for others you only go to around 5.2 (and for y=1 you only go to x around .034. You should just throw these away). Another possibility would be to create several univariate interpolating functions, then piece them together. For this you might try playing with FunctionInterpolation. An alternative, which I did and reproduce below, is to resample at specific x-values to get a rectangle, then form an interpolating function. For the work below I removed all elements where the y coordinate was 1 from the file tmp.tmp (where I had placed your data). The code below gets job done but I imagine there are cleaner ways to recode it. ee = ReadList["~/tmp.tmp",Number,RecordLists->True]; ff = Split[ee, #1[[2]]==#2[[2]]&]; gg = Map[{Part[#,All,{1,3}],#[[1,2]]}&, ff]; hh = Map[{Interpolation[#[[1]]],#[[2]]}&, gg]; dd = Map[Table[{j,#[[2]],#[[1]][j]}, {j,0.,5.,.1}]&, hh]; data = Flatten[dd,1]; func = Interpolation[data]; Sanity check: In[127]:= func[.1,.1] Out[127]= 0.881687 After alot of trial, error, and ugly code I also implemented code to do the averaging over the x coordinates. I start with the same data as above. ee = ReadList["~/tmp.tmp",Number,RecordLists->True]; ff = Split[ee, #1[[2]]==#2[[2]]&]; ff2 = Map[Function[a, Select[a, #[[1]]<6&]], ff]; tlists = Map[Transpose, ff2]; xvallists = Map[First, tlists]; xvals = Apply[Plus,xvallists] / Length[xvallists]; ff3 = Map[ReplacePart[#,xvals,1]&, tlists]; data2 = Flatten[Map[Transpose, ff3], 1]; func2 = Interpolation[data2]; Sanity recheck: In[182]:= func2[.1,.1] Out[182]= 0.881688 It is perhaps worthwhile to compare these methods in an area where the x-sampling was fairly sparse. In[184]:= func[3,.7] Out[184]= 0.156986 In[185]:= func2[3,.7] Out[185]= 0.156993 We still get reasonable agreement. Daniel Lichtblau Wolfram Research