# How to calculate cubic spline coefficients from end slopes

I want to know how to calculate cubic spline interpolation coefficients, which uses end point slope constraint.

There are $N$ points $(x_0,y_0),(x_1,y_1),\dots,(x_{N-1},y_{N-1}) \in \mathbb{R}^2$ where $x_0 < x_1 < \cdots < x_{N-1}$. Cubic spline interpolation should give $N-1$ polinomials

$$S_j(x) = a_j+b_j(x-x_j)+c_j(x-x_j)^2+d_j(x-x_j)^3$$

where $j \in \{0,1,\dots,N-2\}$. Because there are $4(N-1)$ unknown variables $a_j,b_j,c_j,d_j$, $4(N-1)$ equations are required to solve.

Like natural cubic splines, we usually get the first $4(N-1)-2$ equations from the following conditions

• $S_i(x_i) = S_{i+1}(x_i)=y_i$ for $i \in \{0,1,\dots,N-1\}$
• $S_i(x_{i+1}) = S_{i+1}(x_{i+1})$ for $i \in \{0,1,\dots,N-1\}$
• $S_i^\prime(x_{i+1}) = S^\prime_{i+1}(x_{i+1})$ for $i \in \{0,1,\dots,N-2\}$
• $S_i^{\prime\prime}(x_{i+1}) = S^{\prime\prime}_{i+1}(x_{i+1})$ for $i \in \{0,1,\dots,N-2\}$

and so two other conditions are required. For natural cubic splines, conditions $$S_0^{\prime\prime}(x_0)=0 , S_{N-1}^{\prime\prime}(x_{N-1})=0$$ are used, but instead, I want to use edge slope conditions $$S_0^{\prime}(x_0)=v_{\rm first},S_{N-1}^{\prime}(x_{N-1})=v_{\rm last}$$ for my issue. How can it be solved?

## 1 Answer

The two conditions $S_0^{\prime}(x_0)=v_{\rm first}$ and $S_{N-1}^{\prime}(x_{N-1})=v_{\rm last}$ give you two more equations. So now you have a total of $4(N-1)$ linear equations for the $4(N-1)$ unknowns, just like in the "natural spline" case. You can solve this system of equations any way you like. The system has some special structure, and you can solve it most efficiently by taking advantage of this structure. But, any linear system solver will work, and the inefficiency probably won't matter unless $N$ is huge (in the thousands).

Not related to your question: Computing cubic splines is much easier if you express each segment in Hermite form, rather than algebraic form. This ensures from the outset that values and first derivatives match, and you only have to solve a linear system that forces second derivatives to match, too. The size of your system of equations is much smaller.

You can find good descriptions of spline technology, plus some useful code in deBoor's book.