# How to avoid creating negative values when using Interpolation?

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.

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

• You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful Aug 29 '15 at 18:56
• What if you use a monotonic interpolation method? Aug 30 '15 at 1:20

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.

• 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? 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 Aug 29 '15 at 18:09
• I have put some real data in the thread @user8074 Aug 29 '15 at 18:17
• That's a great answer! Thx. Aug 29 '15 at 19:17
• 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. Aug 29 '15 at 20:22