Example of cubic B-spline I have consider a 1-D problem to figure out how B-splines work. I assume that I want to interpolate on x-values 1,2,3 and 4. I gave random values to my control points, namely w1=0, w2=-1, w3=3 and w4=2. I consider the Basis functions for cubic B-spline $B_0=(1-u)^3/6, B_1=(3u^3-6u^2+4)/6, B_2=(-3u^3+3u^2+3u+1)/6, B_3=u^3/6$. Based on the formula $F(x)= \sum Wi Bi(x)$. I also know that u=x/nx where nx=4 in my case. So 
\begin{equation}
F= 0\times B_0+(-1)\times B_1+3\times B_2+2\times B_3$, 
\end{equation}
after substitute the basis functions and doing some calculationsI end up with
\begin{equation} 
F(u)= 1/6 x (-10u^3+15u^2+9u-1). 
\end{equation}
At x=1 u=1/4 so F(1/4)= 65/192 which is not equal to zero as I expected. Since the control point at x=1 is equals zero then I have to end up with zero right?Because the spline in the end it has to be function that goes through the control points so we can check that. Can you please give me some hint about what I doing wrong? I will really appreciate.

 A: If you want to interpolate values $y_1$, $y_2$, $y_3$, $y_4$ at values $x = 1,2,3,4$, then the required "blending" or "basis" functions are:
$$
\begin{align}
g_1(x) &= \tfrac16 (24 - 26 x + 9 x^2 - x^3)  \\
g_2(x) &= \tfrac12 (-12 + 19 x - 8 x^2 + x^3) \\
g_3(x) &= \tfrac12 (8 - 14 x + 7 x^2 - x^3)   \\
g_4(x) &= \tfrac16 (-6 + 11 x - 6 x^2 + x^3)
\end{align}
$$
These give the curve
$$
C(x) = g_1(x)y_1 + g_2(x)y_2 + g_3(x)y_3 + g_4(x)y_4 = \sum_{i=1}^4 y_i g_i(x) \qquad (*)
$$
You can easily check that $C(i) = y_i$ for $i=1,2,3,4$.
In your particular case, you have $y_1=0$, $y_2=-1$, $y_3=3$, $y_4=2$, and the curve becomes
$$
C(x) = \tfrac16 (96 - 161 x + 75 x^2 - 10 x^3)
$$
Again, you can easily check that this has the right properties.
In some sense, this is not a very interesting "spline" because it has only one cubic segment. But, if you insist on using a linear combination of simple cubic basis functions, then this is what you'll always get. The "$g$" functions given above are the only basis functions that will give the sort of interpolation you seem to want.
Where do the basis functions $g_1$, $g_2$, $g_3$, $g_4$ come from? They are actually the Lagrange polynomials for the nodes $x_1 = 1$, $x_2 = 2$, $x_3 = 3$, $x_4 = 4$. There are (at least) two ways to construct them. The most elementary approach is to assume that $C$ has the form
$$
C(x) = ax^3 + bx^2 + cx + d \qquad (**)
$$
The requirement that $C(x_i) = y_i$ for $i=1,2,3,4$ gives us four linear equations, which we can solve to get $a$, $b$, $c$, $d$ in terms of $y_1$, $y_2$, $y_3$, $y_4$. We plug the solutions back into equation $(**)$, and rearrange terms, and we'll get something in the form of (*), from which we can identify $g_1$, $g_2$, $g_3$, $g_4$. This is a very simple special case of the "interpolating spline" technique that several people have suggested to you.
The more clever approach uses the concept of Lagrange polynomials. A little thought shows that $(*)$ will give us the interpolation we need if the functions $g_i$ have the property $g_i(x_j) = \delta_{ij}$. In other words, $g_i(x_j) = 1$ if $i=j$ and $g_i(x_j) = 0$ if $i \ne j$. We can construct polynomials with this property quite easily. For example, we can define $g_2$ by:
$$
g_2(x) = \frac{(x-x_1)(x-x_3)(x-x_4)}{(x_2-x_1)(x_2-x_3)(x_2-x_4)}
$$
It's clear that this is a cubic polynomial that has the desired properties. If you plug in $x_1 = 1$, $x_3 = 3$, $x_4 = 4$, you'll get $g_2(x) = \tfrac12 (-12 + 19 x - 8 x^2 + x^3)$. The other three $g_1$, $g_3$, $g_4$ can be constructed similarly. 
A: Cubic B-splines have $C^2$ continuity, but are not "interpolating" in general, i.e., they pass NEAR their control points, but not through them. Thus your expectation that $F(1/4)$ should be $0$ is mistaken. 
("Interpolating" is used as an adjective by spline-ologists, and is contrasted with "approximating". B-splines are, in general, approximating splines rather than interpolating splines.)
