这个列表是一个简单的函数,它将一个2D点映射为一个数字,如果您将每个{{x,y},z}看作f[x,y]=z
{
{{1,3},9}, {{1,4},16},
{{2,4},8}, {{2,5},10}
} 现在,我想要一个对任何f[x,y]进行插值/外推的函数。
Mathematica拒绝这样做:
Interpolation[{{{1,3},9}, {{1,4},16},{{2,4},8}, {{2,5},10}},
InterpolationOrder->1] 插值::indim:坐标不位于结构化张量积网格上。
我明白为什么(Mathematica想要一个“矩形”域),但是最简单的方法是怎样迫使Mathematica创建一个插值呢?
这不管用:
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}] 函数::nreal:
16在{x,y} = {1,-}附近,函数没有计算成实数。5功能补充::nreal:
17在{x,y} = {1,-}附近,函数没有计算为实数。5功能补充::nreal:
18在{x,y} = {1,-}附近,函数没有计算成实数。5总则::停止:函数的进一步输出::nreal在此计算过程中将被抑制。
即使忽略了上面的警告,计算g也会产生错误。
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)))))) 另一个“明显”的想法(插值插值函数本身)也不起作用。
发布于 2010-07-14 05:33:54
如果多项式插值是可以接受的,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}哈哈!
发布于 2011-05-07 06:26:11
请用我的包裹!
http://library.wolfram.com/infocenter/MathSource/7760/
发布于 2010-08-06 03:16:32
不幸的是,多项式太摇摆不定了,但是线性函数还不够摆动。我相信正确的模型是几个线段,但它们都有不同的斜率。
这是一个可怕的解决办法,它能做我想做的事。
(* 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} https://stackoverflow.com/questions/3242972
复制相似问题