The parts
A 2 kg cart rolls on a smooth table. A spring and a damper tie it to a wall. A damper is a piston in oil, like a door closer: it resists motion. We want an equation that predicts where the cart goes.
Let x be how far the cart has moved right from where it rests with the spring relaxed, in metres. Right is positive. Its velocity is ẋ = dx/dt and its acceleration is ẍ = d²x/dt².
From t = 0 we push the cart to the right with a steady force F = 10 N and keep pushing. The force is the system's input. The position x(t) is its output: the thing we want to predict.
A spring pulls back in proportion to its stretch: force = −kx (Hooke's law). With k = 50 N/m and the cart at x = 0.1 m, the spring pulls 5 N to the left.
A damper pushes against the velocity, not the position: force = −bẋ. With b = 4 N·s/m and the cart moving right at 0.5 m/s, it pushes 2 N to the left. A cart standing still feels nothing from it.
The equation
Draw the cart alone with every force on it: a free-body diagram. Along x there are three: the push F to the right, the spring −kx and the damper −bẋ. Gravity and the table cancel, up and down.
Total force equals mass times acceleration: F − kx − bẋ = mẍ. Move every x term to the left: mẍ + bẋ + kx = F(t). That is the equation of motion, a linear second-order differential equation.
With our parts: 2ẍ + 4ẋ + 50x = 10. Divide by m: ẍ + 2ẋ + 25x = 5. The two numbers that shape the motion are ωn = √(k/m) = 5 rad/s and ζ = b/(2√(km)) = 0.2.
Solving it
The code steps the equation forward every millisecond. The cart overshoots to 0.305 m at 0.64 s, swings back and settles. Watch the arrows: the spring force follows x, the damper force follows speed.
Once the cart stops, ẋ and ẍ are zero, so the equation leaves kx = F: x = F/k = 10/50 = 0.2 m. The spring alone holds the push. The damper only decides how the cart gets there.
Fit a stronger damper, b = 20 N·s/m. One coefficient changes: 2ẍ + 20ẋ + 50x = 10, and ζ becomes 1, called critical damping. The cart now creeps up to 0.2 m with no overshoot.
If the cart starts at rest, the Laplace transform turns ẍ into s²X(s) and ẋ into sX(s): (2s² + 4s + 50)X(s) = F(s). The ratio X(s)/F(s) = 1/(2s² + 4s + 50) is the cart's transfer function.
What is happening
# step 1/12 · setup # about: Cart m = 2 kg, Wall (fixed), Spring k = 50 N/m, Damper b mass m = 2 kg (the cart) spring k = 50 N/m (how stiff) damper b = 4 N·s/m (how much it resists speed) Goal: an equation for x(t), the cart's position over time. Its solution is what the cart will do.
The cart, spring and damper are illustrative values (m = 2 kg, k = 50 N/m, b = 4 or 20 N·s/m, F = 10 N), chosen so ωn = 5 rad/s and ζ = 0.2 exactly. The frozen moment used to explain each force (x = 0.1 m, ẋ = 0.5 m/s) is an example state; every curve and every number while the equation runs is the exact solution of the equation of motion for these values. The Python steps the equation forward 1 ms at a time (semi-implicit Euler). It reaches the same peak, 0.305 m, as the exact solution drawn in the plot.