I'm trying to model a spring damper system from a tutorial that I've found on this site.

If I use the exact same parameters as the ones in the tutorial the system is not stable. I've downloaded the code and verified that the parameters were the same as mine.

a = (k * u) - (d * v);
v = v + a;
s = s + v;

jsfiddle available here

I can make it stable by adding a factor 0.3, like so

s = 0.3 * s + v

However, I would like to know how to make it stable without hacking it. I'm aware that the integrator is not good, but it should still work for this case, because it does so in the demo.

  • $\begingroup$ the equation is supposed to be $s=s_o+vt+\frac{1}{2}at^2$ $\endgroup$
    – Greg
    Commented May 19, 2013 at 20:00
  • $\begingroup$ @Greg, no this formula is for constant acceleration. The OP is talking about a numerical solution where the acceleration changes with position and velocity. $\endgroup$ Commented May 19, 2013 at 20:16

1 Answer 1


Your spring model is fine. It is the numerical integration that needs attention. What you have above is Euler's method for numerical integration with $\Delta t=1$. This method is known to be unstable. You made it stable with artificial (numerical) damping. For a simple spring damper system try to use the mid-point method or better yet a four step Runge-Kutta method

More information here and here.

Your code would like something like this

// time step h=1 or less

a1 = (k*u) - (d*v)
u2 = u + (h/2)*v
v2 = v + (h/2)*a1
a2 = (k*u2) - (d*v2)

u = u + h*v2
v = v + h*a2
  • 1
    $\begingroup$ Thank you. I started searching for integrators and ended up here. The slide before the last one shows the problem. $\endgroup$ Commented May 19, 2013 at 20:39
  • $\begingroup$ @stackoverflownewbie Great reference url. $\endgroup$ Commented May 19, 2013 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.