Prev: Rotate
Next: syntax extension
From: DrMajorBob on 12 Jan 2010 04:52 Here's a method, but it will have sharp corners for some data: data = Sort(a)RandomReal[{0, 1}, {10, 2}]; {min, max} = data[[{1, -1}, 1]]; Clear[f, g] g = Interpolation[data]; f[x_] := Max[0, g@x] Plot[f@x, {x, min, max}, PlotRange -> All] Bobby On Mon, 11 Jan 2010 17:52:33 -0600, dantimatter <google(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? > > Thanks, > Dan > -- DrMajorBob(a)yahoo.com
From: dantimatter on 13 Jan 2010 05:57 many thanks to all! dan
From: Noqsi on 13 Jan 2010 05:58 On Jan 11, 4:52 pm, 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? One way is to transform the data. Choose a positive real function f, apply its inverse to the data. Tranform the interpolation back using f. Exp[] often is a good choice for f. Using Valeri's example data: data = {{0, 2}, {1, 1}, {2, .2}, {3, .01}, {4, 1}, {5, 0.9}, {6, 0.05}, {7, 0.1}, {8, 1}} logdata = data /. {x_, y_} -> {x, Log[y]} interp[x_] = Exp[Interpolation[logdata][x]] If you plot interp[x], you'll see that it avoids zero.
From: dh on 13 Jan 2010 05:54 Sorry there is a type. Should read InterpolationOrder->1. Daniel dh wrote: > 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: DrMajorBob on 14 Jan 2010 05:46
For some data, that works pretty well; for other samples it has HUGE peaks, reaching far above any of the data: data = Sort(a)RandomReal[{0.1, 1}, {20, 2}]; {min, max} = data[[{1, -1}, 1]] f = Interpolation(a)data; logdata = data /. {x_, y_} -> {x, Log[y]}; interp = Exp[Interpolation[logdata]@#] &; Show[Plot[Evaluate(a)Through[{f, interp}@x], {x, min, max}, PlotRange -> All]] (run it several times) The same thing happens here, but not as dramatically: data = Sort(a)RandomReal[{0.1, 1}, {20, 2}]; {min, max} = data[[{1, -1}, 1]] f = Interpolation(a)data; logdata = data /. {x_, y_} -> {x, Sqrt[y]}; interp = (Interpolation[logdata]@#)^2 &; Show[Plot[Evaluate(a)Through[{f, interp}@x], {x, min, max}, PlotRange -> All]] Bobby On Wed, 13 Jan 2010 04:58:01 -0600, Noqsi <jpd(a)noqsi.com> wrote: > On Jan 11, 4:52 pm, 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? > > One way is to transform the data. Choose a positive real function f, > apply its inverse to the data. Tranform the interpolation back using > f. > > Exp[] often is a good choice for f. Using Valeri's example data: > > data = {{0, 2}, {1, 1}, {2, .2}, {3, .01}, {4, 1}, {5, 0.9}, {6, > 0.05}, {7, 0.1}, {8, 1}} > logdata = data /. {x_, y_} -> {x, Log[y]} > interp[x_] = Exp[Interpolation[logdata][x]] > > If you plot interp[x], you'll see that it avoids zero. > -- DrMajorBob(a)yahoo.com |