Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Continuous-Time Transcription and Collocation

The preceding chapter formulated trajectory optimization for systems that are already discrete in time. Physical models are often given instead by ordinary differential equations, so their states and controls are functions of time. A finite-dimensional optimizer cannot choose an entire function directly. Continuous-time trajectory optimization therefore begins by replacing those functions and their differential equations with finitely many variables and algebraic constraints.

How can that replacement preserve enough of the differential equation to make the resulting trajectory meaningful between the stored time points?

This replacement is called transcription, and the resulting finite optimization problem is a nonlinear program (NLP).

Direct collocation performs this replacement by storing state and control values at selected times and interpolating between them with low-degree polynomials. The differential equation is enforced at selected points on each time interval. This chapter develops that construction from one simple example through Euler, trapezoidal, and Hermite--Simpson transcriptions, then applies it to the motion of an overhead crane.

A One-Interval Example

What is the smallest transcription that turns one differential equation on one interval into a finite algebraic defect?

Consider a point that must move from x(0)=0x(0)=0 to x(1)=1x(1)=1. Its velocity is the control, so

x˙(t)=u(t),\dot x(t)=u(t),

and the objective penalizes squared control effort,

minimizex(),u()01u(t)2dt.\underset{x(\cdot),u(\cdot)}{\operatorname{minimize}} \quad \int_0^1 u(t)^2\,dt.

Both xx and uu are unknown functions. As a first finite approximation, retain only their endpoint values X0,X1,U0,U1X_0,X_1,U_0,U_1 and let the control vary linearly between U0U_0 and U1U_1. Integrating x˙=u\dot x=u means that the change in state equals the area under this control. The shaded region below is a unit-width trapezoid. Its area is the average of its two endpoint heights, U0U_0 and U1U_1, multiplied by the width.

Figure 1:Drag the endpoint controls or play the accumulation from left to right. The shaded rectangle and triangle sum to the state change implied by x˙=u\dot x=u. Setting the interval width to h=1h=1 recovers the chapter’s defect X1X0=(U0+U1)/2X_1-X_0=(U_0+U_1)/2.

Loading...
A linear control between two endpoint values forms a trapezoid whose rectangle and triangle areas sum to the state change.

Figure 2:A linear control connects U0U_0 and U1U_1 across an interval of width hh. The rectangle hU0hU_0 and triangle h(U1U0)/2h(U_1-U_0)/2 sum to h(U0+U1)/2h(U_0+U_1)/2. Because x˙=u\dot x=u, this area equals X1X0X_1-X_0.

The resulting equality becomes the defect constraint

X1X012(U0+U1)=0.X_1-X_0-\frac{1}{2}(U_0+U_1)=0.

This algebraic equation is called a defect constraint: its left-hand side measures the mismatch between the endpoint change and the change predicted by the approximated dynamics. Applying the same endpoint approximation to the running cost gives the finite nonlinear program

minimizeX0,X1,U0,U112(U02+U12)subject toX0=0,X1=1,X1X012(U0+U1)=0.\begin{aligned} \underset{X_0,X_1,U_0,U_1}{\operatorname{minimize}} \quad&\frac{1}{2}(U_0^2+U_1^2)\\ \text{subject to}\quad&X_0=0,\qquad X_1=1,\\ &X_1-X_0-\frac{1}{2}(U_0+U_1)=0. \end{aligned}

The boundary conditions reduce the defect to U0+U1=2U_0+U_1=2. Substituting U1=2U0U_1=2-U_0 into the objective gives

12(U02+(2U0)2)=(U01)2+1.\frac12\left(U_0^2+(2-U_0)^2\right) =(U_0-1)^2+1.

The squared term is minimized at U0=1U_0=1, which also gives U1=1U_1=1. The resulting interpolation is the exact solution u(t)=1u(t)=1 and x(t)=tx(t)=t.

This example already contains the main ingredients of direct collocation. The function values became optimization variables, integration became a weighted sum, and the differential equation became an equality constraint. The remaining sections construct these operations systematically for nonlinear vector dynamics and higher-degree polynomials.

From a Continuous Problem to a Finite NLP

How do nodal state and control values, quadrature weights, and defect equations assemble into one nonlinear program?

A continuous-time optimal-control problem can be written in Bolza form:

minimizex(),u(),tfΦ(x(tf),tf)+t0tfL(x(t),u(t),t)dtsubject tox˙(t)=f(x(t),u(t),t),r(x(t0),x(tf),tf)=0,g(x(t),u(t),t)0.\begin{aligned} \underset{x(\cdot),u(\cdot),t_f}{\operatorname{minimize}} \quad& \Phi(x(t_f),t_f) +\int_{t_0}^{t_f} L(x(t),u(t),t)\,dt\\ \text{subject to}\quad& \dot x(t)=f(x(t),u(t),t),\\ &r(x(t_0),x(t_f),t_f)=0,\\ &g(x(t),u(t),t)\leq 0. \end{aligned}

Here x(t)Rnxx(t)\in\mathbb R^{n_x} is the state, u(t)Rnuu(t)\in\mathbb R^{n_u} is the control, Φ\Phi is a terminal cost, and LL is a running cost. The equality r=0r=0 imposes endpoint conditions, while g0g\leq 0 represents constraints that must hold along the path. Setting L=0L=0 gives the Mayer special case, while setting Φ=0\Phi=0 gives the Lagrange special case. All three forms use the same transcription machinery.

Two transcription strategies differ in which values become decision variables and in how they impose the differential equation:

StrategyFinite decision variablesTreatment of the ODE
ShootingControl parameters and, in multiple shooting, selected boundary statesA time integrator advances the state sequentially inside each shooting interval
Direct collocationState and control values at selected nodesAlgebraic defect equations enforce the ODE simultaneously

Shooting and collocation are therefore distinct transcription strategies. A collocation method may reproduce a familiar integration formula, but it exposes the state values to the NLP rather than hiding every state update inside a simulation. The term direct indicates that the continuous problem is converted directly into an optimization problem, without first deriving necessary conditions such as the Pontryagin equations.

Let

t0<t1<<tN=tf,hk=tk+1tk,t_0<t_1<\cdots<t_N=t_f,\qquad h_k=t_{k+1}-t_k,

be a mesh. On each interval, the normalized coordinate is τ=(ttk)/hk\tau=(t-t_k)/h_k, or equivalently

t=tk+hkτ,0τ1.t=t_k+h_k\tau,\qquad 0\leq\tau\leq1.

Every physical interval is thereby mapped to the same reference interval [0,1][0,1]. Differentiation and integration formulas can be constructed once on this reference interval; the length hkh_k then supplies the physical-time scaling. If the final time is also optimized, the interval lengths become variables or fixed fractions of the variable horizon; the reference-interval operators remain unchanged.

One Polynomial, Two Coordinate Systems

Should a polynomial trajectory be stored by its coefficients or by its values at support nodes, and how are the two descriptions related?

The opening example represented a line by its two endpoint values. Higher-order collocation uses the same idea with more values. The polynomials of degree at most rr form the space

Pr={p:degpr}\mathcal P_r=\{p:\deg p\leq r\}

This space, denoted by Pr\mathcal P_r, has dimension r+1r+1. Choosing a basis {ϕ0,,ϕr}\{\phi_0,\ldots,\phi_r\} gives coefficient coordinates

p(τ)=j=0rajϕj(τ).p(\tau)=\sum_{j=0}^{r}a_j\phi_j(\tau).

The monomial choice ϕj(τ)=τj\phi_j(\tau)=\tau^j is familiar, but it is only a coordinate system. The polynomial is the function pp, not its particular list of coefficients.

The same polynomial can instead be identified by its values. Choose r+1r+1 distinct points

σ0,,σr[0,1],\sigma_0,\ldots,\sigma_r\in[0,1],

and record

yi=p(σi).y_i=p(\sigma_i).

These points are called support nodes because the stored values at the nodes determine the polynomial between them. For degree one with support nodes 0 and 1, the construction is already familiar:

p(τ)=y0(1τ)+y1τ.p(\tau)=y_0(1-\tau)+y_1\tau.

The multiplier 1τ1-\tau equals one at the first node and zero at the second; τ\tau does the reverse. For any set of distinct support nodes, the Lagrange cardinal function j\ell_j is the degree-rr polynomial with this same selection property: it equals one at node jj and zero at every other support node. Its formula is

j(τ)=m=0mjrτσmσjσm.\ell_j(\tau) =\prod_{\substack{m=0\\m\neq j}}^r \frac{\tau-\sigma_m}{\sigma_j-\sigma_m}.

At a support node σi\sigma_i, one factor in the numerator is zero unless i=ji=j. When i=ji=j, every numerator equals its corresponding denominator. Consequently,

j(σi)=δij,\ell_j(\sigma_i)=\delta_{ij},

where the Kronecker delta δij\delta_{ij} equals one when i=ji=j and zero otherwise. The cardinal functions therefore reconstruct the polynomial from its nodal values:

p(τ)=j=0ryjj(τ),yj=p(σj).\boxed{ p(\tau)=\sum_{j=0}^{r}y_j\ell_j(\tau), \qquad y_j=p(\sigma_j). }

The uniqueness of this reconstruction follows from a basic root-counting argument. If two polynomials in Pr\mathcal P_r have the same r+1r+1 nodal values, their difference has r+1r+1 distinct roots. A nonzero polynomial of degree at most rr cannot have that many roots, so the two polynomials must be identical.

The coefficient and nodal descriptions are related by the evaluation matrix

Vij=ϕj(σi),y=Va.V_{ij}=\phi_j(\sigma_i), \qquad y=Va.

Distinct support nodes make VV invertible by the same root-counting argument. Thus aa and yy are two coordinate vectors for one polynomial, rather than two different approximations.

For example, take p(τ)=1+2ττ2p(\tau)=1+2\tau-\tau^2 and the nodes 0,12,10,\tfrac12,1. In monomial coordinates,

a=[121],V=[10011214111],a= \begin{bmatrix}1\\2\\-1\end{bmatrix}, \qquad V= \begin{bmatrix} 1&0&0\\ 1&\tfrac12&\tfrac14\\ 1&1&1 \end{bmatrix},

whereas the nodal coordinates are

y=Va=[1742].y=Va= \begin{bmatrix}1\\\tfrac74\\2\end{bmatrix}.

Both vectors describe exactly the same quadratic. Direct collocation uses coordinates like yy: state values and control values at meaningful points. It does not ask the NLP solver to choose monomial coefficients. Software may use coefficient calculations when it constructs fixed operators, but those calculations remain outside the NLP.

Figure 3:One quadratic, two coordinate systems. Evaluating the monomial coefficients aa at the support nodes gives y=Vay=Va. Direct collocation stores the nodal vector yy; the fixed operators DD and ww then return its nodal derivatives and exact integral without adding optimization variables.

<Figure size 740x370 with 2 Axes>

Polynomial space, basis, and nodes are different choices

The construction separates three decisions that are easy to conflate:

Changing the basis does not change the exact polynomial space, although it can change numerical conditioning. Changing the nodes changes the interpolation and the operators built from it. The higher-order node families introduced later can still use Lagrange nodal coordinates in the NLP; choosing those nodes does not require optimizing orthogonal-polynomial coefficients.

Polynomial Interpolation and Least-Squares Regression

When nodal values do not determine an exact interpolant, which projection recovers a polynomial that best matches the available samples?

Interpolation and polynomial regression both produce polynomials, but they answer different questions. Four exact values at four distinct nodes determine one cubic interpolant. Twenty noisy measurements do not generally lie on one cubic, so cubic regression instead chooses the coefficients that minimize the aggregate squared residual. Write AA for the matrix obtained by evaluating the chosen polynomial basis at the supplied input points. The two algebraic problems are then compared below.

Polynomial interpolationLeast-squares regression
InputExact value conditionsUsually noisy or overdetermined observations
Algebraic problemSatisfy Aa=yAa=y exactlyMinimize Aay22\lVert Aa-y\rVert_2^2
ResidualZero when the value conditions uniquely determine a polynomialGenerally nonzero
Typical purposeRepresent a function from exact nodal dataEstimate a trend or conditional mean

If AA is square and invertible, least squares happens to return the exact interpolant with zero residual. That special overlap does not erase the conceptual distinction.

Figure 4:The panels use the same six values. On the left they are treated as six exact conditions for a degree-five interpolant, so every residual is zero. On the right they are treated as six observations for a three-parameter quadratic regression, so the fit trades errors across observations.

<Figure size 700x245 with 2 Axes>

In direct collocation, the nodal states are unknown decision variables rather than observations. Each candidate vector of nodal values defines one interpolating polynomial exactly. The optimizer selects a candidate whose ODE residual vanishes at the collocation nodes. A numerical solver may temporarily work with a scalar measure of constraint violation, but the collocation conditions remain equality constraints rather than a statistical regression loss.

Fixed Operators from Nodal Values

Once the nodes and basis are fixed, can differentiation and integration be reduced to precomputed linear maps on nodal values?

The ODE and the running cost require more than values of an interpolating polynomial. The ODE uses derivatives, and the cost and state update use integrals. Since

p(τ)=j=0ryjj(τ),p(\tau)=\sum_{j=0}^r y_j\ell_j(\tau),

both operations are linear functions of the nodal vector yy. Differentiating and then evaluating at a node σi\sigma_i gives

p(σi)=j=0rj(σi)Dijyj,p'(\sigma_i)=\sum_{j=0}^r \underbrace{\ell_j'(\sigma_i)}_{D_{ij}}y_j,

while integration over the reference interval gives

01p(τ)dτ=j=0r(01j(τ)dτ)wjyj.\int_0^1p(\tau)\,d\tau =\sum_{j=0}^r \underbrace{\left(\int_0^1\ell_j(\tau)\,d\tau\right)}_{w_j}y_j.

The resulting differentiation matrix DD and integration weights ww depend only on the chosen nodes. They can be computed before the optimization begins.

For the three support nodes 0,12,10,\tfrac12,1, the cardinal functions are

0(τ)=(τ12)(τ1)(012)(01)=2τ23τ+1,m(τ)=τ(τ1)(120)(121)=4τ(1τ),1(τ)=τ(τ12)(10)(112)=2τ2τ.\begin{aligned} \ell_0(\tau) &=\frac{(\tau-\frac12)(\tau-1)}{(0-\frac12)(0-1)} =2\tau^2-3\tau+1,\\ \ell_m(\tau) &=\frac{\tau(\tau-1)}{(\frac12-0)(\frac12-1)} =4\tau(1-\tau),\\ \ell_1(\tau) &=\frac{\tau(\tau-\frac12)}{(1-0)(1-\frac12)} =2\tau^2-\tau. \end{aligned}

The factored forms come directly from the general product formula. Each function takes the value one at the node named by its subscript and zero at the other two nodes. Differentiating them at all three nodes gives

[p(0)p(12)p(1)]=[341101143]D[y0ymy1].\begin{bmatrix} p'(0)\\ p'(\tfrac12)\\ p'(1) \end{bmatrix} = \underbrace{ \begin{bmatrix} -3&4&-1\\ -1&0&1\\ 1&-4&3 \end{bmatrix}}_{D} \begin{bmatrix} y_0\\y_m\\y_1 \end{bmatrix}.

Thus a matrix-vector product turns the three stored values into the three nodal derivatives. Integrating the same cardinal functions gives

01p(τ)dτ=[164616]wT[y0ymy1].\int_0^1 p(\tau)\,d\tau = \underbrace{ \begin{bmatrix} \tfrac16&\tfrac46&\tfrac16 \end{bmatrix}}_{w^\mathsf T} \begin{bmatrix} y_0\\y_m\\y_1 \end{bmatrix}.

The row wTw^\mathsf T turns the same three values into the exact integral of their quadratic interpolant. The matrix DD differentiates every polynomial in P2\mathcal P_2 exactly, and ww integrates every polynomial in that space exactly. “Fixed operator” means that these arrays remain constant while the NLP changes the nodal values.

A transcription may use nodes for three distinct purposes:

Node roleWhat it does
Support nodeSupplies coordinates that define a polynomial
Collocation nodeSupplies a point where the ODE residual is constrained
Quadrature nodeSupplies a point used to approximate an integral

A method often reuses one set of points for two or three roles. That is a design choice, not a definition. For example, a state polynomial can be supported at one set of nodes and differentiated at different collocation nodes. The next section begins with slope values at collocation nodes and uses integration to recover state values.

From Nodal Slopes to Collocation Constraints

How do those fixed maps convert differential equations into algebraic constraints at the chosen collocation nodes?

The previous section constructed differentiation and integration operators for an arbitrary polynomial. To impose an ODE, first choose ss collocation nodes c1,,csc_1,\ldots,c_s on [0,1][0,1]. At each node, the differential equation prescribes the state slope. Denote that slope on interval kk by

Fk,j=f(Xk,j,Uk,j,tk+hkcj)F_{k,j} =f(X_{k,j},U_{k,j},t_k+h_kc_j)

for j=1,,sj=1,\ldots,s. Here Xk,jX_{k,j} and Uk,jU_{k,j} are the state and control at the node. Let j\ell_j be the Lagrange cardinal function associated with the collocation nodes c1,,csc_1,\ldots,c_s. The nodal slopes then define the derivative interpolant

x˙h(tk+hkτ)=j=1sFk,jj(τ).\dot x_h(t_k+h_k\tau) =\sum_{j=1}^{s}F_{k,j}\ell_j(\tau).

This polynomial agrees with the ODE slope Fk,jF_{k,j} at every collocation node. Integrating it from the left endpoint to node cic_i gives the state value there:

Xk,i=Xk+hkj=1sAijFk,j,Aij=0cij(τ)dτ.X_{k,i} =X_k+h_k\sum_{j=1}^{s}A_{ij}F_{k,j}, \qquad A_{ij}=\int_0^{c_i}\ell_j(\tau)\,d\tau.

The factor hkh_k appears because dt=hkdτdt=h_k\,d\tau. These equalities are called stage equations; a stage is an interval-local state and control evaluation used by the transcription. Integrating the same derivative polynomial across the full reference interval gives the right endpoint:

Xk+1=Xk+hkj=1sbjFk,j,bj=01j(τ)dτ.\boxed{ X_{k+1} =X_k+h_k\sum_{j=1}^{s}b_jF_{k,j}, \qquad b_j=\int_0^1\ell_j(\tau)\,d\tau. }

The coefficients bjb_j are the areas under the cardinal functions. The endpoint equation is both an integration formula and a defect constraint: its residual compares the stored endpoint Xk+1X_{k+1} with the endpoint predicted from XkX_k and the nodal slopes. If Xk+1X_{k+1} is shared with the next interval, it also enforces state continuity. Otherwise an explicit equality must connect the two interval representations. Merely including an endpoint among the collocation nodes does not create continuity by itself.

A quadrature rule approximates an integral by a weighted sum of function values. Reusing the collocation nodes and their full-interval weights gives the interval running-cost approximation

Jkhkj=1sbjL(Xk,j,Uk,j,tk+hkcj).J_k \approx h_k\sum_{j=1}^{s} b_jL(X_{k,j},U_{k,j},t_k+h_kc_j).

Thus the running cost is evaluated at the same local stages as the vector field. Different quadrature nodes could be used instead; the state and control polynomials would simply be evaluated there.

After all intervals are assembled, collect the nodal states and controls into the decision vector

z=(X0,{Xk,j,Uk,j}k,j,XN).z=\left(X_0,\{X_{k,j},U_{k,j}\}_{k,j},X_N\right)\,.

The resulting finite optimization problem has the form

minimizezΦ(XN,tf)+k=0N1Jksubject tostage equations,endpoint defects and continuity,boundary, path, and bound constraints.\begin{aligned} \underset{z}{\operatorname{minimize}}\quad& \Phi(X_N,t_f)+\sum_{k=0}^{N-1}J_k\\ \text{subject to}\quad& \text{stage equations},\\ &\text{endpoint defects and continuity},\\ &\text{boundary, path, and bound constraints}. \end{aligned}

The original path and bound constraints apply at every continuous time. The finite NLP can impose them only at selected points, usually its support or collocation nodes. Feasibility at those nodes does not rule out a violation between them, so a continuous replay or dense residual check must follow the optimization.

Every interval constraint touches only local stage variables and neighboring endpoint states. Consequently, most derivatives of the constraints with respect to the decision variables are zero. With variables ordered by time, the nonzero blocks lie near the diagonal of the Jacobian, the matrix of constraint derivatives. NLP solvers can exploit this sparse, block-banded pattern. The arrays AA and bb are numerical constants computed before optimization.

Equivalent differentiation form

The slope-value construction starts from Fk,jF_{k,j} and integrates. Many implementations take the equivalent route of starting from nodal state values and differentiating. Let σ0,,σd\sigma_0,\ldots,\sigma_d be support nodes for a state polynomial, and denote their cardinal functions by λr\lambda_r to avoid confusing them with the running cost LL:

xh(tk+hkτ)=r=0dXk,rλr(τ).x_h(t_k+h_k\tau) =\sum_{r=0}^{d}X_{k,r}\lambda_r(\tau).

At a collocation node cic_i, the state is a weighted sum of its support values, and its derivative with respect to τ\tau is another weighted sum. The fixed arrays containing these weights are

Eir=λr(ci),Dir=λr(ci).E_{ir}=\lambda_r(c_i), \qquad D_{ir}=\lambda_r'(c_i).

Thus EE evaluates the state polynomial and DD differentiates it. Because d/dt=(1/hk)d/dτd/dt=(1/h_k)d/d\tau, enforcing the ODE at node cic_i gives

r=0dDirXk,r=hkf(r=0dEirXk,r,Uk,i,tk+hkci).\boxed{ \sum_{r=0}^{d}D_{ir}X_{k,r} =h_k f\left( \sum_{r=0}^{d}E_{ir}X_{k,r}, U_{k,i}, t_k+h_kc_i \right). }

The left side is the derivative with respect to normalized time, and the factor hkh_k on the right converts the physical-time derivative accordingly. In matrix shorthand, all nodal constraints are DXk=hkFkDX_k=h_kF_k. The right endpoint is evaluated with another fixed row:

Xk+1=r=0dλr(1)Xk,r.X_{k+1}=\sum_{r=0}^{d}\lambda_r(1)X_{k,r}.

When support and collocation nodes coincide, the cardinal property makes EE select the corresponding stored state directly. The slope-value and state-value forms describe the same polynomial construction; one integrates nodal slopes, while the other differentiates nodal states.

This implementation pattern is standard in direct collocation Kelly, 2017Andersson et al., 2019. The official CasADi direct-collocation example constructs fixed differentiation, endpoint, and quadrature arrays from Lagrange polynomials, while the NLP variables remain state and control values.

Both algebraic forms follow the same sequence. Nodal values define a local polynomial, fixed arrays differentiate or integrate it, and equality constraints match the resulting slopes and endpoints to the dynamics. The choice between slope values and state values changes the implementation, not the represented collocation method.

Low-Order Transcriptions

Which familiar integration rules appear when the polynomial and node sets are reduced to their lowest-order choices?

The general construction becomes concrete when only one or two slope values are retained on each interval. These cases recover familiar integration formulas, but the formulas now appear as constraints inside an NLP.

One slope value: explicit and implicit Euler

With the single left collocation node c1=0c_1=0, the only cardinal function is 1(τ)=1\ell_1(\tau)=1. The derivative interpolant is therefore the constant slope f(Xk,Uk,tk)f(X_k,U_k,t_k). Its integration weight is b1=1b_1=1, so the endpoint defect is

Xk+1Xkhkf(Xk,Uk,tk)=0.X_{k+1}-X_k-h_k f(X_k,U_k,t_k)=0.

This is explicit Euler. The stored right endpoint is constrained to equal the result of advancing from the left endpoint with its local slope. Because the derivative approximation is constant, its integral is a linear state approximation on the interval.

With the single right collocation node c1=1c_1=1, the constant slope is evaluated at the unknown right endpoint. The same integration weight gives

Xk+1Xkhkf(Xk+1,Uk+1,tk+1)=0,X_{k+1}-X_k-h_k f(X_{k+1},U_{k+1},t_{k+1})=0,

This is implicit Euler. In sequential simulation, the occurrence of Xk+1X_{k+1} inside ff requires a nonlinear solve at each step. In direct transcription, Xk+1X_{k+1} is already an optimization variable, so the relation is imposed as one of the simultaneous equality constraints.

Endpoint slope values: trapezoidal transcription

Choose the endpoint collocation nodes c0=0c_0=0 and c1=1c_1=1, and abbreviate the two ODE slopes by

Fk=f(Xk,Uk,tk),Fk+1=f(Xk+1,Uk+1,tk+1).F_k=f(X_k,U_k,t_k),\qquad F_{k+1}=f(X_{k+1},U_{k+1},t_{k+1}).

The cardinal functions used to interpolate these derivative values are

0(τ)=1τ,1(τ)=τ.\ell_0(\tau)=1-\tau,\qquad \ell_1(\tau)=\tau.

The derivative interpolant is the line joining the two slopes:

x˙h(tk+hkτ)=(1τ)Fk+τFk+1.\dot x_h(t_k+h_k\tau) =(1-\tau)F_k+\tau F_{k+1}.

It equals FkF_k at τ=0\tau=0 and Fk+1F_{k+1} at τ=1\tau=1. Integrating from the known left state gives the continuous state approximation

xh(tk+hkτ)=Xk+hk[(ττ22)Fk+τ22Fk+1].x_h(t_k+h_k\tau) =X_k+h_k\left[ \left(\tau-\frac{\tau^2}{2}\right)F_k +\frac{\tau^2}{2}F_{k+1} \right].

The state approximation is quadratic, even though an implementation may store only its endpoint states and slopes. Evaluating this polynomial at τ=1\tau=1 and equating it to the stored endpoint produces

Xk+1Xkhk2[f(Xk,Uk,tk)+f(Xk+1,Uk+1,tk+1)]=0.\boxed{ X_{k+1}-X_k -\frac{h_k}{2} \left[ f(X_k,U_k,t_k) +f(X_{k+1},U_{k+1},t_{k+1}) \right] =0. }

The coefficients 1/21/2 are the integrals of the two linear cardinal functions. This is the trapezoidal defect derived in the opening example. Applying the same endpoint weights to the running cost gives

Jkhk2[L(Xk,Uk,tk)+L(Xk+1,Uk+1,tk+1)].J_k \approx \frac{h_k}{2} \left[ L(X_k,U_k,t_k) +L(X_{k+1},U_{k+1},t_{k+1}) \right].

The two endpoint costs are averaged and multiplied by the interval length. The degree bookkeeping follows directly from integration: a linear derivative interpolant produces a quadratic state interpolant. This distinction is emphasized in the direct-collocation derivation of Kelly (2017).

Hermite--Simpson Transcription

Can midpoint state and slope information raise the transcription order without requiring a high-degree polynomial over the whole horizon?

Hermite--Simpson extends the trapezoidal construction by adding the midpoint state Xk+12X_{k+\frac12}, control Uk+12U_{k+\frac12}, and ODE slope

Fk+12=f(Xk+12,Uk+12,tk+hk2).F_{k+\frac12}=f\left( X_{k+\frac12},U_{k+\frac12},t_k+\frac{h_k}{2} \right).

The derivative is now specified at 0,12,10,\tfrac12,1, so its interpolant is the quadratic built from the following three cardinal functions:

0(τ)=2τ23τ+1,m(τ)=4τ(1τ),1(τ)=2τ2τ.\ell_0(\tau)=2\tau^2-3\tau+1,\qquad \ell_m(\tau)=4\tau(1-\tau),\qquad \ell_1(\tau)=2\tau^2-\tau.

Integrating each function over the full reference interval gives

b0=16,bm=46,b1=16.b_0=\frac16,\qquad b_m=\frac46,\qquad b_1=\frac16.

These are the familiar Simpson quadrature weights. Substituting them into the general endpoint equation produces the Simpson defect:

Xk+1Xkhk6(Fk+4Fk+12+Fk+1)=0.\boxed{ X_{k+1}-X_k -\frac{h_k}{6} \left(F_k+4F_{k+\frac12}+F_{k+1}\right) =0. }

The midpoint state must also lie on the cubic obtained by integrating the quadratic derivative. Integration only to τ=12\tau=\tfrac12 gives

Xk+12=Xk+hk24(5Fk+8Fk+12Fk+1).X_{k+\frac12} =X_k+\frac{h_k}{24} \left(5F_k+8F_{k+\frac12}-F_{k+1}\right).

This form still contains the midpoint slope. The endpoint defect gives

4Fk+12=6hk(Xk+1Xk)FkFk+1.4F_{k+\frac12} =\frac{6}{h_k}(X_{k+1}-X_k)-F_k-F_{k+1}.

Substituting this expression into the midpoint equation and collecting the endpoint states and slopes yields

Xk+12=Xk+Xk+12+hk8(FkFk+1).\boxed{ X_{k+\frac12} =\frac{X_k+X_{k+1}}{2} +\frac{h_k}{8}\left(F_k-F_{k+1}\right). }

The midpoint state relation and the definition of Fk+12F_{k+\frac12} together enforce the ODE at the midpoint. A quadratic derivative interpolant integrates to a cubic state interpolant, so Hermite--Simpson is not based on a quadratic state approximation. The name reflects its two ingredients: the state is a cubic Hermite interpolant determined by state and slope information, and its endpoint defect uses Simpson weights.

Optional orientation: Gauss, Radau, and Lobatto nodes

Higher-order schemes often place their nodes at roots of Legendre polynomials or at roots of closely related equations that include prescribed endpoints. These placements produce accurate quadrature rules for a given number of function evaluations. Three names indicate which endpoints are included:

FamilyEndpoints included
GaussNeither endpoint
RadauOne endpoint
LobattoBoth endpoints

Gauss nodes exclude both endpoints, Radau nodes include one, and Lobatto nodes include both. This vocabulary describes node placement, not the coordinate basis used by the NLP. It also does not determine continuity. Adjacent state polynomials are continuous only when they share an endpoint state or are connected by an equality constraint. Endpoint inclusion can make that linkage convenient, but it does not automatically provide state continuity or slope continuity.

The detailed comparison of node families, convergence rates, and adaptive degree selection is deferred. Each family enters the present construction in the same way: its nodes define Lagrange functions, which in turn define fixed differentiation and quadrature operators.

Worked Example: Moving an Overhead Crane While Limiting Residual Sway

How do the nodal variables and trapezoidal defects behave in a constrained motion problem whose terminal state must suppress residual oscillation?

An overhead crane moves a trolley while a payload hangs from a cable. A precomputed trolley command can complete the move and still leave the payload swinging. The comparison uses three precomputed acceleration commands. The unshaped baseline moves the trolley without accounting for the payload. A zero-vibration input shaper modifies that baseline to cancel the nominal oscillation. Direct collocation instead chooses a command using the nonlinear payload dynamics and the motion constraints. All three commands are open loop: they are fixed before the move and do not respond to measurements during execution.

The state is x=(p,v,θ,ω)\mathbf{x}=(p,v,\theta,\omega), where pp and vv are trolley position and velocity, and θ\theta and ω\omega are payload angle and angular velocity. The commanded trolley acceleration aa enters the nonlinear dynamics as

p˙=v,v˙=a,θ˙=ω,ω˙=gsinθacosθcω.\dot p=v,\qquad \dot v=a,\qquad \dot\theta=\omega,\qquad \dot\omega=-\frac{g}{\ell}\sin\theta-\frac{a}{\ell}\cos\theta-c\omega.

The first two equations describe trolley motion, while the last two describe a damped pendulum driven at its suspension point. Positive trolley acceleration makes the load lag behind, which accounts for the minus sign multiplying aa. The model treats the cable as a rigid, massless link and assumes that the trolley acceleration can be commanded directly.

The trolley and payload start at rest. The goal is to move the trolley 4 m, stop it, and leave the payload hanging vertically without swinging. The nominal cable length is =1.20\ell=1.20 m. Every command is limited to a1.60|a|\leq 1.60 m/s2^2, and every command is replayed on the same nonlinear continuous-time plant with a 0.02 s sampling interval. A second replay increases the cable length by 10%10\% without redesigning any command. This second plant tests sensitivity to a simple model mismatch.

Two open-loop baselines

The unshaped baseline uses a symmetric trapezoidal velocity profile, produced by constant acceleration, cruising, and constant deceleration. Its acceleration and deceleration phases excite the payload oscillation because their timing ignores the pendulum period.

An input shaper filters a command into weighted, delayed copies. The delays are chosen so that vibrations excited by the copies cancel one another. For a zero-vibration (ZV) shaper, begin by approximating sinθθ\sin\theta\approx\theta and cosθ1\cos\theta\approx1 near the hanging equilibrium. The payload equation then becomes

θ¨+2ζωnθ˙+ωn2θ=a,ωn=g,ζ=c2ωn.\ddot\theta+2\zeta\omega_n\dot\theta+\omega_n^2\theta=-\frac{a}{\ell}, \qquad \omega_n=\sqrt{\frac{g}{\ell}}, \qquad \zeta=\frac{c}{2\omega_n}.

Here ωn\omega_n is the undamped natural frequency and ζ\zeta is the damping ratio. For the nominal parameters, ωn=2.86\omega_n=2.86 rad/s and ζ=0.0061\zeta=0.0061. The shaper splits the baseline command a0a_0 into two copies separated by half of the damped oscillation period:

aZV(t)=A1a0(t)+A2a0(tTd),Td=πωn1ζ2.a_{\mathrm{ZV}}(t)=A_1a_0(t)+A_2a_0(t-T_d), \qquad T_d=\frac{\pi}{\omega_n\sqrt{1-\zeta^2}}\,.

The delay makes the vibration caused by the second copy oppose the vibration remaining from the first. Accounting for decay over the delay gives A1=1/(1+K)A_1=1/(1+K), A2=K/(1+K)A_2=K/(1+K), and K=exp[ζπ/1ζ2]K=\exp[-\zeta\pi/\sqrt{1-\zeta^2}]. Here Td=1.10T_d=1.10 s and the two weights are approximately 0.505 and 0.495.

Nodal decision variables and trapezoidal defects

The third command is found by solving the trajectory-optimization problem. On N=28N=28 intervals with step hh, the NLP decision vector contains the state

Xk=(pk,vk,θk,ωk)X_k=(p_k,v_k,\theta_k,\omega_k)

and acceleration aka_k at every mesh node. These are polynomial values, not monomial coefficients. Each interval contributes the trapezoidal defect

Xk+1Xkh2[f(Xk,ak)+f(Xk+1,ak+1)]=0.X_{k+1}-X_k -\frac{h}{2}\left[ f(X_k,a_k)+f(X_{k+1},a_{k+1}) \right]=0.

The objective trades payload motion against acceleration magnitude and rapid changes in acceleration. The first two terms are collected in the nodal quantity

qk=6θk2+0.15ωk2+0.035ak2.q_k=6\theta_k^2+0.15\omega_k^2+0.035a_k^2.

The coefficient on θk2\theta_k^2 penalizes sway most strongly, while the smaller coefficients penalize angular velocity and acceleration. The smooth state-and-control cost uses trapezoidal endpoint weights. The final term penalizes acceleration slew, the rate at which the acceleration command changes. Because the control is piecewise linear, this rate is constant on each interval, giving

J=h(12q0+k=1N1qk+12qN)+0.002hk=0N1(ak+1akh)2.J =h\left(\frac12q_0+\sum_{k=1}^{N-1}q_k+\frac12q_N\right) +0.002h\sum_{k=0}^{N-1} \left(\frac{a_{k+1}-a_k}{h}\right)^2.

The boundary conditions impose X0=(0,0,0,0)X_0=(0,0,0,0) and XN=(4,0,0,0)X_N=(4,0,0,0), so both the trolley and payload finish at rest. The nodal bounds impose ak1.60|a_k|\leq1.60 m/s2^2, vk1.50|v_k|\leq1.50 m/s, and θk15|\theta_k|\leq15^\circ. Because these bounds are imposed only at nodes, a dense replay remains necessary to check the path between nodes.

The ZV shaper is tailored to one frequency, so it should leave the least residual sway when the cable length matches its design model. The collocation solution uses the full nonlinear nominal model and handles all constraints at once, but it is also open loop and has no explicit robustness guarantee. The cable-length test therefore measures sensitivity to one model mismatch; it does not establish that either method is robust in general.

Figure 5:All three commands move the trolley through the same four-metre task on the nonlinear plant. The unshaped command leaves a large oscillation. The ZV shaper nearly cancels the nominal mode, while direct collocation reaches the terminal state with a smoother, lower-effort command. The lower panel replays the unchanged commands after increasing cable length by 10 percent. Hatched bars denote the mismatched plant.

<Figure size 825x750 with 3 Axes>

The ZV shaper produces the smallest nominal residual sway because the simulated plant closely matches the single oscillatory mode used to design it. The collocation command uses less squared acceleration than either baseline and keeps residual sway below one degree in both replays. The unshaped command completes the trolley move but leaves several degrees of oscillation. Cable-length mismatch increases the ZV residual substantially, while the collocation command degrades more gradually for this particular perturbation.

The table reports continuous-plant measurements rather than node values from the nonlinear program. Residual sway is the largest absolute angle after the common command horizon.

Loading...

The animation uses the same high-accuracy nonlinear validation trajectories as the figure. It does not replay the collocation polynomial itself.

Figure 6:Continuous nonlinear replay of the unshaped, zero-vibration-shaped, and direct-collocation commands. All panels use the same spatial and temporal scales.

Loading...
Inspect the direct-transcription implementation
crane_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def solve_direct_collocation(
    params: CraneParameters | None = None,
    *,
    intervals: int = 28,
    horizon: float | None = None,
    max_iterations: int = 350,
) -> CollocationSolution:
    """Solve a constrained nonlinear point-to-point transcription.

    States and accelerations at every node are decision variables.  Trapezoidal
    defects enforce the nonlinear dynamics, while boundary and path constraints
    are imposed directly on the node values.
    """

    params = params or CraneParameters()
    params.validate()
    if intervals < 8:
        raise ValueError("intervals must be at least eight")

    move = TrapezoidalMove(
        params.target_position,
        params.profile_acceleration,
        params.profile_max_speed,
    )
    shaper = ZeroVibrationShaper.from_parameters(params)
    if horizon is None:
        horizon = move.duration + shaper.delay
    if horizon <= 0.0:
        raise ValueError("horizon must be positive")

    node_time = np.linspace(0.0, horizon, intervals + 1)
    step = horizon / intervals
    state_guess, acceleration_guess = _initial_guess(params, node_time, move, shaper)
    initial = _pack(state_guess, acceleration_guess)

    initial_state = np.zeros(4)
    terminal_state = np.array([params.target_position, 0.0, 0.0, 0.0])

    def objective(vector: np.ndarray) -> float:
        state, acceleration = _unpack(vector, intervals)
        return _transcription_objective(state, acceleration, step)

    def equality(vector: np.ndarray) -> np.ndarray:
        state, acceleration = _unpack(vector, intervals)
        defects = np.empty((intervals, 4))
        for k in range(intervals):
            left = crane_dynamics(state[k], acceleration[k], params)
            right = crane_dynamics(state[k + 1], acceleration[k + 1], params)
            defects[k] = state[k + 1] - state[k] - 0.5 * step * (left + right)
        return np.concatenate(
            [state[0] - initial_state, defects.ravel(), state[-1] - terminal_state]
        )

    nodes = intervals + 1
    lower_state = np.tile(
        np.array([-0.25, -params.velocity_limit, -params.sway_limit, -2.0]),
        (nodes, 1),
    )
    upper_state = np.tile(
        np.array(
            [params.target_position + 0.25, params.velocity_limit, params.sway_limit, 2.0]
        ),
        (nodes, 1),
    )
    lower = _pack(lower_state, np.full(nodes, -params.acceleration_limit))
    upper = _pack(upper_state, np.full(nodes, params.acceleration_limit))

    result = minimize(
        objective,
        initial,
        method="SLSQP",
        bounds=list(zip(lower, upper)),
        constraints={"type": "eq", "fun": equality},
        options={"maxiter": max_iterations, "ftol": 2e-10, "disp": False},
    )
    state, acceleration = _unpack(result.x, intervals)
    max_defect = float(np.max(np.abs(equality(result.x))))
    success = bool(result.success and max_defect < 2e-5)
    if not success:
        raise RuntimeError(
            "crane direct collocation failed: "
            f"{result.message}; maximum equality residual {max_defect:.3e}"
        )
    return CollocationSolution(
        time=node_time,
        state=state,
        acceleration=acceleration,
        objective=float(result.fun),
        max_defect=max_defect,
        success=success,
        message=str(result.message),
    )

Download the complete crane experiment

The optimization checks algebraic defects, bounds, and endpoint conditions at the nodes. The separate nonlinear replay checks what happens between those nodes. A small nodal defect is evidence that the discrete NLP was solved accurately; it is not, by itself, evidence that the mesh resolves the continuous dynamics.

The comparison does not establish that collocation always outperforms input shaping. The ZV calculation is inexpensive and can nearly cancel residual vibration when a lightly damped mode is accurately known. Direct collocation becomes useful when several state and actuator constraints must be handled together. All three commands remain open loop here. Feedback or receding-horizon replanning would be needed to react to unmeasured disturbances during the move.

Exercises

Solution to Exercise 1

The nodal values are

y=[p(0)p(12)p(1)]=[223].y= \begin{bmatrix} p(0)\\p(\tfrac12)\\p(1) \end{bmatrix} = \begin{bmatrix} 2\\2\\3 \end{bmatrix}.

For the monomial basis,

V=[10011214111].V= \begin{bmatrix} 1&0&0\\ 1&\tfrac12&\tfrac14\\ 1&1&1 \end{bmatrix}.

Solving Va=yVa=y returns a=(2,1,2)Ta=(2,-1,2)^\mathsf T. The solve changes coordinates; it does not construct a different polynomial.

Solution to Exercise 2

Problems 1 and 4 are interpolation problems; the least-squares formulation in problem 4 has a zero-residual interpolating solution. Problem 2 is regression. Problem 3 is neither statistical regression nor interpolation of observations: the unknown nodal values define an interpolating polynomial, and collocation adds equality constraints that select those values.

Solution to Exercise 3

The cardinal functions are

0=2τ23τ+1,m=4τ(1τ),1=2τ2τ.\ell_0=2\tau^2-3\tau+1,\qquad \ell_m=4\tau(1-\tau),\qquad \ell_1=2\tau^2-\tau.

They give

D=[341101143],w=16[141].D= \begin{bmatrix} -3&4&-1\\ -1&0&1\\ 1&-4&3 \end{bmatrix}, \qquad w=\frac16 \begin{bmatrix}1\\4\\1\end{bmatrix}.

For y=(1,74,2)Ty=(1,\tfrac74,2)^\mathsf T, Dy=(2,1,0)TDy=(2,1,0)^\mathsf T, which equals p(τ)=22τp'(\tau)=2-2\tau at the three nodes.

Solution to Exercise 4

One node gives the constant derivative interpolant FkF_k or Fk+1F_{k+1}. Integration yields the explicit or implicit Euler defect and a degree-one state. With endpoint nodes, x˙h=(1τ)Fk+τFk+1\dot x_h=(1-\tau)F_k+\tau F_{k+1}. Its integral at τ=1\tau=1 is 12(Fk+Fk+1)\tfrac12(F_k+F_{k+1}), which gives the trapezoidal defect. The linear derivative integrates to a degree-two state.

Solution to Exercise 5

Three distinct slope values define a quadratic derivative interpolant. Integrating that quadratic adds one degree, so the state is cubic. Integrating the cardinal functions over [0,1][0,1] gives (1,4,1)/6(1,4,1)/6 and hence the Simpson defect. Integrating to τ=12\tau=\tfrac12 gives (5,8,1)/24(5,8,-1)/24; eliminating the midpoint slope with the endpoint defect gives

Xk+12=12(Xk+Xk+1)+hk8(FkFk+1).X_{k+\frac12} =\frac12(X_k+X_{k+1}) +\frac{h_k}{8}(F_k-F_{k+1}).
Solution to Exercise 6

The NLP defect measures its equality constraints only at the selected nodes. A coarse polynomial can satisfy those constraints while failing to resolve rapid behavior between them. Evaluate the ODE residual and path constraints on a dense grid, preferably using an independent high-accuracy replay. Refine the offending intervals or raise the local approximation degree, then solve and validate again. Tightening an already small NLP tolerance does not fix representation error.

Summary and Outlook

Direct collocation turns an optimization over whole functions into an optimization over finitely many state and control values. These nodal values define polynomial segments. Algebraic defect constraints then require each segment to agree with the ODE at selected points.

Each polynomial has two equivalent descriptions: basis coefficients or values at nodes,

p(τ)=jajϕj(τ)=jyjj(τ),y=Va.p(\tau)=\sum_j a_j\phi_j(\tau) =\sum_j y_j\ell_j(\tau), \qquad y=Va.

Direct collocation uses the nodal description because the stored values have an immediate physical meaning. Lagrange cardinal functions turn them into fixed operators for computing derivatives, endpoints, and integrals. Although this construction resembles polynomial fitting, the nodal values are optimization variables rather than noisy observations, and the ODE residual is an equality constraint rather than a regression loss.

Interpolating one left or right slope gives explicit or implicit Euler and a linear state approximation. Interpolating both endpoint slopes gives the trapezoidal defect and a quadratic state approximation. Adding the midpoint slope gives Simpson weights, the Hermite--Simpson midpoint relation, and a cubic state approximation.

Across many intervals, each defect involves only neighboring endpoints and local stage values. Most entries in the constraint Jacobian are therefore zero, which allows an NLP solver to exploit a sparse, block-banded structure. The overhead-crane example also shows why solving the NLP is not the final check: constraints that hold at the nodes may still be violated between them. A dense, independent simulation should therefore follow the optimization. Can the same finite-horizon problem respond when that replay reveals a state different from the prediction? Receding-horizon control turns the open-loop transcription into feedback by replanning from each measurement.

References
  1. Kelly, M. (2017). An Introduction to Trajectory Optimization: How to Do Your Own Direct Collocation. SIAM Review, 59(4), 849–904. 10.1137/16M1062569
  2. Andersson, J. A. E., Gillis, J., Horn, G., Rawlings, J. B., & Diehl, M. (2019). CasADi: A Software Framework for Nonlinear Optimization and Optimal Control. Mathematical Programming Computation, 11(1), 1–36. 10.1007/s12532-018-0139-4