Open your browser's debug console and paste this in:
function solveSecondOrderODE(b, c, y0, dy0, dt, tMax) {
let t = 0;
let y = y0;
let dy = dy0;
while (t <= tMax) {
console.log(`At time ${t.toFixed(2)}: y = ${y.toFixed(4)}`);
let ddy = -b * dy - c * y; // Compute the acceleration (second derivative)
y += dy * dt; // Update position
dy += ddy * dt; // Update velocity
t += dt;
}
}
// Example usage:
// Constants b and c for the damped harmonic oscillator
const b = 0.5;
const c = 1.0;
// Initial conditions: y0 (initial position), dy0 (initial velocity)
const y0 = 1.0;
const dy0 = 0.0;
// Time step (dt) and maximum time (tMax)
const dt = 0.1;
const tMax = 10;
solveSecondOrderODE(b, c, y0, dy0, dt, tMax);