我想要“修改”Mathematica的Interpolation[]函数(在一维中),当输入超出范围时,用常量值替换外推。
换句话说,如果插值域是1,20,f1==7和f20==12,我想:
f[x] = 7 for x<=1
f[x] = 12 for x>=20
f[x] = Interpolation[...] 但是,这会失败:
(* interpolation w cutoff *)
interpcut[r_] := Module[{s, minpair, maxpair},
(* sort array by x coord *)
s = Sort[r, #1[[1]] < #2[[1]] &];
(* find min x value and corresponding y value *)
minpair = s[[1]];
(* ditto for max x value *)
maxpair = s[[-1]];
(* return the pure function representing cutoff interpolation *)
Piecewise[{
{minpair[[2]] &, #1 < minpair[[1]] &},
{maxpair[[2]] &, #1 > maxpair[[1]] &},
{Interpolation[r], True}
}]]
test = Table[{x,Prime[x]},{x,1,10}]
InputForm[interpcut[test]]
Piecewise[{{minpair$59[[2]] & , #1 < minpair$59[[1]] & },
{maxpair$59[[2]] & , #1 > maxpair$59[[1]] & }},
InterpolatingFunction[{{1, 10}}, {3, 1, 0, {10}, {4}, 0, 0, 0, 0},
{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, {{2}, {3}, {5}, {7}, {11}, {13}, {17},
{19}, {23}, {29}}, {Automatic}]] 我肯定我漏掉了一些基本的东西。什么?
发布于 2015-01-13 08:07:01
让我向这个旧线程添加一个更新。由于使用了V9,因此可以使用本机(但仍处于实验阶段) "ExtrapolationHandler“参数
test = Table[{x, Prime[x]}, {x, 1, 10}];
g = Interpolation[test, "ExtrapolationHandler" ->
{If[# <= test[[1, 1]], test[[1, 2]], test[[-1, 2]]] &,
"WarningMessage" -> False}];
Plot[g[x], {x, -10, 30}]

发布于 2010-12-25 11:25:04
这是belisarius的答案的一个可能的替代方案:
interpcut[r_] := Module[{s}, s = SortBy[r, First];
Composition[Interpolation[r], Clip[#, Map[First, Through[{First, Last}[s]]]] &]]https://stackoverflow.com/questions/4476219
复制相似问题