Mathematica Interpolation[] 在超出范围时保持不变

发布于 2024-10-08 01:48:06 字数 1170 浏览 3 评论 0原文

我想“修改” Mathematica 的 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}]] 

我确信我错过了一些基本的东西。什么?

I want to "modify" Mathematica's Interpolation[] function (in 1
dimension) by replacing extrapolation with constant values when the
input is out of range.

In other words, if the interpolation domain is [1,20] and f[1]==7 and
f[20]==12, I want:

f[x] = 7 for x<=1 
f[x] = 12 for x>=20 
f[x] = Interpolation[...] 

However, this fails:

(* 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}]] 

I'm sure I'm missing something basic. What?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的呼吸 2024-10-15 01:48:06

函数定义

interpcut[r_, x_] := 
   Module[{s},(*sort array by x coord*)
       s = SortBy[r, First];
       Piecewise[
        {{First[s][[2]], x < First[s][[1]]},
         {Last [s][[2]], x > Last [s][[1]]},
         {Interpolation[r][x], True}}]]; 

测试

test = Table[{x, Prime[x]}, {x, 1, 10}];
f[x_] := interpcut[test, x]
Plot[f[x], {x, -10, 30}]  

alt text

编辑

回答您有关纯函数的评论。

我这样做只是为了清楚,而不是为了作弊。要使用纯函数,只需“遵循食谱”:

interpcut[r_] := Module[{s},
  s = SortBy[r, First];
  Function[Piecewise[
    {{First[s][[2]], # < First[s][[1]]},
     {Last [s][[2]], # > Last [s][[1]]},
     {Interpolation[r][#], True}}]]
  ] 

test = Table[{x, Prime[x]}, {x, 1, 10}];
f = interpcut[test] // InputForm
Plot[interpcut[test][x], {x, -10, 30}]

Function definition

interpcut[r_, x_] := 
   Module[{s},(*sort array by x coord*)
       s = SortBy[r, First];
       Piecewise[
        {{First[s][[2]], x < First[s][[1]]},
         {Last [s][[2]], x > Last [s][[1]]},
         {Interpolation[r][x], True}}]]; 

Test

test = Table[{x, Prime[x]}, {x, 1, 10}];
f[x_] := interpcut[test, x]
Plot[f[x], {x, -10, 30}]  

alt text

Edit

Answering your comment about pure functions.

I did it that way just for clarity, not for cheating. For using pure functions just "follow the recipe":

interpcut[r_] := Module[{s},
  s = SortBy[r, First];
  Function[Piecewise[
    {{First[s][[2]], # < First[s][[1]]},
     {Last [s][[2]], # > Last [s][[1]]},
     {Interpolation[r][#], True}}]]
  ] 

test = Table[{x, Prime[x]}, {x, 1, 10}];
f = interpcut[test] // InputForm
Plot[interpcut[test][x], {x, -10, 30}]
疯了 2024-10-15 01:48:06

让我对这个旧线程添加更新。从 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}]

在此处输入图像描述

Let me add an update to this old thread. Since V9 you can use native (but still experimental) "ExtrapolationHandler" parameter

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}]

enter image description here

拥抱我好吗 2024-10-15 01:48:06

这是贝利萨留答案的一个可能的替代方案:

interpcut[r_] := Module[{s}, s = SortBy[r, First];
    Composition[Interpolation[r], Clip[#, Map[First, Through[{First, Last}[s]]]] &]]

Here's a possible alternative to belisarius's answer:

interpcut[r_] := Module[{s}, s = SortBy[r, First];
    Composition[Interpolation[r], Clip[#, Map[First, Through[{First, Last}[s]]]] &]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文