This Demonstration shows global B-spline curve fitting by the least-squares method. B-splines are a generalization of Bezier curves. In a B-spline, each control point is associated with a particular basis function. The implementation is fully described in the Details.
For definitions and details of the B-spline curve and B-spline basis function, see Related Links.
Assume that and are given, where . We seek a -degree nonrational curve
(1) , ,
(2) the remaining are approximated in the least-squares sense; that is,
is a minimum with respect to the variables.
Here, and are precomputed parameter values.
So is a scalar-valued function of the variables . Applying the standard technique of linear least-squares fitting to minimize, set the derivatives with respect to the points, , to zero. The of these derivatives is
which implies that
It follows that
Equation (1) is one linear equation in the unknowns . Letting yields the system of equations in unknowns,
where is the matrix of scalars
is the vector of points
In order to set up equations (2) and (3), a knot vector with parameters is required.
The can be computed using the following method (with , in each case):
(1) Equally spaced
(2) Chord length
let , then
let , then
The placement of the knots should reflect the distribution of the . If , denote by the largest integer less than or equal to . We need a total of knots; there are internal knots and internal knot spans.
Then define the internal knots by
, . (4)
Equation (4) guarantees that every knot span contains at least one , and under this condition the matrix is positive definite and well-conditioned.
 L. Piegl and W. Tiller, The NURBS Book, 2nd ed., Berlin: Springer-Verlag, 1997 pp. 410–413.