Prev: Rotate
Next: syntax extension
From: dantimatter on 11 Jan 2010 18:52 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? Thanks, Dan
From: Mark McClure on 12 Jan 2010 04:50 On Mon, Jan 11, 2010 at 6:52 PM, dantimatter <google(a)dantimatter.com> wrote: > 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? Here's a (too?) simple approach: data = {{0, 2}, {1, 0.2}, {2, 0.2}, {3, 2}}; f = Interpolation[data]; g[x_] := Max[{f[x], 0}]; Plot[{f[x], g[x]}, {x, 1, 2}, PlotStyle -> {Thin, Thick}] You'll lose some smoothness but you'd certainly lose smoothness by decreasing the InterpolationOrder as well. Mark McClure
From: dh on 12 Jan 2010 04:51 Hi, Interpolation by default fits cubic polynomials. These tend to over/undershoot the points given. If you can live with linear interpolation, you may specify InterpolationOrder->0. Then the points are simply connected by straight lines. Daniel dantimatter 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? > > Thanks, > Dan >
From: Valeri Astanoff on 12 Jan 2010 04:51 On 12 jan, 00:52, dantimatter <goo...(a)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
From: Bill Rowe on 12 Jan 2010 04:52
On 1/11/10 at 6:52 PM, google(a)dantimatter.com (dantimatter) wrote: >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. By default, Interpolation uses piecewise cubic polynomials to fit your data. The coefficients of these polynomials are found by selecting a sufficient number of consecutive (4 for cubics) and solving for the needed coefficients. The dips below zero are a consequence of using a 3 order polynomial and the specific values of the data you are interpolating. >Is there a way to force positivity? Or should I simply adjust the >InterpolationOrder until it works? You really don't have much option here. If you reduce the interpolating order to 1, and all of the data is positive you are guaranteed all values of the resulting interpolation will be positive. But this also means derivatives will not be continuous which may or may not be a problem for you. Lack of continuity in the first and second derivatives means a plot of the interpolating function will not be smooth. No other interpolation order is certain to only provide positive values given positive data. Higher order interpolating polynomials are more likely to dip below zero. If you want continuous first and second derivatives (a smooth interpolation) and only positive values, you either need to use some other basis for interpolating your data than polynomials such as a set of exponential terms or give up the requirement the interpolating function pass through your data. Using exponentials creates other problems. It transforms the problem into one that will not have explicit algebraic solutions for the coefficients. That means you will need iterative methods which will only converge well if you provide sufficiently close initial values as a starting point. Also, these methods tend not to be very stable numerically when more than a few exponential terms are required. You can achieve smooth interpolating functions with cubic polynomials using a smoothed natural cubic spline. If you are interested in this approach, I've written a package for my use which has the needed functions that I would be willing to send you offline. Note, while this will yield a smooth function (continuous first and second derivatives) it will not pass through your data points when you require all positive values for the range of your data. |