2010-07-14 63 views
4

这份名单插值是一个简单的函数,一个2D点映射到一个号码,如果 把每个{{x,y},z}f[x,y]=z力数学对非结构化张格

{ 
{{1,3},9}, {{1,4},16}, 
{{2,4},8}, {{2,5},10} 
} 

我现在想要一个函数,插值/外推​​任何{x,y}

数学拒绝这样做:

Interpolation[{{{1,3},9}, {{1,4},16},{{2,4},8}, {{2,5},10}}, 
InterpolationOrder->1] 

插值:: indim:本 坐标不处在一个结构性 张量积网格。

我明白为什么(数学想要一个“矩形”域),但 什么是强迫数学创建插值最简单的方法?

这不起作用:

f[1,3]=9; f[1,4]=16; f[2,4]=8; f[2,5]=10; 
g=FunctionInterpolation[f[x,y],{x,1,2},{y,3,5}] 

FunctionInterpolation :: nreal:
16附近{X,Y} = {1, - },这个函数不 计算为一个真正的数。 5 FunctionInterpolation :: nreal:
17在{x,y} = {1, - }附近,函数没有将 评估为实数。 5 FunctionInterpolation :: nreal:
18在{x,y} = {1, - }附近,函数没有将 评估为实数。 5 General :: stop:进一步输出 FunctionInterpolation :: nreal 将在此计算过程中被抑制。

即使你忽略上面的警告,评估摹给出错误

g[1.5,4] // FortranForm 


    f(1.5,4) + 0.*(-9.999999999999991*(f(1.4,4) - f(1.5,4)) + 
-  0.10000000000000009* 
-  (9.999999999999991* 
-   (9.999999999999991*(f(1.4,4) - f(1.5,4)) + 
-   4.999999999999996*(-f(1.4,4) + f(1.6,4))) + 
-   0.5000000000000006* 
-   (-10.000000000000014* 
-    (-3.333333333333333*(f(1.3,4) - f(1.6,4)) - 
-    4.999999999999996*(-f(1.4,4) + f(1.6,4))) - 
-   9.999999999999991* 
-    (9.999999999999991*(f(1.4,4) - f(1.5,4)) + 
-    4.999999999999996*(-f(1.4,4) + f(1.6,4)))))) 

其他“明显”的想法(内插值功能 本身)也不起作用。

回答

6

如果多项式插值是可以接受的,InterpolatingPolynomial你想要做什么(其中data是您点的列表上方):

In[63]:= InterpolatingPolynomial[data, {x, y}] 

Out[63]= -24 + x (12 - 5 y) + 12 y 

In[64]:= f[2, 3] 

Out[64]= 6 

你也可以使用Fit做最小二乘上的函数的线性组合拟合在第二个参数中指定:

In[65]:= Fit[Flatten /@ data, {1, x, y}, {x, y}] 

Out[65]= 4.75 - 8. x + 4.5 y 

当然,拟合的函数可能不会精确地插入您的数据点。如果这样的配件是可以接受的,虽然,FindFit可以适合任何(线性或非线性)模型函数指定:

In[72]:= FindFit[Flatten/@data, x y (a Sin[x] + b Cos[y]) + c, {a,b,c}, {x,y}] 

Out[72]= {a -> -0.683697, b -> 0.414257, c -> 15.3805} 

HTH!

1

不幸的是,多项式太夸张了,但是线性函数并不是 就足够了。我相信正确的模型是几个线段, ,但他们都会有不同的斜坡。

这是一个可怕的解决方法,可以做我想做的事情。


(* data in format {{x,y},z} *) 
data = {{{1,3},9}, {{1,4},16}, {{2,4},8}, {{2,5},10}} 

(* find the ranges of x and y *) 
datax = DeleteDuplicates[Transpose[Transpose[data][[1]]][[1]]] 
datay = DeleteDuplicates[Transpose[Transpose[data][[1]]][[2]]] 

(* extract the values of y and z for each x *) 
datamap[t_]:=Map[{#[[1,2]], #[[2]]} &, Select[data, #[[1,1]] == t &]] 

(* interpolate for each value of x, create a rectangular array, and then 
    interpolate in y *) 
Map[(f[#]=Interpolation[datamap[#],InterpolationOrder->1])&, datax] 

(* and now apply f to the expanded grid I've created *) 

datatab = Flatten[Table[ 
{{datax[[i]], datay[[j]]}, f[datax[[i]]][datay[[j]]]}, 
{i,1,Length[datax]}, {j,1,Length[datay]}], 1] 

(* now mathematica will let me interpolate *) 
dataint = Interpolation[datatab, InterpolationOrder->1] 

(* The resulting function agrees with my original*) 

Flatten[Table[{{x,y},dataint[x,y]},{x,1,2},{y,3,5}],1] 

Out[29]= {{{1, 3}, 9}, {{1, 4}, 16}, {{1, 5}, 23}, {{2, 3}, 6}, {{2, 4}, 8}, 
{{2, 5}, 10}} 

(* above contains all my original points [plus a few extra] *) 

(* and does a reasonable job of interpolating *) 

dataint[1.5,3.5] 

9.75 

which is the average of the four corner values: 

{dataint[1,3], dataint[1,4], dataint[2,3], dataint[2,4]} 

{9, 16, 6, 8}