In the mathematical field of numerical analysis, spline interpolation is a form of interpolation where the interpolant is a special type of piecewise polynomial called a spline. That is, instead of fitting a single, high-degree polynomial to all of the values at once, spline interpolation fits low-degree polynomials to small subsets of the values, for example, fitting nine cubic polynomials between each of the pairs of ten points, instead of fitting a single degree-nine polynomial to all of them. Spline interpolation is often preferred over polynomial interpolation because the interpolation error can be made small even when using low-degree polynomials for the spline. Spline interpolation also avoids the problem of Runge's phenomenon, in which oscillation can occur between points when interpolating using high-degree polynomials.
 Originally, spline was a term for elastic rulers that were bent to pass through a number of predefined points, or knots. These were used to make technical drawings for shipbuilding and construction by hand, as illustrated in the figure.
 We wish to model similar kinds of curves using a set of mathematical equations. Assume we have a sequence of 
  
    
      
        n
        +
        1
      
    
    {\displaystyle n+1}
  
 knots, 
  
    
      
        (
        
          x
          
            0
          
        
        ,
        
          y
          
            0
          
        
        )
      
    
    {\displaystyle (x_{0},y_{0})}
  
 through 
  
    
      
        (
        
          x
          
            n
          
        
        ,
        
          y
          
            n
          
        
        )
      
    
    {\displaystyle (x_{n},y_{n})}
  
. There will be a cubic polynomial 
  
    
      
        
          q
          
            i
          
        
        (
        x
        )
        =
        y
      
    
    {\displaystyle q_{i}(x)=y}
  
 between each successive pair of knots 
  
    
      
        (
        
          x
          
            i
            −
            1
          
        
        ,
        
          y
          
            i
            −
            1
          
        
        )
      
    
    {\displaystyle (x_{i-1},y_{i-1})}
  
 and 
  
    
      
        (
        
          x
          
            i
          
        
        ,
        
          y
          
            i
          
        
        )
      
    
    {\displaystyle (x_{i},y_{i})}
  
 connecting to both of them, where 
  
    
      
        i
        =
        1
        ,
        2
        ,
        …
        ,
        n
      
    
    {\displaystyle i=1,2,\dots ,n}
  
. So there will be 
  
    
      
        n
      
    
    {\displaystyle n}
  
 polynomials, with the first polynomial starting at 
  
    
      
        (
        
          x
          
            0
          
        
        ,
        
          y
          
            0
          
        
        )
      
    
    {\displaystyle (x_{0},y_{0})}
  
, and the last polynomial ending at 
  
    
      
        (
        
          x
          
            n
          
        
        ,
        
          y
          
            n
          
        
        )
      
    
    {\displaystyle (x_{n},y_{n})}
  
.
 The curvature of any curve 
  
    
      
        y
        =
        y
        (
        x
        )
      
    
    {\displaystyle y=y(x)}
  
 is defined as
 where 
  
    
      
        
          y
          ′
        
      
    
    {\displaystyle y'}
  
 and 
  
    
      
        
          y
          ″
        
      
    
    {\displaystyle y''}
  
 are the first and second derivatives of 
  
    
      
        y
        (
        x
        )
      
    
    {\displaystyle y(x)}
  
 with respect to 
  
    
      
        x
      
    
    {\displaystyle x}
  
.
To make the spline take a shape that minimizes the bending (under the constraint of passing through all knots), we will define both 
  
    
      
        
          y
          ′
        
      
    
    {\displaystyle y'}
  
 and 
  
    
      
        
          y
          ″
        
      
    
    {\displaystyle y''}
  
 to be continuous everywhere, including at the knots. Each successive polynomial must have equal values (which are equal to the y-value of the corresponding datapoint), derivatives, and second derivatives at their joining knots, which is to say that
 This can only be achieved if polynomials of degree 3 (cubic polynomials) or higher are used. The classical approach is to use polynomials of exactly degree 3 — cubic splines.
 In addition to the three conditions above, a 'natural cubic spline' has the condition that 
  
    
      
        
          q
          
            1
          
          ″
        
        (
        
          x
          
            0
          
        
        )
        =
        
          q
          
            n
          
          ″
        
        (
        
          x
          
            n
          
        
        )
        =
        0
      
    
    {\displaystyle q''_{1}(x_{0})=q''_{n}(x_{n})=0}
  
.
 In addition to the three main conditions above, a 'clamped cubic spline' has the conditions that 
  
    
      
        
          q
          
            1
          
          ′
        
        (
        
          x
          
            0
          
        
        )
        =
        
          f
          ′
        
        (
        
          x
          
            0
          
        
        )
      
    
    {\displaystyle q'_{1}(x_{0})=f'(x_{0})}
  
 and 
  
    
      
        
          q
          
            n
          
          ′
        
        (
        
          x
          
            n
          
        
        )
        =
        
          f
          ′
        
        (
        
          x
          
            n
          
        
        )
      
    
    {\displaystyle q'_{n}(x_{n})=f'(x_{n})}
  
 where 
  
    
      
        
          f
          ′
        
        (
        x
        )
      
    
    {\displaystyle f'(x)}
  
 is the derivative of the interpolated function.
 In addition to the three main conditions above, a 'not-a-knot spline' has the conditions that 
  
    
      
        
          q
          
            1
          
          ‴
        
        (
        
          x
          
            1
          
        
        )
        =
        
          q
          
            2
          
          ‴
        
        (
        
          x
          
            1
          
        
        )
      
    
    {\displaystyle q'''_{1}(x_{1})=q'''_{2}(x_{1})}
  
 and 
  
    
      
        
          q
          
            n
            −
            1
          
          ‴
        
        (
        
          x
          
            n
            −
            1
          
        
        )
        =
        
          q
          
            n
          
          ‴
        
        (
        
          x
          
            n
            −
            1
          
        
        )
      
    
    {\displaystyle q'''_{n-1}(x_{n-1})=q'''_{n}(x_{n-1})}
  
.
 We wish to find each polynomial 
  
    
      
        
          q
          
            i
          
        
        (
        x
        )
      
    
    {\displaystyle q_{i}(x)}
  
 given the points 
  
    
      
        (
        
          x
          
            0
          
        
        ,
        
          y
          
            0
          
        
        )
      
    
    {\displaystyle (x_{0},y_{0})}
  
 through 
  
    
      
        (
        
          x
          
            n
          
        
        ,
        
          y
          
            n
          
        
        )
      
    
    {\displaystyle (x_{n},y_{n})}
  
. To do this, we will consider just a single piece of the curve, 
  
    
      
        q
        (
        x
        )
      
    
    {\displaystyle q(x)}
  
, which will interpolate from 
  
    
      
        (
        
          x
          
            1
          
        
        ,
        
          y
          
            1
          
        
        )
      
    
    {\displaystyle (x_{1},y_{1})}
  
 to 
  
    
      
        (
        
          x
          
            2
          
        
        ,
        
          y
          
            2
          
        
        )
      
    
    {\displaystyle (x_{2},y_{2})}
  
. This piece will have slopes 
  
    
      
        
          k
          
            1
          
        
      
    
    {\displaystyle k_{1}}
  
 and 
  
    
      
        
          k
          
            2
          
        
      
    
    {\displaystyle k_{2}}
  
 at its endpoints. Or, more precisely,
 The full equation 
  
    
      
        q
        (
        x
        )
      
    
    {\displaystyle q(x)}
  
 can be written in the symmetrical form
         (1) where
         (2)         (3)         (4) But what are 
  
    
      
        
          k
          
            1
          
        
      
    
    {\displaystyle k_{1}}
  
 and 
  
    
      
        
          k
          
            2
          
        
      
    
    {\displaystyle k_{2}}
  
? To derive these critical values, we must consider that
 It then follows that
         (5)         (6) Setting t = 0 and t = 1 respectively in equations (5) and (6), one gets from (2) that indeed first derivatives q′(x1) = k1 and q′(x2) = k2, and also second derivatives
         (7)         (8) If now (xi, yi), i = 0, 1, ..., n are n + 1 points, and
         (9) where i = 1, 2, ..., n, and 
  
    
      
        t
        =
        
          
            
              
                x
                −
                
                  x
                  
                    i
                    −
                    1
                  
                
              
              
                
                  x
                  
                    i
                  
                
                −
                
                  x
                  
                    i
                    −
                    1
                  
                
              
            
          
        
      
    
    {\displaystyle t={\tfrac {x-x_{i-1}}{x_{i}-x_{i-1}}}}
  
 are n third-degree polynomials interpolating y in the interval xi−1 ≤ x ≤ xi for i = 1, ..., n such that q′i (xi) = q′i+1(xi) for i = 1, ..., n − 1, then the n polynomials together define a differentiable function in the interval x0 ≤ x ≤ xn, and
         (10)         (11) for i = 1, ..., n, where
         (12)         (13)         (14) If the sequence k0, k1, ..., kn is such that, in addition, q′′i(xi) = q′′i+1(xi) holds for i = 1, ..., n − 1, then the resulting function will even have a continuous second derivative.
 From (7), (8), (10) and (11) follows that this is the case if and only if
         (15) for i = 1, ..., n − 1. The relations (15) are n − 1 linear equations for the n + 1 values k0, k1, ..., kn.
 For the elastic rulers being the model for the spline interpolation, one has that to the left of the left-most "knot" and to the right of the right-most "knot" the ruler can move freely and will therefore take the form of a straight line with q′′ = 0. As q′′ should be a continuous function of x, "natural splines" in addition to the n − 1 linear equations (15) should have
 i.e. that
         (16)         (17) Eventually, (15) together with (16) and (17) constitute n + 1 linear equations that uniquely define the n + 1 parameters k0, k1, ..., kn.
 There exist other end conditions, "clamped spline", which specifies the slope at the ends of the spline, and the popular "not-a-knot spline", which requires that the third derivative is also continuous at the x1 and xn−1 points.
For the "not-a-knot" spline, the additional equations will read:
 where 
  
    
      
        Δ
        
          x
          
            i
          
        
        =
        
          x
          
            i
          
        
        −
        
          x
          
            i
            −
            1
          
        
        ,
         
        Δ
        
          y
          
            i
          
        
        =
        
          y
          
            i
          
        
        −
        
          y
          
            i
            −
            1
          
        
      
    
    {\displaystyle \Delta x_{i}=x_{i}-x_{i-1},\ \Delta y_{i}=y_{i}-y_{i-1}}
  
.
 In case of three points the values for 
  
    
      
        
          k
          
            0
          
        
        ,
        
          k
          
            1
          
        
        ,
        
          k
          
            2
          
        
      
    
    {\displaystyle k_{0},k_{1},k_{2}}
  
 are found by solving the tridiagonal linear equation system
 with
 For the three points
 one gets that
 and from (10) and (11) that
 In the figure, the spline function consisting of the two cubic polynomials 
  
    
      
        
          q
          
            1
          
        
        (
        x
        )
      
    
    {\displaystyle q_{1}(x)}
  
 and 
  
    
      
        
          q
          
            2
          
        
        (
        x
        )
      
    
    {\displaystyle q_{2}(x)}
  
 given by (9) is displayed.
 TinySpline: Open source C-library for splines which implements cubic spline interpolation
 SciPy Spline Interpolation: a Python package that implements interpolation
 Cubic Interpolation: Open source C#-library for cubic spline interpolation