Equivalent Digital Simulation
// 2nd order homogeneous ODE for a mass-spring-damper.
// Guy Fernando (2021)
// Compute M.y" + B.y' + K.y = 0
var f = function (m, b, k) {
return function (x, y) {
return [y[1], -((b * y[1]) + (k * y[0])) / m];
}
}
// Use ODEX to solve the ODE.
var s = new Solver(2);
s.denseOutput = true;
s.absoluteTolerance = s.relativeTolerance = 1e-10;
var xValues = [];
var yValues = [];
const M = 1.0, B = 0.5, K = 5.0;
s.solve(f(M, B, K), 0, [1, 0], 10, s.grid(0.01, function (x, y) {
xValues.push(x);
yValues.push(y[0]);
}));
// Render the graph using Plotly.js.
const config = { responsive: true }
const data = [{
x: xValues,
y: yValues,
type: 'line'
}];
Plotly.newPlot('plot', data, null, config);
Today digital computers are chosen in favour of op-amps for calculating and solving ODEs. As an example of solving the
ODE digitally, the same mass spring damper system as modelled above using op-amps is shown here modelled in the JavaScript
listing. This code is actually running on this web page for plotting the graph above.
The code here uses the ODEX library odex.js ported from the originally version written in FORTRAN. (I had to modify the
library slightly such that it is not dependent on Node.js for client-side use with web browsers.) The ODEX library
implementation is based on the Bulirsch–Stoer algorithm for the numeric solution of ODEs obtaining high accuracy with
comparatively little computational effort.