# Spring damper model does not work very well

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.

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

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

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

• Thank you. I started searching for integrators and ended up here. The slide before the last one shows the problem. May 19, 2013 at 20:39
• @stackoverflownewbie Great reference url. May 19, 2013 at 20:43