# 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.

-

## migrated from physics.stackexchange.comMay 19 '13 at 20:21

This question came from our site for active researchers, academics and students of physics.

the equation is supposed to be $s=s_o+vt+\frac{1}{2}at^2$ –  Greg May 19 '13 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. –  ja72 May 19 '13 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. –  wubbewubbewubbe May 19 '13 at 20:39
@stackoverflownewbie Great reference url. –  ja72 May 19 '13 at 20:43