That's not very surprising.
You have discretized the differential equation.
You might solve the discretized version you got, and see that indeed its solution is what you found.
This means that you need to find a better numerical approximation.
One that is stable, or more stable.
This means an approximation that will be closer to the solution of the DE on a longer time span.
There is of course no approximation that will fit exactly the DE for any time.
There are many methods that improve your integration scheme.
One of the best known is the Runge-Kutta (RG) method.
In my personal toolbox methods are those two that I like to play (RG or other for serious work) with:
- "more precise stepping" (small improvement, less divergent)
v(i)=(h/m)*F(i-1)+v(i-1);
x(i)=F(i-1)/m*h²/2+v(i-1)*h+x(i-1);
- "implicit scheme" (decreasing amplitude)
by solving at each time step the linear system:
F(i)=-k*x(i);
v(i)=(h/m)*F(i)+v(i-1);
x(i)=h*v(i)+x(i-1);
(many variants are possible)
or (if I am not mistaken):
F(i) = -((m*(dt*k*v(i-1) + k*x(i-1)))/(dt**2*k + m))
v(i) = -((-(m*v(i-1)) + dt*k*x(i-1))/(dt**2*k + m))
x(i) = (m*(dt*v(i-1) + x(i-1)))/(dt**2*k + m)
- try RG:
http://en.wikipedia.org/wiki/Runge–Kutta_methods
The stability of any numerical scheme can be analyzed.
You can also do numerical experiments, very easily with matlab.
You will see the benefit of renowned methods, like the RG.
- see also:
http://en.wikipedia.org/wiki/Numerical_methods_for_ordinary_differential_equations
- the DE can also be written as a 2x2 system of 1st order differential equations:
x' = v
v' = F/m = -k x / m
which can be put into matricial for:
transpose({x',v'}) = {{0,1},{-k/m,0}} transpose({x,v})
or: X' = M X where X = transpose({x',v'})
the solution of which is simply X = Exp(M t) Xo
Calculating Exp(M t) is almsot a standard of linear algebra,
and in the case where M is constant, you get an exact solution.
This is of course almost trivial here, as this would be equivalent to an analytical solution.
However, if M is not constant in time, then this integration scheme on a step by step basis is very efficient.
Also, Exp(M t) can also be calculated approximately, and a 1rst order Taylor gives you the method you already used.
Better approximations can for example be based on Padé approximants.At this stage, just try a Runge-Kutta method.