{{indexmenu_n>999}} ====== Spline ====== ;;# Modified in 0.9.1 ;;# ### //f = InterpolatingFunction.Spline(x, y)// or //f = InterpolatingFunction.Spline(x, y, yp1, ypN)// makes a cubic spline through the points $(x_i, y_i)$, an [[documentation:language_reference:objects:interpolatingfunction:start|interpolating function]] of type //"Spline"//. On every interval $[x_i, x_{i+1}]$ it is a cubic polynomial, and the function, its first and its second derivative are continuous. With two arguments the second derivative is 0 at both ends (a natural spline); with four, the slopes at the ends are //yp1// $= f'(x_1)$ and //ypN// $= f'(x_N)$. ### ===== 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. * //y// : a table of $N$ real numbers, the values at the points. * //yp1//, //ypN// : (optional) Reals, the first derivative at the first and at the last point (after sorting). ===== Output ===== * //f// : an interpolating function of type //"Spline"//. //f.x// and //f.y// are the sorted points and values, //f.knots// the points, //f.degree// is 3. Modified in 0.9.1 ### The natural spline is, of all functions through the points with a continuous second derivative, the one with the least curvature (the smallest integral of $f''^2$). Its error between the points decreases as $h^4$ with the distance $h$ between the points when $f''$ of the sampled function is 0 at the ends; otherwise the error near the ends decreases as $h^2$. Give the end slopes when they are known. Through two points the natural spline is a straight line. A spline reproduces straight lines exactly, and with the exact end slopes every cubic polynomial. ### ### Each data point influences the whole spline: near a jump in the data the spline overshoots and ripples, see the example on the [[documentation:language_reference:objects:interpolatingfunction:start|interpolating function]] page. Use //[[documentation:language_reference:objects:interpolatingfunction:functions:monotoniccspline|InterpolatingFunction.HermiteCSpline()]]// for data that must not overshoot. ### ===== Example ===== ### $\cos(x)$ sampled at 7 points on $[0, \pi]$. The natural spline sets $f''(0) = 0$, but $\cos''(0) = -1$; with the correct end slopes $f'(0) = f'(\pi) = 0$ the error is 70 times smaller. ### ==== Input ==== -- cos(x) sampled at 7 points on [0, pi], interpolated by cubic splines x, y = {}, {} for i = 0, 6 do x[i+1] = i*math.pi/6; y[i+1] = math.cos(x[i+1]) end natural = InterpolatingFunction.Spline(x, y) -- f'' = 0 at both ends clamped = InterpolatingFunction.Spline(x, y, 0, 0) -- f'(0) = 0, f'(pi) = 0 -- the largest error between the data points, of the function and of its derivative for _, c in ipairs({{"natural", natural}, {"clamped", clamped}}) do local f, e0, e1 = c[2], 0, 0 for i = 0, 600 do local t = i*math.pi/600 e0 = math.max(e0, math.abs(f(t) - math.cos(t))) e1 = math.max(e1, math.abs(f(t, 1) + math.sin(t))) end print(string.format("%s: max |f - cos| = %.1e, max |f' + sin| = %.1e, f''(0) = %6.3f", c[1], e0, e1, f(0, 2))) end print(string.format("integral from 0 to pi/2: %.6f (exact 1)", InterpolatingFunction.Integrate(clamped, 0, math.pi/2))) natural: max |f - cos| = 1.4e-02, max |f' + sin| = 1.5e-01, f''(0) = 0.000 clamped: max |f - cos| = 2.0e-04, max |f' + sin| = 1.2e-03, f''(0) = -1.023 integral from 0 to pi/2: 0.999885 (exact 1) ===== Table of contents ===== {{indexmenu>../#2|tsort}}