Cubic Spline Interpolation

Cubic spline interpolation is a special case for Spline interpolation that is used very often to avoid the problem of Runge's phenomenon. This method gives an interpolating polynomial that is smoother and has smaller error than some other interpolating polynomials such as Lagrange polynomial and Newton polynomial.

## Contents

* 1 Definition
* 2 Boundary Conditions
* 3 Methods
* 4 Example
* 5 Exercise
* 6 References

## Definition[edit | edit source]

Given a set of n + 1 data points (xi,yi) where no two xi are the same and  a = x 0 < x 1 < ⋯ < x n = b {\displaystyle a=x_{0}<x_{1}<\cdots <x_{n}=b} , the spline S(x) is a function satisfying:

1. S ( x ) ∈ C 2 [ a , b ] {\displaystyle S(x)\in C^{2}[a,b]} ;
2. On each subinterval  [ x i − 1 , x i ] , S ( x ) {\displaystyle [x_{i-1},x_{i}],S(x)} is a polynomial of degree 3, where  i = 1 , … , n . {\displaystyle i=1,\dotsc ,n.}
3. S ( x i ) = y i , {\displaystyle S(x_{i})=y_{i},} for all  i = 0 , 1 , … , n . {\displaystyle i=0,1,\dotsc ,n.}

Let us assume that

S ( x ) = { C 1 ( x ) , x 0 ≤ x ≤ x 1 ⋯ C i ( x ) , x i − 1 < x ≤ x i ⋯ C n ( x ) , x n − 1 < x ≤ x n {\displaystyle S(x)=\left\\{{\begin{array}{rl}C_{1}(x),&x_{0}\leq x\leq x_{1}\\\\\cdots &\\\C_{i}(x),&x_{i-1}<x\leq x_{i}\\\\\cdots &\\\C_{n}(x),&x_{n-1}<x\leq x_{n}\end{array}}\right.}

where each  C i = a i + b i x + c i x 2 + d i x 3 ( d i ≠ 0 ) {\displaystyle C_{i}=a_{i}+b_{i}x+c_{i}x^{2}+d_{i}x^{3}(d_{i}\neq 0)} is a cubic function,  i = 1 , ⋯ , n {\displaystyle i=1,\cdots ,n} .

## Boundary Conditions[edit | edit source]

To determine this cubic spline S(x), we need to determine  a i , b i , c i and d i {\displaystyle a_{i},b_{i},c_{i}{\mbox{ and }}d_{i}} for each i by:

* C i ( x i − 1 ) = y i − 1 {\displaystyle C_{i}(x_{i-1})=y_{i-1}} and  C i ( x i ) = y i {\displaystyle C_{i}(x_{i})=y_{i}} ,  i = 1 , … , n {\displaystyle i=1,\dotsc ,n} .
* C i ′ ( x i ) = C i + 1 ′ ( x i ) {\displaystyle C_{i}^{'}(x_{i})=C_{i+1}^{'}(x_{i})} ,  i = 1 , … , n − 1 {\displaystyle i=1,\dotsc ,n-1} .
* C i ″ ( x i ) = C i + 1 ″ ( x i ) {\displaystyle C_{i}^{''}(x_{i})=C_{i+1}^{''}(x_{i})} ,  i = 1 , … , n − 1 {\displaystyle i=1,\dotsc ,n-1} .

We can see that there are  n + n + ( n − 1 ) + ( n − 1 ) = 4 n − 2 {\displaystyle n+n+(n-1)+(n-1)=4n-2} conditions, but we need to determine  4 n {\displaystyle 4n} coefficients, so usually we add two boundary conditions to solve this problem.

There are three types of common boundary conditions:

I. First derivatives at the endpoints are known:

C 1 ′ ( x 0 ) = f 0 ′ , and C n ′ ( x n ) = f n ′ {\displaystyle C_{1}^{'}(x_{0})=f_{0}^{'},{\mbox{ and }}C_{n}^{'}(x_{n})=f_{n}^{'}} .

This is called clamped boundary conditions.

II. Second derivatives at the endpoints are known:

C 1 ″ ( x 0 ) = f 0 ″ , and C n ″ ( x n ) = f n ″ {\displaystyle C_{1}^{''}(x_{0})=f_{0}^{''},{\mbox{ and }}C_{n}^{''}(x_{n})=f_{n}^{''}} .

The special case  C 1 ″ ( x 0 ) = C n ″ ( x n ) = 0 {\displaystyle C_{1}^{''}(x_{0})=C_{n}^{''}(x_{n})=0} is called natural or simple boundary conditions.

III. When the exact function f(x) is a periodic function with period  x n − x 0 {\displaystyle x_{n}-x_{0}} , S(x) is a periodic function with period  x n − x 0 {\displaystyle x_{n}-x_{0}} too. Thus

C 1 ( x 0 ) = C n ( x n ) , C 1 ′ ( x 0 ) = C n ′ ( x n ) , and C 1 ″ ( x 0 ) = C n ″ ( x n ) {\displaystyle C_{1}(x_{0})=C_{n}(x_{n}),C_{1}^{'}(x_{0})=C_{n}^{'}(x_{n}),{\mbox{ and }}C_{1}^{''}(x_{0})=C_{n}^{''}(x_{n})} .

The spline functions S(x) satisfying this type of boundary condition are called periodic splines.

## Methods[edit | edit source]

There are several methods that can be used to find the spline function S(x) according to its corresponding conditions. Since there are 4n coefficients to determine with 4n conditions, we can easily plug the values we know into the 4n conditions and then solve the system of equations. Note that all the equations are linear with respect to the coefficients, so this is workable and computers can do it quite well.

The algorithm given in Spline interpolation is also a method by solving the system of equations to obtain the cubic function in the symmetrical form.

The other method used quite often is Cubic Hermite spline, this gives us the spline in Hermite form.

Here, we discuss another method using second derivatives  S ″ ( x i ) = M i ( i = 0 , 1 , ⋯ , n ) {\displaystyle S''(x_{i})=M_{i}(i=0,1,\cdots ,n)} to find the expression for spline  S ( x ) {\displaystyle S(x)} .

Let  h i = x i − x i − 1 {\displaystyle h_{i}=x_{i}-x_{i-1}} ,  i = 1 , ⋯ , n {\displaystyle i=1,\cdots ,n} ,  S ″ ( x i ) = C i ″ ( x i ) = C i + 1 ″ ( x i ) = M i ( i = 1 , ⋯ , n − 1 ) {\displaystyle S''(x_{i})=C''_{i}(x_{i})=C''_{i+1}(x_{i})=M_{i}(i=1,\cdots ,n-1)} and  S ″ ( x 0 ) = C 1 ″ ( x 0 ) = M 0 , and S ″ ( x n ) = C n ″ ( x n ) = M n {\displaystyle S''(x_{0})=C''_{1}(x_{0})=M_{0},{\mbox{ and }}S''(x_{n})=C''_{n}(x_{n})=M_{n}} . Note that  M i {\displaystyle M_{i}} 's are unknown (except for type II boundary condition,  M 0 and M n {\displaystyle M_{0}{\mbox{ and }}M_{n}} are given).

Since each  C i {\displaystyle C_{i}} is a cubic polynomial,  C i ″ {\displaystyle C''_{i}} is linear.

By Lagrange interpolation, we can interpolate each  C i ″ {\displaystyle C''_{i}} on  [ x i − 1 , x i ] {\displaystyle [x_{i-1},x_{i}]} since  C i ″ ( x i − 1 ) = M i − 1 {\displaystyle C''_{i}(x_{i-1})=M_{i-1}} and  C i ″ ( x i ) = M i {\displaystyle C''_{i}(x_{i})=M_{i}} , the Lagrange form of this interpolating polynomial is:

C i ″ ( x ) = M i − 1 x i − x h i + M i x − x i − 1 h i {\displaystyle C''_{i}(x)=M_{i-1}{\frac {x_{i}-x}{h_{i}}}+M_{i}{\frac {x-x_{i-1}}{h_{i}}}} for  x ∈ [ x i − 1 , x i ] {\displaystyle x\in [x_{i-1},x_{i}]} .

Integrating the above equation twice and using the condition that  C i ( x i − 1 ) = y i − 1 {\displaystyle C_{i}(x_{i-1})=y_{i-1}} and  C i ( x i ) = y i {\displaystyle C_{i}(x_{i})=y_{i}} to determine the constants of integration, we have

C i ( x ) = M i − 1 ( x i − x ) 3 6 h i + M i ( x − x i − 1 ) 3 6 h i + ( y i − 1 − M i − 1 h i 2 6 ) x i − x h i + ( y i − M i h i 2 6 ) x − x i − 1 h i for x ∈ [ x i − 1 , x i ] . {\displaystyle C_{i}(x)=M_{i-1}{\frac {(x_{i}-x)^{3}}{6h_{i}}}+M_{i}{\frac {(x-x_{i-1})^{3}}{6h_{i}}}+\left(y_{i-1}-{\frac {M_{i-1}h_{i}^{2}}{6}}\right){\frac {x_{i}-x}{h_{i}}}+\left(y_{i}-{\frac {M_{i}h_{i}^{2}}{6}}\right){\frac {x-x_{i-1}}{h_{i}}}\quad {\text{for}}\quad x\in [x_{i-1},x_{i}].}

|
|

(1)

This expression gives us the cubic spline S(x) if  M i , i = 0 , 1 , ⋯ , n {\displaystyle M_{i},i=0,1,\cdots ,n} can be determined.

For  i = 1 , ⋯ , n − 1 , {\displaystyle i=1,\cdots ,n-1,} when  x ∈ [ x i , x i + 1 ] , {\displaystyle x\in [x_{i},x_{i+1}],} , we can calculate that

C i + 1 ′ ( x ) = − M i ( x i + 1 − x ) 2 2 h i + 1 + M i + 1 ( x − x i ) 2 2 h i + 1 + y i + 1 − y i h i + 1 − M i + 1 − M i 6 h i + 1 . {\displaystyle C'_{i+1}(x)=-M_{i}{\frac {(x_{i+1}-x)^{2}}{2h_{i+1}}}+M_{i+1}{\frac {(x-x_{i})^{2}}{2h_{i+1}}}+{\frac {y_{i+1}-y_{i}}{h_{i+1}}}-{\frac {M_{i+1}-M_{i}}{6}}h_{i+1}.}

Therefore,  C i + 1 ′ ( x i ) = − M i h i + 1 2 + y i + 1 − y i h i + 1 − M i + 1 − M i 6 h i + 1 . {\displaystyle C'_{i+1}(x_{i})=-M_{i}{\frac {h_{i+1}}{2}}+{\frac {y_{i+1}-y_{i}}{h_{i+1}}}-{\frac {M_{i+1}-M_{i}}{6}}h_{i+1}.}

Similarly, when  x ∈ [ x i − 1 , x i ] , {\displaystyle x\in [x_{i-1},x_{i}],} , we can shift the index to obtain

C i ′ ( x ) = − M i − 1 ( x i − x ) 2 2 h i + M i ( x − x i − 1 ) 2 2 h i + y i − y i − 1 h i − M i − M i − 1 6 h i . {\displaystyle C'_{i}(x)=-M_{i-1}{\frac {(x_{i}-x)^{2}}{2h_{i}}}+M_{i}{\frac {(x-x_{i-1})^{2}}{2h_{i}}}+{\frac {y_{i}-y_{i-1}}{h_{i}}}-{\frac {M_{i}-M_{i-1}}{6}}h_{i}.}

|
|

(2)

Thus,  C i ′ ( x i ) = M i h i 2 + y i − y i − 1 h i − M i − M i − 1 6 h i . {\displaystyle C'_{i}(x_{i})=M_{i}{\frac {h_{i}}{2}}+{\frac {y_{i}-y_{i-1}}{h_{i}}}-{\frac {M_{i}-M_{i-1}}{6}}h_{i}.}

Since  C i + 1 ′ ( x i ) = C i ′ ( x i ) {\displaystyle C'_{i+1}(x_{i})=C'_{i}(x_{i})} , we can derive

μ i M i − 1 + 2 M i + λ i M i + 1 = d i for i = 1 , 2 , ⋯ , n − 1 , {\displaystyle \mu _{i}M_{i-1}+2M_{i}+\lambda _{i}M_{i+1}=d_{i}\quad {\text{for}}\quad i=1,2,\cdots ,n-1,}

|
|

(3)

where

μ i = h i h i + h i + 1 , λ i = 1 − μ i = h i + 1 h i + h i + 1 , and d i = 6 f [ x i − 1 , x i , x i + 1 ] {\displaystyle \mu _{i}={\frac {h_{i}}{h_{i}+h_{i+1}}},\quad \lambda _{i}=1-\mu _{i}={\frac {h_{i+1}}{h_{i}+h_{i+1}}},\quad {\text{and}}\quad d_{i}=6f[x_{i-1},x_{i},x_{i+1}]\,}

|
|

(4)

and  f [ x i − 1 , x i , x i + 1 ] {\displaystyle f[x_{i-1},x_{i},x_{i+1}]} is a  divided difference.

According to different boundary conditions, we can solve the system of equations above to obtain the values of  M i {\displaystyle M_{i}} 's.

I. For type I boundary condition, we are given  C 1 ′ ( x 0 ) = f 0 ′ {\displaystyle C'_{1}(x_{0})=f'_{0}} and  C n ′ ( x n ) = f n ′ {\displaystyle C'_{n}(x_{n})=f'_{n}} . According to equation (2), we can obtain

C 1 ′ ( x 0 ) = − M 0 ( x 1 − x 0 ) 2 2 h 1 + M 1 ( x 0 − x 0 ) 2 2 h 1 + y 1 − y 0 h 1 − M 1 − M 0 6 h 1 . {\displaystyle C'_{1}(x_{0})=-M_{0}{\frac {(x_{1}-x_{0})^{2}}{2h_{1}}}+M_{1}{\frac {(x_{0}-x_{0})^{2}}{2h_{1}}}+{\frac {y_{1}-y_{0}}{h_{1}}}-{\frac {M_{1}-M_{0}}{6}}h_{1}.}
⇒ f 0 ′ = − M 0 h 1 2 + f [ x 0 , x 1 ] − M 1 − M 0 6 h 1 {\displaystyle \Rightarrow f'_{0}=-M_{0}{\frac {h_{1}}{2}}+f[x_{0},x_{1}]-{\frac {M_{1}-M_{0}}{6}}h_{1}}

⇒ 2 M 0 + M 1 = 6 h 1 ( f [ x 0 , x 1 ] − f 0 ′ ) = 6 f [ x 0 , x 0 , x 1 ] {\displaystyle \Rightarrow 2M_{0}+M_{1}={\frac {6}{h_{1}}}(f[x_{0},x_{1}]-f'_{0})=6f[x_{0},x_{0},x_{1}]} .

|
|

(5.1)

Similarly, simplifying

C n ′ ( x n ) = − M n − 1 ( x n − x n ) 2 2 h n + M n ( x n − x n − 1 ) 2 2 h n + y n − y n − 1 h n − M n − M n − 1 6 h n {\displaystyle C'_{n}(x_{n})=-M_{n-1}{\frac {(x_{n}-x_{n})^{2}}{2h_{n}}}+M_{n}{\frac {(x_{n}-x_{n-1})^{2}}{2h_{n}}}+{\frac {y_{n}-y_{n-1}}{h_{n}}}-{\frac {M_{n}-M_{n-1}}{6}}h_{n}}

we will have

M n − 1 + 2 M n = 6 h n ( f n ′ − f [ x n − 1 , x n ] ) = 6 f [ x n − 1 , x n , x n + 1 ] {\displaystyle M_{n-1}+2M_{n}={\frac {6}{h_{n}}}(f'_{n}-f[x_{n-1},x_{n}])=6f[x_{n-1},x_{n},x_{n+1}]} .

|
|

(5.2)

Therefore, let  λ 0 = μ n = 1 , d 0 = 6 f [ x 0 , x 0 , x 1 ] {\displaystyle \lambda _{0}=\mu _{n}=1,d_{0}=6f[x_{0},x_{0},x_{1}]} and  d n = 6 f [ x n − 1 , x n , x n ] {\displaystyle d_{n}=6f[x_{n-1},x_{n},x_{n}]} , combine (3), (5.1) and (5.2) together, so the system of equations that we need to solve is

[ 2 λ 0 μ 1 2 λ 1 ⋱ ⋱ ⋱ ⋱ ⋱ ⋱ ⋱ ⋱ ⋱ μ n − 1 2 λ n − 1 μ n 2 ] [ M 0 M 1 ⋮ ⋮ ⋮ M n − 1 M n ] = [ d 0 d 1 ⋮ ⋮ ⋮ d n − 1 d n ] . {\displaystyle \left[{\begin{array}{ccccccc}2&\lambda _{0}&&&&&\\\\\mu _{1}&2&\lambda _{1}&&&&\\\&\ddots &\ddots &\ddots &&&\\\&&\ddots &\ddots &\ddots &&\\\&&&\ddots &\ddots &\ddots &\\\&&&&\mu _{n-1}&2&\lambda _{n-1}\\\&&&&&\mu _{n}&2\end{array}}\right]\left[{\begin{array}{c}M_{0}\\\M_{1}\\\\\vdots \\\\\vdots \\\\\vdots \\\M_{n-1}\\\M_{n}\end{array}}\right]=\left[{\begin{array}{c}d_{0}\\\d_{1}\\\\\vdots \\\\\vdots \\\\\vdots \\\d_{n-1}\\\d_{n}\end{array}}\right].}

|
|

(6)

II. For type II boundary condition, we are given

M 0 = f 0 ″ {\displaystyle M_{0}=f''_{0}} and  M n = f n ″ {\displaystyle M_{n}=f''_{n}}

|
|

(7)

directly, so let  λ 0 = μ n = 0 {\displaystyle \lambda _{0}=\mu _{n}=0} ,  d 0 = 2 f 0 ″ {\displaystyle d_{0}=2f''_{0}} , and  d n = 2 f n ″ {\displaystyle d_{n}=2f''_{n}} , and we need to solve the system of equations in the same form as (6).

## Example[edit | edit source]

For points (0,0),(1,0.5), (2,2) and (3,1.5), find the interpolating cubic spline  S ( x ) {\displaystyle S(x)} satisfying  S ′ ( 0 ) = 0.2 {\displaystyle S'(0)=0.2} and  S ′ ( 3 ) = − 1 {\displaystyle S'(3)=-1} .

Solution:

We can easily see that  h i = 1 {\displaystyle h_{i}=1} for  i = 1 , 2 , 3 {\displaystyle i=1,2,3} so  λ 0 = 1 , λ 1 = λ 2 = μ 1 = μ 2 = 1 2 {\displaystyle \lambda _{0}=1,\lambda _{1}=\lambda _{2}=\mu _{1}=\mu _{2}={\frac {1}{2}}} and  μ 3 = 1. {\displaystyle \mu _{3}=1.}

Also, since this is the type I boundary condition problem, we can calculate that

f [ x 0 , x 1 , x 0 ] = f [ x 0 , x 0 , x 1 ] = ( f [ x 1 , x 0 ] − f [ x 0 , x 0 ] ) / ( x 1 − x 0 ) = ( 0.5 − 0 1 − 0 − 0.2 ) / 1 = 0.3 ; {\displaystyle f[x_{0},x_{1},x_{0}]=f[x_{0},x_{0},x_{1}]=(f[x_{1},x_{0}]-f[x_{0},x_{0}])/(x_{1}-x_{0})=\left({\frac {0.5-0}{1-0}}-0.2\right)/1=0.3;}
f [ x 0 , x 1 , x 2 ] = f [ x 0 , x 1 , x 2 ] = ( f [ x 2 , x 1 ] − f [ x 1 , x 0 ] ) / ( x 2 − x 0 ) = ( 2 − 0.5 2 − 1 − 0.5 − 0 1 − 0 ) / 2 = 0.5 ; {\displaystyle f[x_{0},x_{1},x_{2}]=f[x_{0},x_{1},x_{2}]=(f[x_{2},x_{1}]-f[x_{1},x_{0}])/(x_{2}-x_{0})=\left({\frac {2-0.5}{2-1}}-{\frac {0.5-0}{1-0}}\right)/2=0.5;}
f [ x 1 , x 2 , x 3 ] = f [ x 1 , x 2 , x 3 ] = ( f [ x 3 , x 2 ] − f [ x 2 , x 1 ] ) / ( x 3 − x 1 ) = ( 1.5 − 2 3 − 2 − 2 − 0.5 2 − 1 ) / 2 = − 1 ; {\displaystyle f[x_{1},x_{2},x_{3}]=f[x_{1},x_{2},x_{3}]=(f[x_{3},x_{2}]-f[x_{2},x_{1}])/(x_{3}-x_{1})=\left({\frac {1.5-2}{3-2}}-{\frac {2-0.5}{2-1}}\right)/2=-1;} and
f [ x 3 , x 2 , x 3 ] = f [ x 2 , x 3 , x 3 ] = ( f [ x 3 , x 3 ] − f [ x 2 , x 3 ] ) / ( x 3 − x 2 ) = ( − 1 − 1.5 − 2 3 − 2 ) / 1 = − 1 2 . {\displaystyle f[x_{3},x_{2},x_{3}]=f[x_{2},x_{3},x_{3}]=(f[x_{3},x_{3}]-f[x_{2},x_{3}])/(x_{3}-x_{2})=\left(-1-{\frac {1.5-2}{3-2}}\right)/1=-{\frac {1}{2}}.}

Therefore, plug into the system of equations, we have

[ 2 1 1 / 2 2 1 / 2 1 / 2 2 1 / 2 1 2 ] [ M 0 M 1 M 2 M 3 ] = [ 6 × 0.3 6 × 0.5 6 × ( − 1 ) 6 × ( − 1 / 2 ) ] . {\displaystyle \left[{\begin{array}{cccc}2&1&&\\\1/2&2&1/2&\\\&1/2&2&1/2\\\&&1&2\end{array}}\right]\left[{\begin{array}{c}M_{0}\\\M_{1}\\\M_{2}\\\M_{3}\end{array}}\right]=\left[{\begin{array}{c}6\times 0.3\\\6\times 0.5\\\6\times (-1)\\\6\times (-1/2)\end{array}}\right].}

The solution is  M 0 = − 0.36 , M 1 = 2.52 , M 2 = − 3.72 {\displaystyle M_{0}=-0.36,M_{1}=2.52,M_{2}=-3.72} and  M 3 = 0.36. {\displaystyle M_{3}=0.36.}

Therefore, by the general expression of the solution, we have

C 1 ( x ) = M 0 ( x 1 − x ) 3 6 + M 1 ( x − x 0 ) 3 6 + ( y 0 − M 0 6 ) x 1 − x 1 + ( y 1 − M 1 6 ) x − x 0 1 = − 0.36 ( 1 − x ) 3 6 + 2.52 x 3 6 + 0.36 6 ( 1 − x ) + ( 0.5 − 2.52 6 ) x = 0.06 ( x − 1 ) 3 + 0.42 x 3 + 0.06 ( 1 − x ) + 0.08 x = 0.48 x 3 − 0.18 x 2 + 0.2 x . {\displaystyle {\begin{aligned}C_{1}(x)&=M_{0}{\frac {(x_{1}-x)^{3}}{6}}+M_{1}{\frac {(x-x_{0})^{3}}{6}}+(y_{0}-{\frac {M_{0}}{6}}){\frac {x_{1}-x}{1}}+(y_{1}-{\frac {M_{1}}{6}}){\frac {x-x_{0}}{1}}\\\&=-0.36{\frac {(1-x)^{3}}{6}}+2.52{\frac {x^{3}}{6}}+{\frac {0.36}{6}}(1-x)+(0.5-{\frac {2.52}{6}})x\\\&=0.06(x-1)^{3}+0.42x^{3}+0.06(1-x)+0.08x\\\&=0.48x^{3}-0.18x^{2}+0.2x\,.\end{aligned}}}

Similarly,

C 2 ( x ) = − 1.04 ( x − 1 ) 3 + 1.26 ( x − 1 ) 2 + 1.28 ( x − 1 ) + 0.5 , {\displaystyle C_{2}(x)=-1.04(x-1)^{3}+1.26(x-1)^{2}+1.28(x-1)+0.5,} and
C 3 ( x ) = 0.68 ( x − 2 ) 3 − 1.86 ( x − 2 ) 2 + 0.68 ( x − 2 ) + 2. {\displaystyle C_{3}(x)=0.68(x-2)^{3}-1.86(x-2)^{2}+0.68(x-2)+2.}

Thus the cubic spline is

S ( x ) = { 0.48 x 3 − 0.18 x 2 + 0.2 x , 0 ≤ x ≤ 1 − 1.04 ( x − 1 ) 3 + 1.26 ( x − 1 ) 2 + 1.28 ( x − 1 ) + 0.5 , 1 < x ≤ 2 0.68 ( x − 2 ) 3 − 1.86 ( x − 2 ) 2 + 0.68 ( x − 2 ) + 2 , 2 < x ≤ 3 {\displaystyle S(x)=\left\\{{\begin{array}{rl}0.48x^{3}-0.18x^{2}+0.2x,&0\leq x\leq 1\\\\-1.04(x-1)^{3}+1.26(x-1)^{2}+1.28(x-1)+0.5,&1<x\leq 2\\\0.68(x-2)^{3}-1.86(x-2)^{2}+0.68(x-2)+2,&2<x\leq 3\end{array}}\right.}

## Exercise[edit | edit source]

For points (0,0), (1,0.5), (2,2) and (3,1.5), find the interpolating cubic spline  S ( x ) {\displaystyle S(x)} satisfying  S ″ ( 0 ) = − 0.3 {\displaystyle S''(0)=-0.3} and  S ″ ( 3 ) = 3.3 {\displaystyle S''(3)=3.3} .

Since this is the type II boundary condition, we use
M 0 = {\displaystyle M_{0}=}

and  M 3 = {\displaystyle M_{3}=}

Also, we have
λ 0 = {\displaystyle \lambda _{0}=}

,  μ 3 = {\displaystyle \mu _{3}=}

,  d 0 = {\displaystyle d_{0}=}

and  d 3 = {\displaystyle d_{3}=}

Same as the above example, we have
λ 1 = λ 2 = μ 1 = μ 2 = {\displaystyle \lambda _{1}=\lambda _{2}=\mu _{1}=\mu _{2}=}

and
d 1 = {\displaystyle d_{1}=}

and  d 2 = {\displaystyle d_{2}=}

Therefore, we can construct the system of equations:

Solution:

[ 2 0 1 / 2 2 1 / 2 1 / 2 2 1 / 2 0 2 ] [ M 0 M 1 M 2 M 3 ] = [ − 0.6 3 − 6 6.6 ] . {\displaystyle \left[{\begin{array}{cccc}2&0&&\\\1/2&2&1/2&\\\&1/2&2&1/2\\\&&0&2\end{array}}\right]\left[{\begin{array}{c}M_{0}\\\M_{1}\\\M_{2}\\\M_{3}\end{array}}\right]=\left[{\begin{array}{c}-0.6\\\3\\\\-6\\\6.6\end{array}}\right].}

Since  M 0 = − 0.3 {\displaystyle M_{0}=-0.3} and  M 3 = 3.3 {\displaystyle M_{3}=3.3} , we can find the solution:

M 1 = 2.7 {\displaystyle M_{1}=2.7} and  M 2 = − 4.5 {\displaystyle M_{2}=-4.5} .

Plug these into the general expression for cubic spline and simplify, we can obtain

S ( x ) = { 0.5 x 3 − 0.15 x 2 + 0.15 x , 0 ≤ x ≤ 1 − 1.2 ( x − 1 ) 3 + 1.35 ( x − 1 ) 2 + 1.35 ( x − 1 ) + 0.5 , 1 < x ≤ 2 1.3 ( x − 2 ) 3 − 2.25 ( x − 2 ) 2 + 0.45 ( x − 2 ) + 2 , 2 < x ≤ 3 {\displaystyle S(x)=\left\\{{\begin{array}{rl}0.5x^{3}-0.15x^{2}+0.15x,&0\leq x\leq 1\\\\-1.2(x-1)^{3}+1.35(x-1)^{2}+1.35(x-1)+0.5,&1<x\leq 2\\\1.3(x-2)^{3}-2.25(x-2)^{2}+0.45(x-2)+2,&2<x\leq 3\end{array}}\right.}

You can see the difference of the two cubic splines in Figure 1.

Figure 1: Interpolating Cubic Splines

## References[edit | edit source]

Polynomial and Spline Interpolation, http://www.ohiouniversityfaculty.com/youngt/IntNumMeth/lecture19.pdf

数值分析，李庆扬，王能超，易大易，2001. ISBN 7-302-04561-5 (Numerical Analysis, Qinyang Li, Nengchao Wang, Dayi yi.)