我希望通过替换插值函数Interpolation[]的外推部分为常数值来“修改”它(在1维情况下)。换句话说,如果插值域是[1,20],且f[1]==7和f[20]==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}]]
我相信我错过了一些基础的东西。是什么呢?
Module[{s = SortBy[r, First];}, ...]
是可行的。至于With[]
,在这里也可以使用... - user414706