这个列表是一个简单的函数,它将一个二维点映射到一个数字,如果
你把每一个{{x,y},z}
如 f[x,y]=z
{
{{1,3},9}, {{1,4},16},
{{2,4},8}, {{2,5},10}
}
我现在想要一个内插/外推的函数
f[x,y]
对于任何 {x,y}
.Mathematica 拒绝这样做:
Interpolation[{{{1,3},9}, {{1,4},16},{{2,4},8}, {{2,5},10}},
InterpolationOrder->1]
Interpolation::indim: The coordinates do not lie on a structured tensor product grid.
我明白为什么(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}]
FunctionInterpolation::nreal:
16 Near {x, y} = {1, --}, the function did not evaluate to a real number. 5 FunctionInterpolation::nreal:
17 Near {x, y} = {1, --}, the function did not evaluate to a real number. 5 FunctionInterpolation::nreal:
18 Near {x, y} = {1, --}, the function did not evaluate to a real number. 5 General::stop: Further output of FunctionInterpolation::nreal will be suppressed during this calculation.
即使您忽略上述警告,评估 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))))))
另一个“明显”的想法(插值插值函数
他们自己)也不起作用。
最佳答案
如果多项式插值可接受, 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}
哼!
关于wolfram-mathematica - 强制 Mathematica 在非结构化张量网格上进行插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3242972/