Calculating a cubic spline goes wrong I am trying to solve a old exam and really stuck at the cubic splines. 
We have the function $f(x) = \cos^2(\frac{x}{2})$  and the points $x_0 = \frac{\pi}{2}$, $x_1=0$ and $x_2 = \frac{\pi}{2}$. 
With some simple math we get the points: 
$(-\frac{\pi}{2},\frac{1}{2}),(0,1),(\frac{\pi}{2},\frac{1}{2})$.
The splines have the form:
$$ p_i(x) = a_i + b_i(x-x_i) + c_i(x-x_i)^2 + d_i(x-x_i)^3$$
So we get the $a_i$ simply by $a_i = y_i$. 
some formulae that are needed
$$
\begin{align}
h_i &= x_{i+1} - x_i\\
b_i &= \frac{1}{h_i}(a_{i+1}-a_i) - \frac{h_i}{3}(c_{i+1}+2c_i)\\
d_i &= \frac{1}{3h_i}(c_{i+1}-ci)\\
c_1 &= 0, \ \ c_n=0
\end{align}$$
So next step for the calculation is to get the $c_i$ out of $Ac=r$ 
with 
$$ 
A=\left[
2(h_1+h_2) \dots
\right] 
$$
Since we just got 3 points it is an $1\times 1$ matrix (else it would be bigger). So I calculated the matrix $A=(2\pi)$ since $h_1 = h_2 = \frac{\pi}{2}$ with 
$$
r = \frac{3}{h_2}(a_3-a_2)-\frac{3}{h1}(a_2-a_1) 
$$
Out of this we get that $c_2 = -\frac{2}{3}$ if I am not wrong. And now we can calculate the left over coefficients.
$$
\begin{align}
b_1 &= \frac{1}{\pi} + \frac{9\pi}{8} \\
b_2 &= -\frac{1}{\pi} - \frac{18\pi}{8} \\
d_1 &= -\frac{6}{12\pi}\\
d_2 &= \frac{6}{12\pi}
\end{align}
$$
BUT the splines are not correct. For example for the first interval I get
$$
p_1(x) = \frac{1}{2} + (\frac{1}{\pi} + \frac{9\pi}{8})(x-\frac{\pi}{2}) - \frac{6}{12\pi}(x-\frac{\pi}{2})^3\\
p_2(x) = \frac{1}{2} + (\frac{1}{\pi} + \frac{9\pi}{8})(x-0) - \frac{6}{12\pi}(x-0)^3
 $$
So what have i done wrong? Most of the stuff is just using  formulae but I don't get the right solution out of it. 
In the end thanks for reading this long post!
 A: I hope that the mistake I pointed out in a comment will help to straighten out the computation. Meanwhile, let me present a different approach, which may be more illuminating that "put these numbers into these formulas". 
The given data points are symmetric about the $y$-axis. So will be the spline. In particular, the spline will have derivative zero at $x=0$. By symmetry, we only need to find it on the interval $[-\pi/2,0]$. 
First, find the piecewise linear function through given points: on the interval $[-\pi/2,0]$ it is $l(x)=1+x/\pi$. On the whole interval the piecewise linear interpolant looks like this.

Second, look for the cubic correction term $c(x)$, which must be zero at the nodes of interpolation, have zero second derivative at $-\pi/2$, and have derivative $-1/\pi$ at $x=0$ (to cancel out the slope of linear term, which creates a corner on the plot above). It is convenient to use Taylor form at $-\pi/2$, in the following fashion: 
$$c(x) = a\left(\frac{2}{\pi}x + 1\right)^3 + b\left(\frac{2}{\pi}x + 1\right)$$
This choice simplifies the algebra. Indeed, $c(0)=0$ implies $b=-a$ and $c'(0)=-1/\pi$ yields
$$ 3\frac{2}{\pi}a - \frac{2}{\pi}a = -\frac{1}{\pi}$$
hence $a=-1/4$. 
Final result: for $-\pi/2\le x\le 0$ the spline is 
$$
p(x) = 1+\frac{1}{\pi}x - \frac14 \left(\frac{2}{\pi}x + 1\right)^3 + \frac14\left(\frac{2}{\pi}x + 1\right)
$$
On the other interval it's symmetric: $p(x)=p(-x)$. 
Here is the plot of piecewise linear interpolant (blue), cubic correction term (red), and their sum  (black), which is the desired spline.  

Derivation from basic principles is generally more enlightening than mimicking a spline-generating computer routine.
