An algorithm which implements these equations is included below.
- Choose the initial position and speed, with the most common choice v(t=0)=0 and some fixed value for the position.
- Choose the method you wish to employ in solving the problem.
- Subdivide the time interval [t_i,t_f] into a grid with step size
h=\frac{t_f-t_i}{N},
where
N is the number of mesh points.
- Calculate now the total energy given by
E_0=\frac{1}{2}kx(t=0)^2=\frac{1}{2}k.
- The Runge-Kutta method is used to obtain x_{i+1} and v_{i+1} starting from the previous values x_i and v_i .
- When we have computed x(v)_{i+1} we upgrade t_{i+1}=t_i+h .
- This iterative process continues till we reach the maximum time t_f .
- The results are checked against the exact solution. Furthermore, one has to check the stability of the numerical solution against the chosen number of mesh points N .