Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| documentation:language_reference:objects:interpolatingfunction:functions:monotoniccspline [2025/11/20 04:20] – external edit 127.0.0.1 | documentation:language_reference:objects:interpolatingfunction:functions:monotoniccspline [2026/11/16 23:21] (current) – Maurits W. Haverkort | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| {{indexmenu_n> | {{indexmenu_n> | ||
| ====== HermiteCSpline ====== | ====== HermiteCSpline ====== | ||
| + | ;;# | ||
| + | <color # | ||
| + | ;;# | ||
| ### | ### | ||
| - | InterpolatingFunction.HermiteCSpline(x, | + | //f = InterpolatingFunction.HermiteCSpline(x, |
| + | ### | ||
| + | |||
| + | ===== Input ===== | ||
| + | |||
| + | * //x// : a table of $N \geq 2$ different real numbers, the points. They need not be sorted; the points are sorted together with their values and slopes. | ||
| + | * //y// : a table of $N$ real numbers, the values at the points. | ||
| + | * //m// : (optional) a table of $N$ real numbers, the first derivative at the points. | ||
| + | |||
| + | ===== Output ===== | ||
| + | |||
| + | * //f// : an interpolating function of type //" | ||
| - | InterpolatingFunction.HermiteCSpline(x,y) creates a [[https://en.wikipedia.org/wiki/Monotone_cubic_interpolation|Monotone-Cubic-Spline]] where the slopes are chosen such that the returned function | + | ### |
| + | Without //m// the slopes are estimated: at a point inside, the average of the slopes $(y_{i+1} - y_i)/(x_{i+1} - x_i)$ of the two neighbouring intervals, at the ends the slope of the first and of the last interval. They are then limited as proposed by Fritsch and Carlson ([[https://doi.org/10.1137/0717021|SIAM J. Numer. Anal. 17, 238 (1980)]]): on an interval | ||
| ### | ### | ||
| + | ### | ||
| + | With //m// the slopes are used as given, without limiting. With the exact derivatives the Hermite spline reproduces every cubic polynomial, and its error decreases as $h^4$. | ||
| + | ### | ||
| + | |||
| + | ### | ||
| + | The curve on an interval only depends on the points nearby, so changing one data point changes the function only locally. The price is a second derivative that jumps at the points, and an error that decreases more slowly than that of a // | ||
| + | ### | ||
| + | |||
| + | ===== Example ===== | ||
| + | ;;# | ||
| + | <color # | ||
| + | ;;# | ||
| + | |||
| + | ### | ||
| + | An occupation that rises from 0 to 1. The cubic spline overshoots below 0 and above 1, the Hermite spline does not. With the exact slopes a cubic polynomial is reproduced. | ||
| + | ### | ||
| + | |||
| + | ==== Input ==== | ||
| + | <code Quanty Example.Quanty> | ||
| + | -- an occupation that rises from 0 to 1, sampled at 8 points | ||
| + | x = {0, 1, 2, 2.5, 3, 3.5, 4, 6} | ||
| + | y = {0, 0, 0.05, 0.4, 0.9, 1, 1, 1} | ||
| + | S = InterpolatingFunction.Spline(x, | ||
| + | H = InterpolatingFunction.HermiteCSpline(x, | ||
| + | for _, f in ipairs({S, H}) do | ||
| + | local min, max = 0, 1 | ||
| + | for i = 0, 600 do | ||
| + | local v = f(i/100) | ||
| + | min, max = math.min(min, | ||
| + | end | ||
| + | print(string.format(" | ||
| + | end | ||
| + | |||
| + | -- with the exact slopes a cubic Hermite spline reproduces every cubic polynomial | ||
| + | p = function(t) return 1 + t - 2*t^2 + 0.5*t^3 end | ||
| + | dp = function(t) return 1 - 4*t + 1.5*t^2 end | ||
| + | yp, mp = {}, {} | ||
| + | for i = 1, #x do yp[i] = p(x[i]); mp[i] = dp(x[i]) end | ||
| + | P = InterpolatingFunction.HermiteCSpline(x, | ||
| + | print(string.format(" | ||
| + | </ | ||
| + | |||
| + | <file Quanty_Output Example.out> | ||
| + | Spline | ||
| + | HermiteCSpline min | ||
| + | cubic with exact slopes: P(4.7) - p(4.7) = 1.8e-15 | ||
| + | </ | ||
| ===== Table of contents ===== | ===== Table of contents ===== | ||
| - | {{indexmenu> | + | {{indexmenu> |
| + | |||
| + | |||