   Another possible method for modeling springs with Euler's method
  Now, as a disclaimer, this is based on my understanding of and limited experience with Euler's method. I recently took interest in trying to write something to model physics as a personal challenge and been looking at some resources as a guide. When I first came across an explanation of the complications in modeling springs, this solution instantly sprang to mind. I don't know if the physics is good, if the math is right, but its an interesting idea. I have yet to test an implementation of it. Hopefully someone here can give me insight or point out my mistakes.Euler's method isn't terrible. Its stable enough when the rate of change is linear. When acceleration is constant, using Euler's method to model movement is accurate enough. However springs are different. They present a problem because acceleration is not constant. However, the rate of acceleration is constant, what is known as the onset or jerk. Modeling springs on their jerk, however, take up back to linear rate of change equations that work much better under Euler's method. Here's some math, sorry for the sloppy formatting.Hooke's Law:F = -kp    where p is positiona = -1/m kpj = (DELTA a) / (DELTA t) where j is the jerk; a change in accelerationj = ((-kp_i / m) - (-kp_f / m)) / (DELTA t)   where p_i is the initial position and p_f is finalj = (-k)/(DELTA t m) * (p_f - p_i)j = (-k)/(DELTA t m) * (DELTA p)    since p_f - p_i is the change of positionDELTA p = -( DELTA t * m ) / k * jnow k and j are constants, because k was defined as such under Hooke's law and j because the rate of change of acceleration is constant. Therefore we can merge these two into a super constant, termed K. This leaves us with the equationDELTA p = -K * DELTA t * mWhere K is a different from k, but essentially a spring constant. Now, once again, I'm not sure if this is valid physics, but if it is, this equation should give a more accurate position per step during the integration.   
  Okay, i just realized that i have things slightly wrong. Euler's method would still bring in inaccuracies in situations I described. There will be an error in velocity calculations when there is acceleration. They are just minimized when acceleration is constant.I still stand by the math, but in that case, this equation could then be used for more accurate spring calculation, right? (Assuming its valid math)   ;  Jerk is not constant for a spring-mass system (unless it's standing still ;-)).  Simple example: Choose spring constant = 1, damping = 0; you get (state-space form):(d/dt) p = v(d/dt) v = -pNow say initial condition isp(0) = 1v(0) = 0 .Then the solution to this ODE is,p(t) = cos(t)v(t) = -sin(t)The jerk is,j(t) = (d/dt) v(t) = -cos(t)which is clearly not constant.-----If you want a decent numerical method (which is nearly as simple as explicit Euler) for solving the kinds of ODEs that appear when you model systems of springs, try semi-implicit Euler.  I'd say this is the "right" answer.  There are fancier (higher-order) versions of this, and they are known generally as symplectic integrators; you can google it if you want to dive in.----There are also hackish things you can do.  For instance, if your system looks like,(d/dt) x = f(x)and you know that the energy E(x) is conserved, then in principle the following ODE has the same solution(d/dt) x = f(x) - (dE/dx)T(E(x) - E(x0))since the second term is always zero.  But if you were to simulate this with Euler, the results would be different, and the second term would tend to conserve energy.  You can think of this as a poor man's predictor-corrector method.E.g., for the system above, x = (p,v) , E(x) = E(p,v) = (1/2)(p2 + v2) , and,(d/dt) p = v - p[(1/2)(p2 + v2) - E0](d/dt) v = p - v[(1/2)(p2 + v2) - E0]where E0 is the initial energy of the system.[Edited by - Emergent on May 21, 2009 3:29:50 PM]