Wolfram mathematica 强制Mathematica在非结构化张量网格上插值
此列表是一个简单的函数,用于将二维点映射到数字(如果需要) 您认为每个Wolfram mathematica 强制Mathematica在非结构化张量网格上插值,wolfram-mathematica,interpolation,Wolfram Mathematica,Interpolation,此列表是一个简单的函数,用于将二维点映射到数字(如果需要) 您认为每个{{x,y},z}都是f[x,y]=z { {{1,3},9}, {{1,4},16}, {{2,4},8}, {{2,5},10} } 我现在想要一个函数,它可以为任何{x,y}插值/外推f[x,y] Mathematica拒绝这样做: Interpolation[{{{1,3},9}, {{1,4},16},{{2,4},8}, {{2,5},10}}, InterpolationOrder->1
{{x,y},z}
都是f[x,y]=z
{
{{1,3},9}, {{1,4},16},
{{2,4},8}, {{2,5},10}
}
我现在想要一个函数,它可以为任何{x,y}
插值/外推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))))))
另一个“明显”的想法(插值函数
(他们自己)也不起作用 如果多项式插值是可接受的,则执行您想要的操作(其中
data
是您上面的点列表):
您还可以使用对第二个参数中指定的函数的线性组合进行最小二乘拟合:
In[65]:= Fit[Flatten /@ data, {1, x, y}, {x, y}]
Out[65]= 4.75 - 8. x + 4.5 y
当然,拟合函数可能无法精确插值数据点。但是,如果可以接受此类拟合,则可以拟合到指定的任何(线性或非线性)模型函数:
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}
嗯 不幸的是,多项式太摇摆了,而线性函数却不是 扭动得够厉害了。我认为正确的模型是几个线段, 但它们都有不同的坡度 这是一个可怕的解决办法,它符合我的要求
(* 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}
请用我的包裹
Yasushi Iwasaki,欢迎来到Stacka社区,感谢您的贡献。
(* 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}