Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Interpolate data with a monotonic piecewise cubic Hermite interpolant
| ResourceFunction["CubicMonotonicInterpolation"][{f1,f2,…}] constructs a monotonic cubic Hermite interpolation of the function values fi, assumed to correspond to x values 1, 2, …. | |
| ResourceFunction["CubicMonotonicInterpolation"][{{x1,f1},{x2,f2},…}] constructs a monotonic cubic Hermite interpolation of the function values fi corresponding to x values xi. | 
Construct an approximate function that interpolates the data:
| In[1]:= | ![f = ResourceFunction[
  "CubicMonotonicInterpolation"][{0., 0.6, 1., 1.3, 1.4, 2.1}]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/501bc02d19a25616.png) | 
| Out[1]= |  | 
Apply the function to find interpolated values:
| In[2]:= | ![f[3.7]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/708d089adc289d52.png) | 
| Out[2]= |  | 
Plot the interpolation function along with the original data:
| In[3]:= | ![Show[Plot[f[x], {x, 1, 6}], ListPlot[{0., 0.6, 1., 1.3, 1.4, 2.1}]]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/16ee9a12c0ad5fa0.png) | 
| Out[3]= |  | 
Interpolate and plot a monotonic data set:
| In[4]:= | ![data = {{0., 0.}, {0.7, 0.02}, {2.1, 0.06}, {3., 20.}, {3.8, 20.}, {5.1, 62.2}, {5.9, 62.24}, {7., 62.26}};
fun = ResourceFunction["CubicMonotonicInterpolation"][data]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/0bc75ae0dabcc292.png) | 
| Out[4]= |  | 
| In[5]:= | ![Plot[fun[x], {x, 0, 7}, Epilog -> {Directive[AbsolutePointSize[5], ColorData[97, 4]], Point[data]}]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/34cc91640d353af7.png) | 
| Out[5]= |  | 
Form an interpolation from data given as a TimeSeries:
| In[6]:= | ![ts = TemporalData[
  TimeSeries, {{{1, Cos[1] + Sin[1], Cos[2] + Sin[2], Cos[3] + Sin[3],
       Cos[4] + Sin[4], Cos[5] + Sin[5], Cos[6] + Sin[6], Cos[7] + Sin[7], Cos[8] + Sin[8], Cos[9] + Sin[9], Cos[10] + Sin[10]}}, {{{0.016965744360214943`, 0.9973865299845299, 1.24591736467102, 1.8114088692820352`, 1.9107450982712126`, 3.013995043949912, 3.323754146542503, 4.551557869716615, 5.13709169763333, 5.334143602077121, 5.441221219515277}}}, 1, {"Continuous", 1}, {"Discrete", 1}, 1, {ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}},
    False, 10.1];](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/7e8f4577cc91b5d5.png) | 
| In[7]:= | ![ListPlot[ts, Filling -> Axis]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/55763869bfb7f0b5.png) | 
| Out[7]= |  | 
| In[8]:= | ![fun = ResourceFunction["CubicMonotonicInterpolation"][ts]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/49ee90b3a83558df.png) | 
| Out[8]= |  | 
| In[9]:= | ![Plot[fun[x], {x, ts["FirstTime"], ts["LastTime"]}]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/49ac403fabda7ca3.png) | 
| Out[9]= |  | 
Compare different monotonic interpolation methods:
| In[10]:= |  | 
| In[11]:= | ![fSte = ResourceFunction["CubicMonotonicInterpolation"][data, Method -> "Steffen"];
fFC = ResourceFunction["CubicMonotonicInterpolation"][data, Method -> "FritschCarlson"];
fSti = ResourceFunction["CubicMonotonicInterpolation"][data, Method -> "Stineman"];](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/7559a0d2ec02050d.png) | 
| In[12]:= | ![Plot[{fSte[x], fFC[x], fSti[x]}, {x, 0, 9}, PlotLegends -> {"Steffen", "Fritsch-Carlson", "Stineman"}]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/3bf584f8e70fd738.png) | 
| Out[12]= |  | 
With PeriodicInterpolation->True, the data is interpreted as one period of a periodic function:
| In[13]:= | ![fp = ResourceFunction[
  "CubicMonotonicInterpolation"][{1, 5, 7, 2, 3, 1}, PeriodicInterpolation -> True]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/3363969ff6fe631d.png) | 
| Out[13]= |  | 
| In[14]:= | ![Plot[fp[x], {x, -12, 18}, {Axes -> None, Frame -> True}]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/4d2d49578947f7b9.png) | 
| Out[14]= |  | 
A monotonic data set:
| In[15]:= |  | 
| In[16]:= | ![fHerm = Interpolation[data, Method -> "Hermite"];
fSpl = Interpolation[data, Method -> "Spline"];
fMon = ResourceFunction["CubicMonotonicInterpolation"][data];](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/2142e90678a81cbd.png) | 
Compare CubicMonotonicInterpolation with the built-in "Hermite" and "Spline" methods of Interpolation. The built-in methods do not preserve monotonicity, and give spurious oscillations:
| In[17]:= | ![Plot[{fMon[x], fHerm[x], fSpl[x]}, {x, 0, 1}, Epilog -> {Directive[AbsolutePointSize[5], ColorData[97, 4]], Point[data]}, PlotLegends -> {"Monotonic", "Hermite", "Spline"}, PlotRange -> All]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/1fe32329ab6f81e4.png) | 
| Out[17]= |  | 
The interpolant returned by CubicMonotonicInterpolation is continuous up to its first derivative:
| In[18]:= | ![data = {{1., 1.}, {2., 1.8}, {3., 5.}, {3.006, 5.0001}, {5., 7.}, {6.,
     10.}, {7., 10.}};
fun = ResourceFunction["CubicMonotonicInterpolation"][data];](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/1390abe6d1ce4503.png) | 
| In[19]:= | ![Plot[{fun[x], fun'[x], fun''[x]}, {x, 1, 7}, PlotLegends -> {"f", "f'", "f''"}, PlotRange -> All]](https://www.wolframcloud.com/obj/resourcesystem/images/157/15779973-0285-4c3b-8cbd-d925e523396a/07902f58355ac34b.png) | 
| Out[19]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License