Re: restricting interpolating functions to be positive

*To*: mathgroup at smc.vnet.net*Subject*: [mg106430] Re: restricting interpolating functions to be positive*From*: Valeri Astanoff <astanoff at gmail.com>*Date*: Tue, 12 Jan 2010 04:49:50 -0500 (EST)*References*: <higdjs$kfi$1@smc.vnet.net>

On 12 jan, 00:52, dantimatter <goo... at dantimatter.com> wrote: > Hi All, > > I would like to construct an interpolation that can only take on > positive values. The data that I'm trying to interpolate is all > positive, but for some reason the Interpolation[] function 'wants' to > connect the dots by dipping below zero. Is there a way to force > positivity? Or should I simply adjust the InterpolationOrder until it > works? Good day, Here is a DIY way to do it (just a toy example) : In[1]:= data = {{0, 2}, {1, 1}, {2, .2}, {3, .01}, {4, 1}, {5, 0.9}, {6, 0.05}, {7, 0.1}, {8, 1}}; xm = data[[-1, 1]]; [ we're gonna get negative interpolation with data close to zero ] In[3]:= f1 = Interpolation[data, InterpolationOrder -> 1]; f2 = Interpolation[data]; [ f1 is used just to locate the minimums, an additional test should be used below to only select minimums close to zero ] In[5]:= dx = 0.1; [ dx is to be adjusted accordingly ] In[6]:= data2 = Table[{{x}, f2[x], If[ f1'[x - dx] < 0 && f1'[x + dx] > 0 , 0, f2'[x]]}, {x, 0, xm}] During evaluation of In[6]:= InterpolatingFunction::dmval: Input \ value {-0.1} lies outside the range of data in the interpolating \ function. Extrapolation will be used. >> Out[6]= {{{0}, 2., -0.963333}, {{1}, 1., -0.968333}, {{2}, 0.2, -0.563333}, {{3}, 0.01, 0}, {{4}, 1., 0.823333}, {{5}, 0.9, -0.531667}, {{6}, 0.05, 0}, {{7}, 0.1, 0.483333}, {{8}, 1., 1.30833}} [ Notice derivatives at 3 and 6 set to zero ] In[7]:= f3 = Interpolation[data2]; In[8]:= Plot[f1[x], {x, 0, xm}] [...] In[9]:= Plot[f2[x], {x, 0, xm}] [...] In[10]:= Plot[f3[x], {x, 0, xm}] [...] Seems okay ( sure not for all cases ! ) -- Valeri Astanoff