In the following, two versions of our algorithm are presented: one using a cubic spline and the other using a quintic spline for approximating the BVP. As the derivations and resulting algorithms are similar, we show the connecting links by presenting both methods in parallel. The version based on cubic splines is naturally simpler, although, using quintic splines leads to a smoother approximation. Indeed it is
- instead of
-continuous, which can be preferable for some use cases. Moreover, quintic splines allow us to directly preset first and second-order derivatives at both boundaries, which otherwise requires the introduction of virtual control points and in turn can lead to poor results, as discussed in
Section 3. Although deriving the proposed collocation algorithm for quintic splines is more advanced than its cubic counterpart, overall performance is superior, because the same approximation quality can be obtained with less collocation sites and hence with less computational effort as shown in
Section 3. However, this requires the underlying ODE to be sufficiently smooth. Note that, in contrast to our intention, most other investigations choose quintic splines for approximating fourth-order ODEs, e.g., in [
8,
10,
11,
17] (similar to choosing cubic splines for second-order ODEs).
Having a background in mechanical engineering, the authors intention is to present a simple and self-contained derivation of the proposed algorithm, which can be easily understood, implemented, and extended by readers also lacking a dedicated mathematical background.
2.1. Problem Statement
Consider the second-order linear time-variant ODE
where
and
denote the first and second derivative of the unknown function
with respect to time
t, i.e.,
Note that
t does not have to represent time. However, this synonym is used in the following due to the typical appearance of (
1) in dynamical systems. The coefficients
,
,
, and the right-hand side
are arbitrary, in general nonlinear, but known functions of
t. Let system (
1) be constrained by the BCs
where
and
define the considered time interval
and
,
,
,
,
,
are user-defined constants. Then system (
1) together with the BCs (
2) represents the second-order linear two-point BVP for which an approximation is to be found. Note that (
2) considers Dirichlet, Neumann, and second-order BCs independently of each other. In contrast, various other algorithms assume Robin BCs, i.e., a linear combination of Dirichlet and Neumann BCs, which is not equivalent to our approach. Due to (
2), the BVP is over-determined and the existence of a solution
depends on the consistency of the BCs with the ODE.
In the following, we investigate the approximation of
through spline collocation, i.e., we generate a spline
which satisfies the underlying ODE (
1) at a user-defined set of distinct collocation sites
, numbered in increasing order, which lie within the considered interval
, i.e.,
Moreover,
is forced to fulfill the BCs (
2) at
and
, i.e.,
Here we use to denote the approximating spline while the exact solution is represented by . For clarity, we also use different denominations for and by using the terms collocation sites and collocation points, respectively. While are user-defined parameters, describe the solution to be found.
Since the proposed collocation algorithm is strongly related to the interpolation of cubic/quintic splines, which may not be common to some readers, spline interpolation is recapitulated in
Section 2.3. Then, the proposed collocation method is derived in
Section 2.7. Moreover, we reuse core elements of the interpolation algorithm during collocation, thus we cannot omit its derivation.
2.2. Spline Parametrization
Before diving into the derivation of algorithms, one first has to decide which spline representation to use. In the literature, formulations such as basis splines (B-splines) are common, since they feature inherent continuity and local control, which typically leads to banded systems [
20]. In general, B-splines do not pass through their control points, which seems to make interpolation difficult at first sight; however, efficient algorithms for interpolation and collocation exist, see, for example, [
21]. In [
20], it has been shown that B-splines might not be as stable and efficient as other representations, namely monomial and Hermite type bases, especially when it comes to implementation. In particular, monomial bases have been recommended due to their superior condition, and thus lower roundoff errors. For all three forms, B-spline, Hermite type, and monomial, the core operation during Gaussian collocation is typically the solution of an almost block diagonal (ABD) linear system of equations [
6,
21]. A generic solver for these type of systems is SOLVEBLOK [
22], while the special structure occurring for monomial bases is exploited by ABDPACK [
23] with increased speed and lower memory consumption. Unfortunately, for smoothest spline collocation as presented in the following, the corresponding collocation matrix is dense, thus we cannot apply these algorithms. However, when compared to Gaussian collocation, the count of collocation sites and thus the dimension of the corresponding LSE is much smaller, which can lead to comparable performance.
Despite the popularity of B-splines, we use the so-called piecewise polynomial (PP) form [
21], which describes the spline through the coefficients of interconnected, but independently defined, polynomial segments. We use a special type of monomial bases, namely the canonical form of the polynomials, which may not be as efficient as the choice in [
20]; however, it makes our algorithm much simpler. By using this formulation, continuity between the spline segments needs to be explicitly established. The evaluation of the resulting spline, however, boils down to the evaluation of a single polynomial belonging to the corresponding segment, which is in general much quicker than evaluating the equivalent B-spline form. The evaluation of B-splines of degree
p with the well-known de Boor’s algorithm [
24] takes
operations [
25]. There are optimized versions of it as proposed in [
25,
26]; however, these are numerically less stable [
27]. In contrast, evaluating a polynomial of degree
p with Horner’s method [
28] takes only
, i.e.,
, operations [
29]. This is essential for time-critical applications, where the resulting spline has to be evaluated as quickly as possible. Note that we are free to construct the spline in B-spline formulation and convert it to the corresponding PP form in a post-processing step, see [
21]. However, this introduces an additional (expensive) step which we try to avoid since, in our case, not only the evaluation but also the construction of the spline is time critical.
Let the spline
be defined as
where
represents the
i-th of the
spline segments parametrized by the normalized interpolation parameter
. We call
and
the global and local interpolation parameters, respectively, for which we choose the linear mapping
with
as the duration of the
i-th segment
and its reciprocal
. The partitioning of the spline into
n segments is visualized in
Figure 3. In the following, we predominantly derive expressions in local segment space, i.e., with respect to
, since this makes the notation clearer, especially in
Section 2.7. Note that, in contrast to some other approaches, we do not require homogeneous partitioning, thus the segmentation can be chosen arbitrarily as long as spline knots do not coincide. However, in
Section 2.3, we show that for best numerical stability uniform partitioning should be used.
In the case of cubic splines (left subscript
C), each segment
represents a polynomial of degree three,
where
,
,
, and
are its constant coefficients. The first two derivatives of
with respect to
t are obtained by applying the chain rule:
For quintic splines (left subscript
Q), each segment
represents a polynomial of degree five,
where
,
,
,
,
, and
are its constant coefficients. As in the cubic case, we obtain the first four derivatives of
with respect to
t through the chain rule:
2.3. Spline Interpolation: Preliminaries
In the following, we recall how a given set of
data points
with
can be interpolated with a
or
smooth cubic or quintic spline, respectively. The derivation explicitly uses the PP form of the spline and leads to the same algorithm as presented in [
18], except for slight modifications in notation. Note that [
18] only deals with quintic splines. However, the method for cubic segments presented in this paper is a simplified version of the same scheme. Moreover, the derivation is investigated in more detail than it is in [
18]. Readers not interested in these details are referred to Algorithm 1 which summarizes the results of this section.
In contrast to [
18], we only consider the case of predefined first- and second-order derivatives at the boundaries of the quintic spline, i.e., we assume
,
,
, and
to be given (as indicated in
Figure 3). For the cubic counterpart, we lose two degrees of freedom, allowing us to predefine only two constraints out of
. For the remainder of this section, we restrict ourselves to the case of predefined second-order time derivatives
and
as this allows an efficient algorithm similar to the one presented for quintic splines. Note that this choice includes the common case of natural cubic splines, i.e.,
. For cubic splines, we postpone the enforcement of the remaining boundary conditions, i.e.,
and
, to the end of
Section 2.7.
In the following, we only consider distinct and ascending interpolation sites, i.e., for .
2.4. Cubic Spline Interpolation: Derivation
Since our goal is a spline which passes through the given data points
, we enforce the interpolation constraints
Furthermore, we aim at
continuity, thus we further require that
Inserting (
7) and (
9) into (
15) and in the second row of (
16) allows us to reformulate the spline coefficients as
where
for
are the
unknowns which have to be computed using the remaining
equations given by the first row of (
16). For this purpose, we expand the first row of (
16) with (
8) and insert
,
,
and
from (
17) to obtain
which holds for
. This equation can be considered as defining intermediate unknowns
and can be written as LSE of the form
with
and
,
given by
and their elements
Herein
,
, and
represent the lower diagonal, diagonal, and upper diagonal elements of
, respectively. Note that
only depends on the choice of
, thus it can be reused in case we need to perform further interpolations with the same segmentation
. While
represents the interpolation sites
, the interpolation points
are encoded in
. The additional term
incorporates the BCs such that we can write the system in the neat form of (
20). From the solution
we can directly obtain the unknowns
which in turn can be used together with the data points
and BCs to compute the segment coefficients (
17) such that the spline
is fully defined. Note that
is symmetric, i.e.,
; however, we do not make use of this property.
Although one can compute
from (
19) using an arbitrary solver for linear systems of equations, there is a more efficient way for doing so: since
is tridiagonal, we can solve (
19) with the Thomas algorithm [
30,
31]. Derived from an
decomposition of
, one performs a recursive forward elimination
followed by a backward substitution
Computing
out of
and
thus boils down to
operations. Note that in contrast to [
31] where
in our case
, thus
n changes to
for computing the count of operations in total [
31]. Since
is symmetric and positive definite, one may think of using an algorithm based on Cholesky factorization instead of
decomposition, as this has proven to be approximately twice as efficient where applicable. However, this rule of thumb seems to be no longer valid for the special case of tridiagonal matrices: the Cholesky factorization
in [
32], where the computation of the lower diagonal matrix
L exploits the special structure and the diagonal matrix
is used to avoid evaluating expensive square roots, leads to
operations in total.
The numerical stability of cubic spline interpolation is shown in
Appendix A.1.
2.5. Quintic Spline Interpolation: Derivation
As previously mentioned, the following is a detailed version of the derivation given in [
18]. Just as in the cubic case, our task is to pass through the given data points
, thus we enforce the interpolation constraints
We use the additional degrees of freedom to enforce not only
, but instead
continuity with
Inserting (
10), (
11), and (
12) into (
28) and into the first two rows of (
29) allows us to reformulate the spline coefficients to
where
and
for
are the
unknowns which we still have to determine using the remaining
equations given by the last two rows of (
29). In particular, we expand the fourth row of (
29) with (
14) and insert
,
, and
from (
30) to obtain
In the same manner, we expand the third row of (
29) with (
13) and insert
,
,
and
from (
30) which leads to
Both (
31) and (
32) hold for
which allows casting them in the form of
with
and
,
given by
and their block components
Just as in the cubic case,
,
, and
represent the lower diagonal, diagonal, and upper diagonal blocks of
, respectively. As suggested in [
18], the additional parameter
in the definitions (
35)–(
38) is chosen as
From an analytical point of view,
has no influence on the solution
(at least if
). However, it improves numerical stability which is verified in
Appendix A.2.
As for the cubic spline
is symmetric and only depends on the choice of
, while the interpolation points
are contained in
. As before, the additional term
incorporates the BCs such that we can write the system in the form of (
34). In contrast to the cubic case, the solution
represents not only
, but also
, which is now additionally required to compute the segment coefficients from (
30).
Since
is block-tridiagonal, we can again solve (
33) efficiently with the generalization of the Thomas algorithm to block-tridiagonal matrices [
31]. Based on an
decomposition of
, we first run a recursive forward elimination
followed by a backward substitution
Computing
out of
and
requires at a maximum
operations. Again [
31] uses
while in our case
holds, thus
n changes to
for computing the count of operations in total [
31]. For this upper bound, explicit computation of the inverse of
and
by Gaussian elimination is assumed, which in practice should be avoided by solving a
LSE instead [
31]. Thus, a corresponding implementation can be expected to require even less operations.
In
Appendix A.2, the interested reader can find considerations on the numerical stability of quintic spline interpolation.
2.7. Spline Collocation: Derivation
The following is based on the collocation algorithm presented in [
19,
33]. However, we extend the method from cubic to quintic splines. Moreover, we do not use natural splines, but instead integrate the boundary conditions directly into the scheme. Lastly, in contrast to [
19,
33], we do not need to modify the right-hand side of (
1), thus leading to a “true” collocation of the ODE for all collocation sites, which are chosen to be the interior spline knots. As runtime performance is of the highest priority for our application, we choose smoothest spline collocation. This minimizes the count of (expensive) collocation sites, thus reduces the count of equations to solve, and instead uses the available degrees of freedom to force
(cubic spline) or
(quintic spline) continuity. Moreover, in our application,
is used as input for controlling the motion of a robot, thus a smooth
is equivalent to small changes in joint accelerations, i.e., motor jerks, which in turn improves overall stability during locomotion.
As stated in
Section 2.1, we require the approximation
to fulfill the underlying ODE at certain collocation sites
, see (
3), while simultaneously satisfying the boundary conditions as specified in (
4). Note that we use the index
k instead of
i to highlight that our new task consists in collocating the ODE at the interior knots, i.e.,
rather than the previously investigated interpolation at all knots, i.e.,
. Furthermore, it should be pointed out that although (
3) holds, this does not imply that
,
, or
. In other words,
will not coincide with the real solution
at the collocation sites
. However, it will behave similarly at these spots (meaning that they will satisfy the same Equation (
1)), which is illustrated in
Figure 4.
As first step, we introduce the auxiliary variables
,
, and
which are defined as
where
,
,
and
,
,
are the corresponding counterparts for the case of cubic and quintic splines, respectively. While
represents the (yet unknown) collocation points
,
contains their corresponding first (and second) time derivatives, which can be seen as “internal” unknowns, as they will be implicitly defined through an embedded spline interpolation. Lastly,
depicts the BCs, where we lack
and
in the case of cubic splines as has been previously explained.
From (
17), (
30), and (
43) we observe that the spline segments
are linear with respect to
,
, and
, i.e.,
holds. The gradients are fully defined by the spline partitioning
, which is assumed to be known. Thus the construction of the spline
is equivalent to the search for a corresponding
and
. Note that to obtain (
44), we used (
17) and (
30), which in turn were derived from fulfilling the interpolation condition together with enforcing continuity of the second time derivative (cubic spline), or first and second time derivative (quintic spline) at the interior knots. In order to accomplish full
and
continuity, we further make use of
from (
19) and (
33), which represents continuity of the first time derivative (cubic spline) or third and fourth time derivative (quintic spline), respectively. In particular, we observe from (
23), (
24), (
37), and (
38), that
is linear with respect to
and
, thus
holds, where the gradients again depend only on the known partitioning
. We further observe that, according to the definitions (
21), (
35) and (
43), we can write the mapping
with
being the identity matrix of appropriate size. Since
and
are constant, by “constant” we mean in this context that an expression does not depend on the yet unknown
or
, and assumed to be non-singular, it is clear from (
45) that not only
, but also
and thus
are linear with respect to
and
. Hence one can write
Note that
and
only depend on the known
, which allows us to safely differentiate (
45) with respect to
and
to obtain
which we can use to compute the yet unknown gradients in (
47). Note that this can be done very efficiently due to the (block-)tridiagonal form of
as already discussed in
Section 2.3. Since
is diagonal, this property also holds for the product
. However, for best numerical stability, one should solve for the gradients of
first and use the mapping (
46) to obtain the gradients of
afterwards, which is of negligible cost since
, and thus also
, is diagonal. The right-hand sides necessary to solve (
48) only depend on
and are derived in
Appendix B. Lastly, we insert
from (
47) into (
44) and obtain
or equivalently
with the known spline gradients
We can obtain the corresponding expressions for the first and second time derivatives by following the exact same scheme. In particular, we get
with
Although computing the spline gradients can be done very efficiently, their mathematical representation is rather lengthy. A formulation of the gradients, which is ready for implementation, is given in
Appendix B.
Lastly, we fulfill the dynamics of the ODE by inserting (
50) and (
52) into (
3), which leads to
for
and
with
. This can be formulated as LSE
with
where the
k-th row of
and
corresponds to the collocation site
and is given by
Note that we choose a partitioning of the spline such that the collocation sites coincide with the starting (“left”) knot of each segment, i.e.,
, see
Figure 4. This allows us to skip the computation of certain spline gradients. Since the underlying ODE is of order two, only gradients of the last three coefficients, i.e.,
,
,
(cubic) or
,
,
(quintic), have to be computed, for details see
Appendix B. This simplifies the implementation and improves the overall performance. Solving (
56) for
represents the key operation (i.e., bottleneck for large
n) of the proposed collocation method, since
is in general dense while all other operations are either simple explicit expressions or linear systems in (block-)tridiagonal form, which can be solved efficiently. This justifies our strategy to minimize the count of collocation points (and thus the dimension of
) and instead force high order continuity.
As soon as
has been obtained, we can compute
directly from (
47), since the gradients of
with respect to
and
are already available as by-products of computing
, see (
48). From
,
and
the segment coefficients can finally be computed using (
17) or (
30).
2.8. Satisfying First Order Boundary Conditions for Cubic Splines
The presented method for cubic spline collocation respects
,
,
, and
as BCs. However, our task was to fulfill all BCs given in (
4), which seems at first to be only possible with quintic spline collocation. Also satisfying the first-order BCs, i.e.,
and
, can be achieved with moderate effort. For that purpose we insert two auxiliary knots, the so-called virtual control points
and
, which give us the necessary degrees of freedom to introduce additional constraints. The term “virtual” highlights that these points are not used as collocation sites. Both,
and
, have to lie within the specified start- and endtime and must not coincide with the collocation sites. This way the spline remains properly partitioned. For simplicity, we place the virtual control points at the centers of the (originally) first and last segments, i.e.,
Obviously, inserting two knots leads to a different segmentation of the spline, i.e.,
; however, the boundaries and collocation sites remain unchanged. If we adapt the indexing such that
and
, all findings derived so far are also valid for this case. The only difference is that we do not force
to fulfill the underlying ODE at
and
anymore. Instead, we satisfy the BCs
and
by replacing the first and last row of
and
with
In this way the resulting cubic spline fulfills all BCs given in (
4), satisfies the underlying ODE at the specified collocation sites, and is
continuous at the interior spline knots (which include
and
).