首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当超出范围时保持不变的数学Interpolation[]

当超出范围时保持不变的数学Interpolation[]
EN

Stack Overflow用户
提问于 2010-12-18 10:07:07
回答 2查看 1.9K关注 0票数 6

我想要“修改”Mathematica的Interpolation[]函数(在一维中),当输入超出范围时,用常量值替换外推。

换句话说,如果插值域是1,20,f1==7和f20==12,我想:

代码语言:javascript
复制
f[x] = 7 for x<=1 
f[x] = 12 for x>=20 
f[x] = Interpolation[...] 

但是,这会失败:

代码语言:javascript
复制
(* 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}]] 

我肯定我漏掉了一些基本的东西。什么?

EN

回答 2

Stack Overflow用户

发布于 2015-01-13 08:07:01

让我向这个旧线程添加一个更新。由于使用了V9,因此可以使用本机(但仍处于实验阶段) "ExtrapolationHandler“参数

代码语言:javascript
复制
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}]

票数 2
EN

Stack Overflow用户

发布于 2010-12-25 11:25:04

这是belisarius的答案的一个可能的替代方案:

代码语言:javascript
复制
interpcut[r_] := Module[{s}, s = SortBy[r, First];
    Composition[Interpolation[r], Clip[#, Map[First, Through[{First, Last}[s]]]] &]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4476219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档