# 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 $$F= 0\times B_0+(-1)\times B_1+3\times B_2+2\times B_3,$$

after substitute the basis functions and doing some calculationsI end up with

$$F(u)= 1/6 x (-10u^3+15u^2+9u-1).$$

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.

• Closely related to this question: math.stackexchange.com/questions/699113/… and this one math.stackexchange.com/questions/702980/…. Both are from this same user. I gave it two good shots, but was not successful in solving his problems. I'll let someone else try, this time. – bubba Mar 13 '14 at 9:42
• Your explanations was really really good and I am really appreciate. But what is confused me and I am still confuse about is for the knots. You mention that the knot vector $1,2,3,4$ would not work. If I have 4 x-values as presented above I can't use B-spline interpolation? This is what I mean with the $1,2,3,4.$ You told me to write it like this $(0,0,0,0,1,2,3,4,5,5,5,5)$. This is what it doesn't make sense to me and confuse me. The points that I want to interpolate are the $1,2,3,4$. – user105627 Mar 13 '14 at 10:16

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.

• How did you end up with these basis functions? Also you wrote the curve $C(u)$ as a function of $x$. What am I supposed to substitute on my curve the value $u=1/4$ or the value $x=1$? – user105627 Mar 13 '14 at 11:15
• Sorry. Removed the $u$ variable. Use your $x$ values. – bubba Mar 13 '14 at 11:18
• How did you find the new basis functions $g_1(x),g_2(x),g_3(x),g_4(x)$? – user105627 Mar 13 '14 at 11:27
• First, please confirm that they solve your problem. If they don't, then I don't want to spend any time explaining them. – bubba Mar 13 '14 at 11:33
• I had made the substitution on $C(x)$ with $x=1,2,3,4$ and it works fine I ended up with $y=0,-1,3,2$ respectively. – user105627 Mar 13 '14 at 11:55

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.)

• For the $F(1/4)\approx 0.34$ which is pretty close to the control point $0$. But at $x=2$ $u=1/2$ and $F(1/2)=1$ and the control point $w_1=-1$ which is quite far the same holds for $x=3$ $u=3/4$ where $F(3/4)\approx 5.88$ and the corresponding control point was $w_2=3$. Why for the other control points there is such a big difference between the $F$ value and the control points? I did something wrong? – user105627 Mar 12 '14 at 23:26
• I didn't actually check your work. I just happened to know that B-splines generally don't interpolate their control points, so you should not have expected this one to do so. To be honest, I can't make much sense of what you wrote with "u" and "x" both appearing in your formula. It might help others answer if you gave a reference for your B-spline notation; it's been written down many many ways over the years. – John Hughes Mar 12 '14 at 23:44
• Here is the formula: $S(x)= \sum_{i=0}^3 B_i(u) w_{i+k}$ where $i=\lfloor {x/n_x} \rfloor-1$ and $u=x/n_x- \lfloor {x/n_x}\rfloor$. The basis functions are described above. – user105627 Mar 13 '14 at 9:31