Numerical solutions of the Mass-Spring-Damper model.
cc nmsd.c -o nmsd -lm
./nmsd && gnuplot -p nmsd.pltWarning
This document lacks many important details. Its purpose is to lay down the basic ideas. To fully understand the presented material, the suggested papers/books must be studied.
The choice of the "cryptic," old style is deliberate. It serves three purposes:
- C is an old but easy-to-learn language that can be compiled even on embedded systems. It is an efficient language, meaning it is favorable for numerical methods and it does not hide the implementation details.
- Declaring the variables at the top of the function makes the algorithms less noisy, in my opinion.
- The cryptic variable names prepare the reader: many numerical methods have useful old C/FORTRAN implementations to study, but they use the same "cryptic" style because of the limitations of their time.
Warning
The implementation of the methods is not general. They are capable of solving only a second order differential equation, but they can be easily modified to extend their capabilities.
The Mass-Spring-Damper model describes a 1-degree-of-freedom mechanical system consisting of a mass, a spring and a damper. The equation of motion of the unforced system:
where
The governing equation is a 2nd order linear homogeneous differential equation with constant coefficients. There are many ways to solve this equation numerically, only a few will be presented in this document and their implementation can be found in the accompanying nmsd.c file.
We can solve this equation analytically, this will be the base-line for the comparison of different numerical methods.
Let's introduce the damping constant
after regrouping we get:
since
which is a quadratic equation. To solve it we can use the quadratic formula:
Since the original equation is linear, the general solution is:
The
Note
Suggested material:
Boyce W.E., DiPrima R.C., Meade D.B. (2017): Elementary Differential Equations and Boundary Value Problems, ISBN 978-1-119-37792-4
- Chapter 3: Second-Order Linear Differential Equations
Thomson W.T. (1993): Theory of Vibration with Applications, ISBN 0-7487-4380-4
- Chapter 2.6: Viscously Damped Free Vibration
The explicit Euler method (also called the forward Euler method) is a numerical procedure for solving ordinary differential equations with a given initial value.
Let's recall the defintion of the derivative:
assuming
after some rearrangement, we get:
where
This is why it is called an explicit method, because the calculation of the solution at the next time step is based only on the information that is already known (the current time step).
Since
The Euler method can solve the following problem:
which is a first order ordinary differential equation with a given initial value, but we want to solve a second order ordinary differential equation. We can transform our original equation to a first order system of differential equations as follows:
We introduce the state vector:
meaning the derivative of this vector becomes:
so the original equation can be written as:
This is the msd() function defined in the source code.
This transformation allows us to use the Euler method to solve our problem by applying it to each equation in the system given the
initial conditions.
This method is often used in simpler video games as a base of the physics engine due to its simplicity and speed for a first-pass or low-complexity physics systems.
Note
Suggested material:
Iserles A. (2009): A First Course in the Numerical Analysis of Differential Equations, ISBN 978-0-511-50637-6
- Chapter 1.2: Euler’s method
Teschl G. (2012): Ordinary Differential Equations and Dynamical Systems, ISBN 978-0821883280
- Chapter 3.2: Linear autonomous first-order systems
- Chapter 3.3: Linear autonomous equations of order
$n$
Millington I. (2007): Game Physics Engine Development, ISBN 978-0-12-381976-5
- Chapter 3.3: The Integrator
Since the error is dependent of the time step, one way to reduce this error is to evaluate the function more times in one time step. This is the basic idea of the explicit Runge-Kutta method.
It achieves this by computing the
and using them to compute the final result:
This method evaluates the function 4 times.
A more compact notation of these steps is the Butcher tableau:
The explicit Runge-Kutta method is a fourth-order method (
Note
Suggested material:
Runge C.D.T. (1895): Über die numerische Auflösung von Differentialgleichungen, DOI 10.1007/BF01446807
Kutta W. (1901): Beitrag zur näherungsweisen Integration totaler Differentialgleichungen, Zeitschrift für Mathematik und Physik, 46, pp. 435–453
Butcher J.C. (1965): On the Attainable Order of Runge-Kutta Methods, DOI 10.2307/2003670
Hairer E., Norsett S.P., Wanner G. (1993): Solving Ordinary Differential Equations I: Nonstiff Problems, ISBN 978-3-540-56670-0
- Chapter II.1: The First Runge-Kutta Methods
The other method to improve the accuracy of the numerical method is to decrease the step size, but this leads to an increase of the numerical computation needed.
An other aspect is that we change the step size to improve the accuracy; this is what we want to control.
This is why the adaptive step or variable step methods were born: the embedded Runge-Kutta methods.
The idea is that we use
- if the solution is accepted: increase the step size, to avoid unnecessary computation,
- otherwise: decrease the step size and rerun the calculation.
This way we directly control the error by ensuring it remains below the combined threshold defined by the absolute tolerance and the relative tolerance.
The Dormand–Prince method is a fifth-order method with a fourth-order error estimation. The Butcher tableau of the method:
| 0 | ||||||||
This method is widely used in scientific computing; for example this is the default method of the MATLAB/Simulink software (ode45).
Note
Suggested material:
Dormand J.R., Prince P.J. (1980): A family of embedded Runge-Kutta formulae, DOI 10.1016/0771-050X(80)90013-3
Hairer E., Norsett S.P., Wanner G. (1993): Solving Ordinary Differential Equations I: Nonstiff Problems, ISBN 978-3-540-56670-0
- Chapter II.4: Automatic Step Size Control
- Chapter II.4: Starting Step Size
- Chapter II.5: Embedded Formulas of Order 5
Shampine L., Reichelt M. (1997): The MATLAB ODE Suite, DOI 10.1137/S1064827594276424
The Verlet integration is a numerical method designed to integrate Newton's equations of motion:
where
The Velocity-Verlet method can solve the system using the following steps:
- Calculate acceleration in the current time step
- Update the half-step velocity
- Update position with the half-step velocity
- Calculate new acceleration
- Update the full-step velocity
This method is a second order method.
It is often used in Discrete Element Method and Molecular Dynamics simulations due to its stability and energy conservative properties.
Note
Suggested material:
Verlet L. (1967): Computer "Experiments" on Classical Fluids. I. Thermodynamical Properties of Lennard-Jones Molecules, DOI 10.1103/PhysRev.159.98
Allen M.P., Tildesley D.J. (2017): Computer Simulation of Liquids, ISBN 978–0–19–880319–5
- Chapter 3.2.1: The Verlet algorithm
Vyas D.R., Ottino J.M., Lueptow R.M., Umbanhowar P.B. (2025): Improved velocity-Verlet algorithm for the discrete element method, DOI 10.1016/j.cpc.2025.109524
The results of the presented methods can be seen in the following image, where the displacement

