8

I'm having some problems with Interpolation. I'm trying to interpolating some measurements data into mathematica. But using Interpolation will create some negative data, which is actually not possible in the real life.

For example, I'm trying to interpolate these data:

fct = Interpolation[{0, 0, 0, 0, 0, 0, 0, 0, 140.6833333, 24.58333333,
    3.5, 98.86666667, 0.8, 2.233333333, 0.983333333, 0.466666667, 
   5.983333333, 0.583333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    11.5, 7.333333333, 27.13333333, 263.7833333, 316.9166667, 429.2, 
   532.4666667, 567.4833333, 486.4333333, 61.98333333, 0, 0, 0, 0, 0, 
   0}]

Then I try to visualize them

Plot[fct[i], {i, 1, 48}]

Only to find that there is a little part of the function that lies unter the x axis.

enter image description here

Is there any way to avoid this? Thank you all in advance!

Michael E2
  • 235,386
  • 17
  • 334
  • 747
407PZ
  • 1,441
  • 8
  • 20

1 Answers1

8

Sometimes, a linear interpolation is sufficient and setting

InterpolationOrder->1

can avoid over/under-shoots in the interpolated function.

EDIT

You can also use an auxiliary function made to keep the values non-negative while keeping a sort of smoothness:

ClearAll[keepPositive];
Attributes[keepPositive] = {Listable};
keepPositive[x_] := Piecewise[{{Tanh[x - 1] + 1, x <= 1}, {x, True}}]

Now, you can do:

keepPositiveCoeff = 10;

fct = Interpolation[keepPositiveCoeff {0, 0, 0, 0, 0, 0, 0, 0,
140.6833333, 24.58333333,
3.5, 98.86666667, 0.8, 2.233333333, 0.983333333, 0.466666667, 
5.983333333, 0.583333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11.5, 7.333333333, 27.13333333, 263.7833333, 316.9166667, 429.2, 
532.4666667, 567.4833333, 486.4333333, 61.98333333, 0, 0, 0, 0, 0, 
0}, InterpolationOrder -> 2];

Plot[keepPositive[fct[i]]/keepPositiveCoeff, {i, 1, 48}]

and see that it never goes below zero.

For me it looks better with InterpolationOrder -> 2.

user8074
  • 1,592
  • 8
  • 7
  • Thank you for your answer. A linear Interpolation is OK with normal calculations. But these measurements should actually go into a differential equation. So actually, the second derivative of this interpolated function should always exist. If I use the linear interpolation, will the second derivative be equal to zero? – 407PZ Aug 29 '15 at 18:04
  • Yes, the second derivative will be zero with linear interpolation (and the first derivative is of course discontinuous. I will try to post another solution soon – user8074 Aug 29 '15 at 18:09
  • I have put some real data in the thread @user8074 – 407PZ Aug 29 '15 at 18:17
  • That's a great answer! Thx. – 407PZ Aug 29 '15 at 19:17
  • I find a way to avoid negative values by interpolating the square root of the values. When needed, then take a square of them and it looks also quite smooth to me. – 407PZ Aug 29 '15 at 19:19
  • 1
    The square root has the effect of sending values close to zero a little far away, closer to 1. Sometimes, interpolations create big undershoots (it might not be your case) and in that case the square root would not work. In any case, when it works, you found a very efficient solution. – user8074 Aug 29 '15 at 20:22
  • Hi. I've tried your method but it brought me another problem. This keepPositive Function made zeros into non-zero values. Can it be solved? Thx in advance. – 407PZ Sep 04 '15 at 13:55
  • @407Peezy Could you give me an example of these spurious zeros? – user8074 Sep 04 '15 at 15:42