<s>[INST] Cubic Spline Interpolation My problem is to find a interpolating cubic spline to the points 
$$\left\{(0,0), \left(\frac{\pi}{2}, 1\right), \left(\pi,0\right), \left(\frac{3\pi}{2}, -1\right),(2\pi,0)\right\}$$
I did as follows:
I set up the system
$$ \begin{cases}
 &  hg_0+2(h+h)g_1+hg_{2} = 6\left(\dfrac{y_{2}-y_1}{h} - \dfrac{y_1-y_{0}}{h}\right)\\ 
 &   hg_{1}+2(h+h)g_2+hg_{3} = 6\left(\dfrac{y_{3}-y_2}{h} - \dfrac{y_2-y_{1}}{h}\right)\\ 
 & hg_{2}+2(h+h)g_3+hg_{4} = 6\left(\dfrac{y_{4}-y_3}{h} - \dfrac{y_3-y_{2}}{h}\right)
\end{cases} $$
Naturally cubic spline, used $ g_0 = g_4 = 0 $.
$$ \begin{cases}
 &  \phantom{hg_0+ }4hg_1+hg_{2} = -\dfrac{12}h\\ 
 &   hg_{1}+4hg_2+hg_{3} = 0\\ 
 & hg_{2}+4hg_3\phantom{ +hg_{4}} = \dfrac{12}{h}
\end{cases} $$
I solved the system and found $ g_1 = -12/\pi^2 $, $ g_2 = 0 $ e $ g_3 = 12/\pi^2 $.
These formulas used to calculate the coefficients.
\begin{equation}
a_k = \frac{g_k-g_{k-1}}{6h_k};\;\;\;\;\;\;b_k = \frac{g_k}{2};\;\;\;\;\;\;
c_k = \frac{y_k-y_{k-1}}{h_k} + \frac{2h_kg_k+g_{k-1}h_k}{6};\;\;\;\;\;\;
d_k = y_k.
\end{equation}
Calculate the values of $a_k$ and of the values of $b_k$.
When I calculate the value of $c_1$, got
$$ c_1 = \frac{y_1-y_{0}}{h} + \frac{2hg_1+g_0h}{6} = \frac{1-0}{\pi/2} + \frac{2(\pi/2)(-12/\pi^2)+0(\pi/2)}{6} = 0 $$
But my book says that the result is $c_1 = 3/\pi$
Where you are wrong?
 [/INST] Find the Cubic Spline for the points:
$$\left\{(0,0), \left(\frac{\pi}{2}, 1\right), \left(\pi,0\right), \left(\frac{3\pi}{2}, -1\right),(2\pi,0)\right\}$$
We will make use of this algorithm, which is certainly equivalent to the one you are using.
Since we have equal spacing, all of the $h_i = \dfrac{\pi}{2}$.
We have:
$$a = \begin{bmatrix} a_0 \\ a_1 \\ a_2 \\ a_3 \\ a_4 \end{bmatrix} = \begin{bmatrix} y_0 \\ y_1 \\ y_2 \\ y_3 \\ y_4 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \\ 0 \\ -1 \\ 0 \end{bmatrix}$$
Forming $Ac = v$ and solving for the $c_i$ terms, yields:
$$\begin{bmatrix} 1 & 0 & 0 & 0 & 0 \\ \dfrac{\pi}{2} & 2 \pi & \dfrac{\pi}{2} & 0 & 0 \\ 0 & \dfrac{\pi}{2} & 2 \pi & \dfrac{\pi}{2} & 0 \\ 0 & 0 & \dfrac{\pi}{2} & 2 \pi & \dfrac{\pi}{2} \\ 0 & 0 & 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} c_0 \\ c_1 \\ c_2 \\ c_3 \\ c_4 \end{bmatrix} = \begin{bmatrix} 0 \\ -\dfrac{12}{\pi} \\ 0 \\ \dfrac{12}{\pi} \\ 0 \end{bmatrix} \implies \begin{bmatrix} c_0 \\ c_1 \\ c_2 \\ c_3 \\ c_4 \end{bmatrix} = \begin{bmatrix} 0 \\ -\dfrac{6}{\pi^2} \\ 0 \\ \dfrac{6}{\pi^2} \\ 0 \end{bmatrix}$$
We now form the $b_i$ terms as:
$$b_i = \dfrac{1}{h_i}(a_{i+1}-a_i) - \dfrac{h_i}{3}(2 c_i + c_{i+1}), i= 0 \ldots 3$$
This yields:
$$b = \begin{bmatrix} b_0 \\ b_1 \\ b_2 \\ b_3 \end{bmatrix} = \begin{bmatrix} \dfrac{3}{\pi} \\ 0 \\ -\dfrac{3}{\pi} \\ 0 \end{bmatrix}$$
We now form the $d_i$ terms as:
$$d_i = \dfrac{1}{3h_i}(c_{i+1}-c_i) , i= 0 \ldots 3$$
This yields:
$$d = \begin{bmatrix} d_0 \\ d_1 \\ d_2 \\ d_3  \end{bmatrix} = \begin{bmatrix} -\dfrac{4}{\pi^3} \\ \dfrac{4}{\pi^3} \\ \dfrac{4}{\pi^3} \\-\dfrac{4}{\pi^3} \end{bmatrix}$$
We are now ready to form the splines as:
$$\begin {align} S_0(x) &= a_0 + b_0(x-x_0)+c_0(x-x_0)^2 + d_0(x-x_0)^3 \\
S_1(x) &= a_1 + b_1(x-x_1)+c_1(x-x_1)^2 + d_1(x-x_1)^3 \\ S_2(x) &= a_2 + b_2(x-x_2)+c_2(x-x_2)^2 + d_2(x-x_2)^3 \\ S_3(x) &= a_3 + b_3(x-x_3)+c_3(x-x_3)^2 + d_3(x-x_3)^3\end{align} $$
The final result is:
$$\begin {align} S_0(x) &= -\dfrac{4 x^3}{\pi ^3} + \dfrac{3 x}{\pi }, x \in \left[0, \dfrac{\pi}{2} \right] \\
 S_1(x)& = \dfrac{4 x^3}{\pi ^3}-\dfrac{12 x^2}{\pi ^2}+\dfrac{9 x}{\pi }-1, x \in \left[\dfrac{\pi}{2}, \pi \right] \\
 S_2(x)& = \dfrac{4 x^3}{\pi ^3}-\dfrac{12 x^2}{\pi ^2}+\dfrac{9 x}{\pi }-1, x \in \left[\pi, \dfrac{3 \pi}{2} \right] \\
 S_3(x) &= -\dfrac{4 x^3}{\pi ^3}+\dfrac{24 x^2}{\pi ^2}-\dfrac{45 x}{\pi }+26,x \in \left[\dfrac{3 \pi}{2}, 2 \pi \right] \end {align}$$
If we plot the splines over their respective ranges, we get:

</s>