CUBIC_HERMITE_SPLINE
Overview
The CUBIC_HERMITE_SPLINE
function performs piecewise cubic interpolation of a 1-D function, matching both function values and first derivatives at each data point. This method is useful for smooth curve fitting in engineering, graphics, and scientific computing, where both the value and slope of the function are known at each sample point. The interpolation is continuous (smooth in both value and first derivative) and is based on the Hermite form of the cubic polynomial:
where , , , and are Hermite basis functions, , are function values, and , are derivatives at the endpoints of each interval. For more details, see the SciPy CubicHermiteSpline documentation and the Wikipedia article .
This function is a simplified wrapper for the SciPy CubicHermiteSpline
class, supporting only 1D arrays and not exposing advanced options like axis
or extrapolate
.
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=CUBIC_HERMITE_SPLINE(x, y, dydx, x_new)
x
(2D list, required): Table of one column, strictly increasing x-coordinates of data points.y
(2D list, required): Table of one column, function values at each x.dydx
(2D list, required): Table of one column, first derivatives at each x.x_new
(2D list, required): Table of one column, x-coordinates where interpolation is desired.
The function returns a 2D list (column vector) of interpolated values at each x_new
, or an error message (list of one string) if the input is invalid.
Examples
Example 1: Interpolate a Sine Curve
Inputs:
x | y | dydx | x_new |
---|---|---|---|
0 | 0.0 | 1.0 | 0.5 |
1 | 0.84147 | 0.5403 | 0.75 |
2 | 0.909297 | -0.4161 | 1.5 |
3 | 0.14112 | -0.98999 | 2.5 |
Excel formula:
=CUBIC_HERMITE_SPLINE({0;1;2;3}, {0;0.84147;0.909297;0.14112}, {1;0.5403;-0.4161;-0.98999}, {0.5;0.75;1.5;2.5})
Expected output:
Result |
---|
0.4782 |
0.6809 |
0.9949 |
0.5969 |
Example 2: Linear Data
Inputs:
x | y | dydx | x_new |
---|---|---|---|
0 | 0 | 1 | 0.5 |
1 | 1 | 1 | 1.5 |
2 | 2 | 1 | 2.0 |
Excel formula:
=CUBIC_HERMITE_SPLINE({0;1;2}, {0;1;2}, {1;1;1}, {0.5;1.5;2.0})
Expected output:
Result |
---|
0.5 |
1.5 |
2.0 |
Example 3: Quadratic Data
Inputs:
x | y | dydx | x_new |
---|---|---|---|
0 | 0 | 0 | 0.5 |
1 | 1 | 2 | 1.5 |
2 | 4 | 4 | 2.0 |
Excel formula:
=CUBIC_HERMITE_SPLINE({0;1;2}, {0;1;4}, {0;2;4}, {0.5;1.5;2.0})
Expected output:
Result |
---|
0.25 |
2.25 |
4.0 |
Example 4: Flat Data
Inputs:
x | y | dydx | x_new |
---|---|---|---|
0 | 2 | 0 | 0.5 |
1 | 2 | 0 | 1.0 |
2 | 2 | 0 | 1.5 |
Excel formula:
=CUBIC_HERMITE_SPLINE({0;1;2}, {2;2;2}, {0;0;0}, {0.5;1.0;1.5})
Expected output:
Result |
---|
2.0 |
2.0 |
2.0 |
Python Code
from scipy.interpolate import CubicHermiteSpline as scipy_cubic_hermite_spline
def cubic_hermite_spline(x, y, dydx, x_new):
"""
Piecewise cubic interpolation matching values and first derivatives at each point.
Args:
x: 2D list, strictly increasing x-coordinates of data points.
y: 2D list, function values at each x.
dydx: 2D list, first derivatives at each x.
x_new: 2D list, x-coordinates where interpolation is desired.
Returns:
2D list (column vector) of interpolated values at each x_new, or a list of one string error message if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate input shapes
try:
x_flat = [float(row[0]) for row in x]
y_flat = [float(row[0]) for row in y]
dydx_flat = [float(row[0]) for row in dydx]
x_new_flat = [float(row[0]) for row in x_new]
except Exception:
return [["Invalid input: all arguments must be 2D lists of numbers."]]
if not (len(x_flat) == len(y_flat) == len(dydx_flat)):
return [["Invalid input: x, y, dydx must have the same length."]]
if len(x_flat) < 2:
return [["Invalid input: at least two data points required."]]
if any(x2 <= x1 for x1, x2 in zip(x_flat, x_flat[1:])):
return [["Invalid input: x must be strictly increasing."]]
try:
spline = scipy_cubic_hermite_spline(x_flat, y_flat, dydx_flat)
y_new = spline(x_new_flat)
return [[round(float(val), 4)] for val in y_new]
except Exception as e:
return [[f"scipy.interpolate.CubicHermiteSpline error: {e}"]]
Live Notebook
Edit this function in a live notebook .