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:spline [2019/09/09 15:04] – Martin Brass | documentation:language_reference:objects:interpolatingfunction:functions:spline [2026/11/17 07:31] (current) – Maurits W. Haverkort | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| {{indexmenu_n> | {{indexmenu_n> | ||
| ====== Spline ====== | ====== Spline ====== | ||
| + | ;;# | ||
| + | <color # | ||
| + | ;;# | ||
| ### | ### | ||
| - | InterpolatingFunction.Spline(x, | + | //f = InterpolatingFunction.Spline(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. | ||
| + | * //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 //" | ||
| - | InterpolatingFunction.Spline(x,y,yp1,ypn) creates a C-spline where the derivatives at the outermost knots are determined by yp1 and ypn (both real values) | + | ### |
| + | 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'' | ||
| + | ### | ||
| + | |||
| + | ### | ||
| + | Each data point influences the whole spline: near a jump in the data the spline overshoots and ripples, see the example on the [[documentation: | ||
| + | ### | ||
| + | |||
| + | ===== Example ===== | ||
| + | |||
| + | ### | ||
| + | $\cos(x)$ sampled at 7 points on $[0, \pi]$. The natural spline sets $f'' | ||
| ### | ### | ||
| ==== Input ==== | ==== Input ==== | ||
| <code Quanty Example.Quanty> | <code Quanty Example.Quanty> | ||
| - | A = InterpolatingFunction.Spline({1,2,3},{1,4,9}) | + | -- cos(x) sampled at 7 points on [0, pi], interpolated by cubic splines |
| - | B = InterpolatingFunction.Spline({1,2,3},{1,4,9},0,0) -- derivatives vanish at the endpoint | + | x, y = {}, {} |
| + | for i = 0, 6 do x[i+1] = i*math.pi/ | ||
| + | natural = InterpolatingFunction.Spline(x, y) -- f'' | ||
| + | clamped | ||
| + | |||
| + | -- the largest error between the data points, of the function and of its derivative | ||
| + | for _, c in ipairs({{" | ||
| + | local f, e0, e1 = c[2], 0, 0 | ||
| + | for i = 0, 600 do | ||
| + | local t = i*math.pi/ | ||
| + | e0 = math.max(e0, | ||
| + | e1 = math.max(e1, | ||
| + | end | ||
| + | print(string.format(" | ||
| + | c[1], e0, e1, f(0, 2))) | ||
| + | end | ||
| + | print(string.format(" | ||
| </ | </ | ||
| + | <file Quanty_Output Example.out> | ||
| + | natural: max |f - cos| = 1.4e-02, max |f' + sin| = 1.5e-01, f'' | ||
| + | clamped: max |f - cos| = 2.0e-04, max |f' + sin| = 1.2e-03, f'' | ||
| + | integral from 0 to pi/2: 0.999885 (exact 1) | ||
| + | </ | ||
| ===== Table of contents ===== | ===== Table of contents ===== | ||
| - | {{indexmenu> | + | {{indexmenu> |
| + | |||
| + | |||