Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
documentation:language_reference:objects:interpolatingfunction:functions:spline [2019/09/09 15:04] – Martin Brassdocumentation:language_reference:objects:interpolatingfunction:functions:spline [2026/11/17 07:31] (current) – Maurits W. Haverkort
Line 1: Line 1:
 {{indexmenu_n>999}} {{indexmenu_n>999}}
 ====== Spline ====== ====== Spline ======
 +;;# 
 +<color #ed1c24>Modified in 0.9.1</color>
 +;;#
  
 ### ###
-InterpolatingFunction.Spline(x,y) creates a natural C-spline : x table of knots, y table of function-values at corresponding knots+//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. <color #ed1c24>Modified in 0.9.1</color>
  
-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''^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 ==== ==== 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/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)))
 </code> </code>
  
 +<file Quanty_Output Example.out>
 +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)
 +</file>
  
 ===== Table of contents ===== ===== Table of contents =====
-{{indexmenu>.#1|msort}}+{{indexmenu>../#2|tsort}} 
 + 
 + 
  
Print/export