Problem 296341 · hard · Level 02 Linear Data Structures

The Suspension Simulation Is a Difference Equation

difference equation · semi-implicit Euler · mass-spring-damper · final value · algebra

The suspension simulation keeps two numbers per step, position x and velocity v. The ride-height filter at the start of this level keeps two old outputs. They are the same kind of object: the simulation can be rewritten as a second-order difference equation in the positions alone,

x[j] = a1 * x[j-1] + a2 * x[j-2] + b1 * F

and then everything you know about difference equations (computing them sample by sample, their final value) applies to the car.

Recall the semi-implicit Euler step from position x[j-1] and velocity v[j-1] to step j, with a constant force F:

v[j] = v[j-1] + dt * (F - c * v[j-1] - k * x[j-1]) / m
x[j] = x[j-1] + dt * v[j]

The second line says that the velocity used in step j is v[j] = (x[j] - x[j-1]) / dt, and the same holds one step earlier: v[j-1] = (x[j-1] - x[j-2]) / dt. (It even holds for the first step, if you take x[-1] = 0 for a car at rest at x[0] = 0.) Substitute the first line into the second, replace v[j-1] by the position difference, and collect the terms in x[j-1], x[j-2] and F.

Write as_difference_equation(m, c, k, dt, F) that returns a tuple of four numbers: the coefficients a1, a2, b1, and the final value of the difference equation for this constant force, b1 * F / (1 - a1 - a2). Simplify that last one by hand first and see what it is physically.

You can test your coefficients with Run: positions computed from your difference equation must match the suspension simulation step for step.

Examples

Input:  m = 1, c = 0, k = 4, dt = 0.5, F = 2
Output: (1.0, -1.0, 0.25, 0.5)
Explanation: x[1] = 0.25 * 2 = 0.5, x[2] = 1.0 * 0.5 - 1.0 * 0 + 0.5 = 1.0,
x[3] = 1.0 * 1.0 - 1.0 * 0.5 + 0.5 = 1.0: the positions [0, 0.5, 1.0, 1.0] of the simulation.

Input:  m = 400, c = 2000, k = 20000, dt = 0.01, F = 800
Output: (1.945, -0.95, 2.5e-07, 0.04)

Constraints

  • answers are compared with a tolerance of 1e-6; do not round

Goals

  • Eliminate the velocity from a two-state simulation to get one equation in positions only
  • Write a mass-spring-damper simulation as a second-order difference equation
  • Check the final value of the difference equation against the physics
Starting Python…