Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Constraint Cubic spline

  • Last Updated : 24 Oct, 2021

Cubic Spline Interpolation

Cubic spline interpolation is a way of finding a curve that connects data points with a degree of three or less. Splines are polynomial that are smooth and continuous across a given plot and also continuous first and second derivatives where they join.

We take a set of points [xi, yi] for i = 0, 1, …, n for the function y = f(x). The cubic spline interpolation is a piecewise continuous curve, passing through each of the values in the table. 

Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the Machine Learning Foundation Course at a student-friendly price and become industry ready.



  • Following are the conditions for the spline of degree K=3:
    • The domain of s is in intervals of [a, b].
    • S, S’, S” are all continuous function on [a,b].

S(x) =\begin{bmatrix} S_0 (x), x \epsilon [x_0, x_1] \\ S_1 (x), x \epsilon [x_1, x_2] \\ ... \\ ... \\ ... \\ S_{n-1} (x), x \epsilon [x_1, x_2] \end{bmatrix}



Here Si(x) is the cubic polynomial that will be used on the subinterval [xi, xi+1].

The main factor about spline is that it combines different polynomials and does not use a single polynomial of degree n to fit all the points at once, it avoids high degree polynomials and thereby the potential problem of overfitting. These low-degree polynomials need to be such that the spline they form is not only continuous but also smooth.

But for the spline to be smooth and continuous, the two consecutive polynomials and  Si (x) and Si+1 (x) must join at xi

S_i (x_i) = S_{i+1} (x_i)= f(x_i) = y_i

Or, Si (x) must be passed through two end-points:

S_{i-1} (x_{i-1}) = S_{i} (x_i)= f(x_{i-1}) ; S_i (x_i) = S_{i+1} (x_i)= f(x_i) = y_i

Assume, S” (x) = Mi (i= 0,1,2, … , n). Since S(x) is cubic polynomial , so S” (x) is the linear polynomial in  [xi, xi+1], then S”’ (x) will be:

S''' (x) = \frac{M_{i+1} - M_i }{x_{i+1} - x_i} \, \forall x \epsilon [x_i, x_{i+1}]



By applying the Taylor series:

S(x) = S(x_i) + S'(x_i)(x-x_{i}) + \frac{S'' (x)}{2!}(x-x_i)^{2} + \frac{M_{i+1}-M_i}{3! (x_{i+1} - x_i)} (x -x_i)^{3}

Let, x = xi+1:

y_{i+1} = y_i + S'(x_i)(x_{i+1} - x_i) + \frac{M_i}{2!}(x_{i+1} - x_i)^{2} + \frac{M_{i+1} - M_i}{3!} (x_{i+1} - x_i)^{2} \\ \\ \\ S'(x_i) = \frac{y_{i+1} - y_i}{x_{i+1} -x_i} - \frac{1}{6}(M_{i+1} + 2M_i)(x_{i+1}- x_i)                 

Similarly, we apply above equation b/w range [xi-1, xi]:

S'(x_i) = \frac{y_{i} - y_{i-1}}{x_{i} -x_{i-1}} - \frac{1}{6}(M_{i} + 2M_{i-1})(x_{i}- x_{i-1})

Let hi =xi – xi-1

\mu_i = \frac{h_i }{h_{i+1} + h_i} \\ \\ \lambda_i = 1-\mu_i =  \frac{h_{i+1} }{h_{i+1} + h_i}

Now, we have n-1 equations, but have n+1 variables i.e M0, M1, M2,…Mn-1, Mn. Therefore, we need to get 2 more equations. For that, we will be using additional boundary conditions.

Let’s consider that we know S’ (x0) = f0‘ and S’ (xn) = fn‘, especially if S’ (x0) and S’ (xn) both are 0. This is called the clamped boundary condition.



S_{1}'(x_0) = \frac{y_{1} - y_{0}}{h_i} - \frac{1}{6}(M_{1} + 2M_{0})(h_1)

f_{0}^{'} = - M_0 \frac{h}{2} + f[x_0, x_1] - \frac{M_1 - M_0}{6}h_1

2M_0 + M_1 = \frac{6}{h_1} (f[x_0, x_1] - f_{0}')

Similarly, for Mn

2M_{n} + M_{n-1} = \frac{6}{h_n} (f_{n}' -f[x_{n-1}, x_n]  )

or

2M_{n} + M_{n-1} = {6}f[x_0, x_0, x_1] \\ d_n = f[x_{n-1}, x_n, x_n]

Combining the above equation in to the matrix form, we get the following matrix:

\begin{bmatrix} 2&  \lambda_0 &  &  &  &  &  &  &  &  &  &  & \\ \mu_1&  2&  \lambda_1&  &  &  &  &  &  &  &  &  & \\ &  .&  .&  .&  &  &  &  &  &  &  &  & \\ &  &  .&  .&  .&  &  &  &  &  &  &  & \\ &  &  &  .&  .&  .&  &  &  &  &  &  & \\ &  &  &  &  .&  .&  .&  &  &  &  &  & \\ &  &  &  &  &  .&  .&  .&  &  &  &  & \\ &  &  &  &  &  &  .&  .&  .&  &  &  & \\ &  &  &  &  &  &  & & \mu_{n-1}&  2&  \lambda_{n-1}&  &   \\ &  &  &  &  &  &  &  &  &  &  \mu_n&  2& \\ \end{bmatrix} \begin{bmatrix} M_0 \\ M_1\\ .\\ .\\ .\\ .\\ .\\ .\\ M_{n-1}\\ M_n \end{bmatrix} = \begin{bmatrix} d_0 \\ d_1 \\ .\\ .\\ .\\ .\\ .\\ .\\ d_{n-1}\\ d_n \end{bmatrix}

Constraint Cubic Spline

Constraint cubic spline was proposed by the  CJC Kruger in his article. The algorithm is an extension of cubic spline interpolation. The important step in it is the calculation of the slope at each point. The idea behind the slope calculation is that the slope at a point will be b/w the slope of the two adjacent lines joining that point, if one of them is 0 then the slope at point should also be 0.



 Let take a collection of point (x_0, y_0), (x_1, x_2) …\, …\, … \, (x_{i-1}, y_{i-1}),(x_{i}, y_{i}), (x_{i+1}, y_{i+1})…\, …\, … \, (x_n, y_n). The cubic curve can be given by:

f_i(x) = a_i  + b_i x + c_i x^{2} + d_i x^{3}

The above curve pass through all of the following points:

f_i (x) = f_{i+1}(x) = y_i

THe first order derivative must be continuous at intermediate points:

f_i^{'}(x_i) = f_{i+1}^{'}(x_i) = f^{'}(x)

which can be calculated by following formula for intermediate points:

f^{'}(x_i) = \frac{2}{\frac{x_{i+1} - x_i}{y_{i+1} - y_i} + \frac{x_{i} - x_{i-1}}{y_{i} - y_{i-1}} } =0        if slope changes sign at point.

First derivative (slope) of each end point is calculated by following formula:

f_1^{'}(x_0) = \frac{3(y_1- y_0)}{2(x_1 -x_0)} - \frac{f^{'}(x_1)}{2} \\ f_n^{'}(x_{n+1}) = \frac{3(y_n - y_{n-1})}{2(x_n -x_{n-1})} - \frac{f^{'}(x_{n-1})}{2}

Second derivative are calculated by following formula:

f_i^{''}(x_{i-1}) = \frac{2 [f_i^{'}(x_{i}) + 2f_i^{'}(x_{i-1}) ]}{x_i - x_{i-1}} + \frac{6(y_i - y_{i-1})}{(x_i - x_{i-1})^2} \\ f_i^{''}(x_{i}) = \frac{2 [2f_i^{'}(x_{i}) + f_i^{'}(x_{i-1}) ]}{x_i - x_{i-1}} - \frac{6(y_i - y_{i-1})}{(x_i - x_{i-1})^2}

Solving for the coefficient of curve gives:

d_i = \frac{f_i^{''}(x_{i}) - f_i^{''}(x_{i-1})}{6(x_i - x_{i-1})} \\ c_i = \frac{x_i f_i^{''}(x_{i-1}) -  x_{i-1} f_i^{''}(x_{i}) }{2(x_i - x_{i-1})} \\ b_i = \frac{(y_i - y_{i-1}) - c_i (x_i^2 - x_{i-1}^2) - d_i (x_i^3 - x_{i-1}^3)}{x_i - x_{i-1}} \\ a_i = y_{i-1} - b_{i} x_{i-1} - c_i x_{i-1}^2 - d_i x_{i-1}^3

Conclusion

Constraint vs Natural Spline (Upward)

Constraint vs Cubic (downward)

Constraint cubic spline interpolation has the following advantages as compared to a standard cubic spline.

  • It generates a relatively smooth curve as compare to standard cubic spline
  • Interpolated values are calculated without solving a system of equations.
  • It never overshoots the immediate values.

References:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!