8
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – Michael E2
    Commented Aug 29, 2015 at 18:56
  • 2
    $\begingroup$ What if you use a monotonic interpolation method? $\endgroup$ Commented Aug 30, 2015 at 1:20

1 Answer 1

8
$\begingroup$

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.

$\endgroup$
8
  • $\begingroup$ 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? $\endgroup$
    – 407PZ
    Commented Aug 29, 2015 at 18:04
  • $\begingroup$ 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 $\endgroup$
    – user8074
    Commented Aug 29, 2015 at 18:09
  • $\begingroup$ I have put some real data in the thread @user8074 $\endgroup$
    – 407PZ
    Commented Aug 29, 2015 at 18:17
  • $\begingroup$ That's a great answer! Thx. $\endgroup$
    – 407PZ
    Commented Aug 29, 2015 at 19:17
  • 1
    $\begingroup$ 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. $\endgroup$
    – user8074
    Commented Aug 29, 2015 at 20:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.