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.

Every chapter so far has leaned on one magic line, sd.sdof_response(...), with the promise that you would build it yourself, and this chapter keeps that promise. By the end you will have written, from scratch, the solver that the rest of the book quietly calls for harmonic loads, blast pulses, wind, and earthquakes. There is no new physics here, only one equation, (1), and three honest ways to march it forward in time.

The strategy is the spine of the whole book: cast the equation of motion into state space, a first-order form that general-purpose solvers integrate natively, and then step it. We will meet the exact stepper first, a matrix exponential that is correct to machine precision. Then come the two approximate steppers every structural-dynamics code actually ships with, Newmark-β\beta and the central-difference method, and the chapter ends by watching how their error shrinks as you refine the time step.

m u¨(t)+c u˙(t)+k u(t)=p(t).m\,\ddot u(t) + c\,\dot u(t) + k\,u(t) = p(t).

1Why computers want first-order equations

Figure 1 maps the route this chapter takes from the equation of motion to a working solver. The top row is a change of variables: stacking the displacement and the velocity into one state vector turns the second-order equation of motion into a first-order system. The bottom row is the march: once that system is discretized over a step Δt\Delta t, two constant matrices carry the state from one instant to the next, and a loop repeats that single operation until the record ends. The first two sections below derive the two rows in turn.

The plan of the chapter: the second-order equation of motion recast as a
first-order state-space system (top), and the discretized one-step march that a
loop repeats (bottom).

Figure 1:The plan of the chapter: the second-order equation of motion recast as a first-order state-space system (top), and the discretized one-step march that a loop repeats (bottom).

General-purpose ODE solvers, such as SciPy’s solve_ivp or MATLAB’s ode45, cannot take (1) as written, because it is second order: the highest derivative is u¨\ddot u. They are built to advance a first-order system

z˙=f(z,t),\dot z = f(z, t),

one that says “given the state right now, here is its rate of change.” The trick, borrowed from control theory, is to stack the displacement and the velocity into a single state vector so that the one second-order equation becomes two coupled first-order equations. Structural codes such as OpenSees take the other route and integrate the second-order form directly with Newmark’s method, which this chapter meets later; the first-order form is the one that admits an exact step.

Define the state

z(t)=[u(t)u˙(t)],z(t) = \begin{bmatrix} u(t) \\ \dot u(t) \end{bmatrix},

the displacement on top and the velocity underneath, so its time derivative is z˙=[u˙, u¨]T\dot z = [\dot u,\ \ddot u]^{T}. The top row is free: u˙\dot u is literally the second entry of zz. The bottom row comes straight from (1), solved for the acceleration:

u¨=1m(p−c u˙−k u)=−km u−cm u˙+1m p.\ddot u = \frac{1}{m}\bigl(p - c\,\dot u - k\,u\bigr) = -\frac{k}{m}\,u - \frac{c}{m}\,\dot u + \frac{1}{m}\,p.

Write those two rows as a matrix-vector product and the second-order equation has become a clean first-order state-space system:

z˙=A z+B p,A=[01−km−cm],B=[01m].\dot z = A\,z + B\,p, \qquad A = \begin{bmatrix} 0 & 1 \\[2pt] -\dfrac{k}{m} & -\dfrac{c}{m} \end{bmatrix}, \qquad B = \begin{bmatrix} 0 \\[2pt] \dfrac{1}{m} \end{bmatrix}.

That is the entire recast: read off the top and bottom rows of (5) and you recover u˙=u˙\dot u = \dot u and (4). The matrix AA holds the structure (its mass, damping, and stiffness); the vector BB says where the load pp enters (it pushes the velocity, never the position directly), and together they make up the top row of Figure 1.

In code, AA and BB are four numbers and two numbers. The engine exposes them as sd.state_space, and we can confirm they match (5) by hand. We use the same idealized one-story frame as Chapter 1 throughout, so the numbers stay familiar.

A =
 [[  0.    1. ]
 [-25.   -0.5]]
B =
 [0.  0.5]

Tn = 1.257 s,  wn = 5.000 rad/s,  zeta = 0.050

The bottom-left entry is −k/m=−25-k/m = -25 and the bottom-right is −c/m=−0.5-c/m = -0.5, while BB injects 1/m=0.51/m = 0.5 into the velocity equation. There is nothing mysterious in either matrix: they are the equation of motion, rearranged so a computer can read it.

2The exact step: a matrix exponential

To see why a clean solution exists, forget matrices for a moment and look at the scalar version of (5). The simplest first-order equation is

x˙=a x,\dot x = a\,x,

a single number aa in place of the matrix AA. Every calculus course shows its solution: the function whose rate of change is proportional to itself is the exponential, so starting from x(0)=x0x(0)=x_0:

x(t)=eat x0.x(t) = e^{a t}\,x_0 .

Step forward by Δt\Delta t and the multiplier that carries xkx_k to xk+1x_{k+1} is just the number eaΔte^{a\Delta t}. The state-space equation z˙=Az\dot z = A z (take p=0p=0 for the moment) is exactly this same equation, only with a 2×22\times2 matrix AA instead of the scalar aa and a vector zz instead of xx. It would be remarkable if the answer were different, and it is not. The solution is

z(t)=eAt z0,z(t) = e^{A t}\,z_0,

where eAte^{At} is the matrix exponential: the matrix analogue of eate^{at}, defined by feeding the matrix into the very same power series that defines the scalar exponential:

eAt=I+At+12!(At)2+13!(At)3+⋯ .e^{A t} = I + A t + \tfrac{1}{2!}(A t)^2 + \tfrac{1}{3!}(A t)^3 + \cdots .

Differentiate that series term by term and you get A eAtA\,e^{At}, so z(t)=eAtz0z(t)=e^{At}z_0 genuinely satisfies z˙=Az\dot z = A z, exactly as ddteat=a eat\tfrac{\mathrm d}{\mathrm dt}e^{at}=a\,e^{at} in the scalar case. The matrix eAte^{At} is the propagator: hand it any state and it returns that state advanced by a time tt, with no approximation.

Figure 2 shows what that means in the phase plane of Chapter 2, for a lightly damped oscillator in free vibration. Each dot is the state one step of Δt=Tn/10\Delta t = T_n/10 after the last, obtained by multiplying by eAΔte^{A\Delta t}, and every dot sits exactly on the continuous spiral, however coarse the step. Keeping only the first two terms of the series (9), I+AΔtI + A\Delta t, moves the state along the tangent instead, and that single step already lands off the curve.

Free vibration in the phase plane: exact steps z_{k+1} = e^{A\Delta t}z_k
(dots) on the continuous trajectory, and a single tangent step
(I + A\Delta t)\,z_0 (square).

Figure 2:Free vibration in the phase plane: exact steps zk+1=eAΔtzkz_{k+1} = e^{A\Delta t}z_k (dots) on the continuous trajectory, and a single tangent step (I+AΔt) z0(I + A\Delta t)\,z_0 (square).

Now restore the load, held constant across each step: p(t)=pkp(t)=p_k for tk≤t<tk+1t_k\le t<t_{k+1}, the zero-order hold (ZOH) assumption. Then z˙=Az+Bpk\dot z = A z + B p_k is the same linear equation with a constant push, and integrating it over the step (homogeneous propagator plus the particular response to the constant pkp_k) gives the exact one-step march

zk+1=Ad zk+Bd pk,Ad=eA Δt,Bd=A−1(Ad−I) B.z_{k+1} = A_d\,z_k + B_d\,p_k, \qquad A_d = e^{A\,\Delta t}, \qquad B_d = A^{-1}\bigl(A_d - I\bigr)\,B .

The first term propagates the current state forward by eAΔte^{A\Delta t}; the second, Bd=A−1(Ad−I)BB_d = A^{-1}(A_d-I)B, is what the constant force pkp_k adds over the step (it is ∫0ΔteAτ dτ B\int_0^{\Delta t} e^{A\tau}\,\mathrm d\tau\,B written in closed form). The two discrete operators AdA_d and BdB_d are computed once and do not change from step to step, because mm, cc, kk, and Δt\Delta t are constant, and then (10) is just a 2×22\times2 matrix multiply per step. That is the loop on the bottom row of Figure 1.

The whole solver fits in about a dozen lines. Let us write it plainly, without reaching for sdtools, so you can see there is no sleight of hand.

2.1Verifying against the truth

Chapter 2 gives us a closed-form answer to check against. For free vibration (p=0p = 0) of an underdamped oscillator released from u0u_0 with velocity u˙0\dot u_0, the displacement is

u(t)=e−ζωnt[ u0cos⁡ωdt+u˙0+ζωnu0ωd sin⁡ωdt],u(t) = e^{-\zeta\omega_n t} \left[\,u_0\cos\omega_d t + \frac{\dot u_0 + \zeta\omega_n u_0}{\omega_d}\,\sin\omega_d t \right],

with ωd=ωn1−ζ2\omega_d = \omega_n\sqrt{1-\zeta^2}. Let us nudge the roof by 20 mm, release it, and compare our hand-written solver to this exact formula.

maximum absolute error over 6 s : 4.42e-17 m
<Figure size 962x390 with 1 Axes>
<Figure size 962x390 with 1 Axes>

Figure 3:The matrix-exponential solver (markers) laid on top of the exact damped free-vibration formula (line).

The error is ∼10−17\sim 10^{-17} m, machine precision, and that is worth pausing on. The matrix exponential does not approximate the free-vibration response; it reproduces (11) term for term, because eAΔte^{A\Delta t} is the exact propagator of a linear system. The only approximation in (10) is the zero-order hold on the forcing, and when there is no forcing there is nothing left to approximate.

our exact_step vs sd.sdof_response  ->  max difference = 0.00e+00

The difference is exactly zero, so there is no hidden machinery. When Chapter 6 builds a response spectrum by solving hundreds of oscillators, this is the loop running inside.

3Newmark-β\beta: the structural-dynamics workhorse

If the matrix exponential is exact, why does any engineering code bother with anything else? The first reason is that eAΔte^{A\Delta t} is exact only for a linear, time-invariant system: the moment the structure yields (steel that goes plastic, a gap that closes), AA changes every step and the clean propagator is gone. The second is that the profession standardized decades ago on a family of steppers that work directly with uu, u˙\dot u, u¨\ddot u and extend gracefully to nonlinear problems. The most popular is Newmark-β\beta (Newmark, 1959).

Newmark’s idea is to assume a shape for how the acceleration varies across one step, then integrate that shape once to get the velocity and twice to get the displacement. Figure 4 shows the two shapes in common use. Inside one step the true acceleration (dashed) is unknown, but its two end values are tied to the equation of motion at the start and the end of the step. The average-acceleration rule replaces it with the constant mean of the two ends and the linear-acceleration rule with a straight line between them, and in both cases the shaded area under the assumed shape is the change in velocity across the step.

The acceleration assumed inside one time step by the average-acceleration rule
(left) and the linear-acceleration rule (right), against an unknown true
variation through the same end values.

Figure 4:The acceleration assumed inside one time step by the average-acceleration rule (left) and the linear-acceleration rule (right), against an unknown true variation through the same end values.

The simplest useful assumption is the left one: the acceleration is constant over the step, equal to the average of its two endpoints:

u¨(τ)≈aˉ=12(u¨k+u¨k+1),0≤τ≤Δt.\ddot u(\tau) \approx \bar a = \tfrac12\bigl(\ddot u_k + \ddot u_{k+1}\bigr), \qquad 0 \le \tau \le \Delta t .

Integrating this constant from τ=0\tau=0 (where the velocity is u˙k\dot u_k and the displacement is uku_k) gives, at τ=Δt\tau=\Delta t:

u˙k+1=u˙k+aˉ Δt,uk+1=uk+u˙k Δt+12 aˉ Δt2.\dot u_{k+1} = \dot u_k + \bar a\,\Delta t, \qquad u_{k+1} = u_k + \dot u_k\,\Delta t + \tfrac12\,\bar a\,\Delta t^2 .

Substitute aˉ=12(u¨k+u¨k+1)\bar a = \tfrac12(\ddot u_k+\ddot u_{k+1}) back in and these are exactly the two update formulas Newmark uses. Newmark wrote them with two tunable weights γ\gamma and β\beta so that other acceleration shapes (linear, for instance) fit the same template:

u˙k+1=u˙k+Δt [(1−γ) u¨k+γ u¨k+1],uk+1=uk+Δt u˙k+12 Δt2[(1−2β) u¨k+2β u¨k+1].\begin{aligned} \dot u_{k+1} &= \dot u_k + \Delta t\,\bigl[(1-\gamma)\,\ddot u_k + \gamma\,\ddot u_{k+1}\bigr],\\[2pt] u_{k+1} &= u_k + \Delta t\,\dot u_k + \tfrac{1}{2}\,\Delta t^2\bigl[(1-2\beta)\,\ddot u_k + 2\beta\,\ddot u_{k+1}\bigr]. \end{aligned}

Setting γ=12\gamma = \tfrac12 and β=14\beta = \tfrac14 collapses (14) straight back to the average-acceleration integral (13). To check, note that the velocity bracket becomes 12(u¨k+u¨k+1)=aˉ\tfrac12(\ddot u_k+\ddot u_{k+1})=\bar a, and the displacement bracket becomes 12u¨k+12u¨k+1=aˉ\tfrac12\ddot u_k+\tfrac12\ddot u_{k+1}=\bar a as well. This average-acceleration rule (also called the trapezoidal rule) is special: it is unconditionally stable, meaning it never blows up no matter how large the time step, and it conserves energy exactly for an undamped oscillator. It is the default in OpenSees and the default behind sd.newmark. The linear-acceleration rule on the right of Figure 4 is the case γ=12\gamma = \tfrac12, β=16\beta = \tfrac16, which is more accurate per step but stable only for Δt<0.551 Tn\Delta t < 0.551\,T_n.

To turn (14) into a usable stepper, substitute them into the equation of motion written at step k+1k+1 and solve for uk+1u_{k+1}. For our linear SDOF that solve is a single division by an effective stiffness k^\hat k, after which the velocity and acceleration follow.

Example 1 takes one pass through Algorithm 2 by hand, so that every quantity in the loop has a number attached to it.

Here is the implementation, following the algorithm line for line.

The next cell runs that function for a single step and repeats Example 1, including the equilibrium check and the exact value.

a1 = 820   a2 = 81   a3 = 2   k_hat = 870
step 1: u = 0.01770 m   v = -0.0460 m/s   a = -0.4195 m/s^2
equilibrium check m*a + c*v + k*u = 3.3e-16 kN
exact at t = 0.1 s: u = 17.59 mm   (Newmark 17.70 mm, 0.6% high)

Cross-check the function three ways: against the library’s sd.newmark, against the exact matrix-exponential solver, and, for good measure, under a genuine forced load, not just free vibration. We drive the frame with a short sinusoidal push.

free vibration   : Newmark vs exact, peak diff = 3.07e-05 m
our newmark_step vs sd.newmark        = 0.00e+00 m
forced (pulse)   : peak |u|  Newmark = 619.101 mm, exact = 619.523 mm

The two solvers agree to well under a percent at this step size: the free-vibration traces differ by at most 0.031 mm on a 20 mm swing, and the forced peaks by 0.07 %. The small residual is exactly the approximation Newmark makes and the matrix exponential does not, and it shrinks predictably as Δt\Delta t falls, which we quantify in the convergence section. The same sd engine reaches Newmark through sd.sdof_response(..., method="newmark"); it is the path Chapter 7 follows when the spring stops being linear.

4Central difference: cheap, explicit, and only sometimes stable

The third stepper is the simplest of all. The central-difference method approximates the velocity and acceleration at step kk with symmetric finite differences of the displacement:

u˙k≈uk+1−uk−12 Δt,u¨k≈uk+1−2uk+uk−1Δt2,\dot u_k \approx \frac{u_{k+1} - u_{k-1}}{2\,\Delta t}, \qquad \ddot u_k \approx \frac{u_{k+1} - 2u_k + u_{k-1}}{\Delta t^2},

substitutes them into the equation of motion at step kk, and solves for the one unknown uk+1u_{k+1}. Because the equation is written at step kk (where everything except uk+1u_{k+1} is already known), no system solve is needed at all: the method is explicit. That makes each step extraordinarily cheap, which is why explicit integration dominates impact, blast, and crash simulation. The engine provides it as sd.central_difference.

Figure 5 pictures both the stencil and its price. Three displacement samples one step apart fix a parabola (left): the slope of the dashed chord through the outer two estimates the velocity at the middle point, and the change between the two green half-step slopes estimates the acceleration. The right panel plots the amplification factor ∣λ∣|\lambda|, the factor by which one step multiplies the amplitude of undamped free vibration. Average-acceleration Newmark keeps ∣λ∣=1|\lambda| = 1 for any step, while central difference does so only up to Δt=Tn/π\Delta t = T_n/\pi and then jumps above one, which the derivation below explains.

The central-difference stencil on a displacement history (left) and the
amplification factor of one step for undamped free vibration, central difference
against average-acceleration Newmark (right).

Figure 5:The central-difference stencil on a displacement history (left) and the amplification factor of one step for undamped free vibration, central difference against average-acceleration Newmark (right).

But the cheapness comes with a string attached. The central-difference method is only conditionally stable: the time step must be smaller than a critical value set by the natural period:

Δt<Δtcr=Tnπ.\Delta t < \Delta t_\text{cr} = \frac{T_n}{\pi}.

That π\pi is not arbitrary; it falls out of asking when one step amplifies the last. Apply the scheme to undamped free vibration (c=0c=0, p=0p=0): substituting the two finite differences (17) into mu¨+ku=0m\ddot u + k u = 0 and using ωn2=k/m\omega_n^2=k/m gives the three-term recurrence

uk+1=(2−ωn2Δt2) uk−uk−1.u_{k+1} = \bigl(2 - \omega_n^2\Delta t^2\bigr)\,u_k - u_{k-1}.

A recurrence like this has solutions of the form uk=λku_k = \lambda^k, and the motion stays bounded only if ∣λ∣≤1|\lambda|\le 1. Plugging uk=λku_k=\lambda^k into the recurrence gives the characteristic quadratic λ2−(2−ωn2Δt2)λ+1=0\lambda^2 - (2-\omega_n^2\Delta t^2)\lambda + 1 = 0. Its two roots multiply to 1, so they sit as a distinct conjugate pair on the unit circle (the bounded, oscillatory case) only while the middle coefficient stays strictly within (−2,2)(-2, 2). At the endpoint 2−ωn2Δt2=−22-\omega_n^2\Delta t^2 = -2 the two roots coincide at λ=−1\lambda=-1, and a repeated root on the circle grows linearly (uk∼k (−1)ku_k \sim k\,(-1)^k), already unstable; push 2−ωn2Δt22-\omega_n^2\Delta t^2 any further below -2 and one root jumps outside the unit circle, ∣λ∣>1|\lambda|>1, so every step magnifies the previous one. The threshold is therefore ωn2Δt2=4\omega_n^2\Delta t^2 = 4, i.e. ωnΔt=2\omega_n\Delta t = 2, and since ωn=2π/Tn\omega_n = 2\pi/T_n:

Δtcr=2ωn=22π/Tn=Tnπ.\Delta t_\text{cr} = \frac{2}{\omega_n} = \frac{2}{2\pi/T_n} = \frac{T_n}{\pi}.

Step past that limit and the algorithm does not merely lose accuracy: it explodes, each step amplifying the last, exactly as the ∣λ∣>1|\lambda|>1 root predicts. Our frame has Tn≈1.257T_n \approx 1.257 s, so Δtcr=Tn/π≈0.40\Delta t_\text{cr} = T_n/\pi \approx 0.40 s, and the next two cells watch the explosion happen on either side of that limit.

Tn = 1.257 s   ->   stability limit  dt_cr = Tn/pi = 0.400 s
dt = 0.05 s (< dt_cr): peak |u| =    20.00 mm   (well behaved)
dt = 0.42 s (> dt_cr): peak |u| = 3.04e+02 m    (diverged)
<Figure size 1118x403 with 2 Axes>
<Figure size 1118x403 with 2 Axes>

Figure 6:Central-difference free vibration of the frame with Δt=0.05\Delta t = 0.05 s, below Δtcr=Tn/π\Delta t_\text{cr} = T_n/\pi (left, linear axis), and with Δt=0.42\Delta t = 0.42 s, just above it (right, logarithmic axis).

With Δt=0.05\Delta t = 0.05 s the response decays exactly as it should. With Δt=0.42\Delta t = 0.42 s, a hair above the limit, it climbs from 20 mm to 304 m in eight seconds, more than four orders of magnitude, although the structure itself is perfectly well behaved: the blow-up is a property of the algorithm. Two lessons hide in Figure 6, and the first is practical: an explicit code must police its time step against (18), and for a real multi-story building the relevant TnT_n is the shortest (highest-mode) period, which can be tiny, forcing a very small Δt\Delta t. The second is reassuring: the matrix exponential and average-acceleration Newmark have no such limit. They are unconditionally stable; you choose Δt\Delta t for accuracy, never to avoid catastrophe.

5Accuracy and stability versus the time step

With three working solvers in hand, the next questions are how wrong each one is and how fast it improves as you shrink Δt\Delta t. Because we have an exact answer for free vibration, (11), we can measure the error honestly. For each step size we run all three methods, compare to the analytic trace, and report the root-mean-square error normalized by the peak amplitude.

exp     : error at dt=0.004 -> 5.88e-15,  at dt=0.16 -> 1.60e-15
newmark : error at dt=0.004 -> 1.47e-04,  at dt=0.16 -> 2.16e-01
central : error at dt=0.004 -> 7.64e-05,  at dt=0.16 -> 1.30e-01
<Figure size 858x546 with 1 Axes>
<Figure size 858x546 with 1 Axes>

Figure 7:Convergence of the three integrators on the free-vibration test: normalized RMS error versus time step on log-log axes, with a slope-2 reference guide.

The log-log slopes of Figure 7 tell the whole story. Newmark and central difference are both second-order methods: their error scales as Δt2\Delta t^2, so the curves are straight lines of slope 2 and halving the step cuts the error by four. The matrix exponential, by contrast, lies pinned at round-off for every step, a flat line at ∼10−15\sim 10^{-15}, confirming once more that for a linear system it is not approximating anything.

5.1What Newmark gets wrong: period elongation

Second-order accuracy describes how the error shrinks; it does not describe its character. The average-acceleration Newmark rule has a famous, well-understood fingerprint: it elongates the period slightly (the numerical oscillation runs a touch slow) while keeping the amplitude essentially right. The central- difference method does the opposite, shortening the period. We can see both by running a deliberately coarse step and overlaying the exact answer.

<Figure size 988x403 with 1 Axes>
<Figure size 988x403 with 1 Axes>

Figure 8:Period distortion at a coarse step (Δt=0.10\Delta t = 0.10 s, about Tn/13T_n/13): against the exact trace (gray), Newmark’s average-acceleration rule lags while central difference leads, both keeping the amplitude faithful.

exact damped period   Td        = 1.2582 s
Newmark   period (dt=0.10)       = 1.2833 s  (+2.0% elongated)
central   period (dt=0.10)       = 1.2446 s  (-1.1% shortened)
Newmark   period (dt=Tn/20)      = 1.2684 s  (+0.8% elongated)

A 2 % period error sounds harmless, but over a long earthquake record it accumulates into a visible phase shift, and near a resonance peak a small frequency error can move the response noticeably. The cure is simply a finer step, or the matrix exponential when the system is linear. A common rule of thumb is Δt≤Tn/20\Delta t \le T_n/20 for the mode you care about, and at that step the Newmark period error printed above falls to 0.8 %. Knowing the character of each method’s error is what lets you pick the step with confidence instead of superstition.

6The engine, demystified

We set out to build the solver the rest of the book takes for granted, and three ideas did all the work:

From here on, “solve the SDOF” is a settled, one-line operation. The next three chapters (harmonic and periodic loads in Chapter 4, arbitrary and measured loads in Chapter 5, and earthquake response spectra in Chapter 6) do nothing more than hand a different p(t)p(t) to sd.sdof_response and plot what comes back. You now know exactly what happens inside that call. The full sdtools API, every function the later chapters lean on, is cataloged in Appendix B.