Cubic spline solving equation $$S(x)=\begin{cases}
x^3 +4x^2 -2x +7 & \text{ if } -1\leq x\leq 0,  \\ 
  x^3 - 2x^2 +4x +5& \text{   if    }   1\leq x\leq 2,  
\end{cases}$$
is a cubic spline with knots $\{-1, 0, 1, 2\}$ 
Find $s2(x)$ on the interval $0\leq x\leq 1$ 
So $S'(x) = 3x^2 +8x -2$ if $-1\leq x\leq 0$ 
$S'(x)=3x^2 - 4x +4$ if $1\leq x\leq 2$ 
I know We need $s2(0) = 7$, $s2(1) = 8$ and $s2'(0) = -2$ , $s2'(1) = 3$
But I don't know what to actually put into the matrix to solve this.. 
The answer given in class was 

$$s2(x) = -x^3 + 4x^2 -2x +7$$

but I'm not sure how he got that.
 A: The goal in matching a polynomial to points and slopes is to write a system of equations that, when solved, gives the coefficients.  There are two basic equations, shown here for cubics:
$$y=ax^3+bx^2+cx+d$$
$$y'=3ax^2+2bx+c$$
These then can be converted to matrix rows, with $a$, $b$, $c$, $d$ as the targets:
$$\left[\begin{array}{cccc|c}
x^3&x^2&x&1&y\\
3x^2&2x&1&0&y'\\
\end{array}\right]$$
So to find the whole polynomial we set up a row for each constraint.
$$\left[\begin{array}{cccc|c}
0&0&0&1&7\\
1&1&1&1&8\\
0&0&1&0&-2\\
3&2&1&0&3\\
\end{array}\right]$$
Turn this to RREF we get the last column as $[-1,4,-2,7]$, which are the coefficients of the interpolating cubic:
$$y=-x^3+4x^2-2x+7$$
Two things of note:


*

*if you have more constraints, you can include them; you need the order to go up by one for each additional constraint you have: five constraints means you're finding a quartic, six means you're finding a quintic, etc.  At least one of the constraints must be on the actual value and not the derivative.

*if you're going to be doing a lot of this sort of solving and your $x$ coordinates can be easily shifted to $0$ and $1$, you can simply take the inverse of the above matrix and apply it to your $y$ values.  Once done this is a lot faster than trying to run rref every time.
A: $s2(x)=ax^3+bx²+cx+d$
$s2'(x)=3ax²+2bx+c$
You just have to replace $x$ with $\{0,1\}$ and you will get 4 equations with 4 unknowns...
