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 [ti,tf] into a grid with step size
h=tf−tiN,
where
N is the number of mesh points.
- Calculate now the total energy given by
E0=12kx(t=0)2=12k.
- The Runge-Kutta method is used to obtain xi+1 and vi+1 starting from the previous values xi and vi.
- When we have computed x(v)i+1 we upgrade ti+1=ti+h.
- This iterative process continues till we reach the maximum time tf.
- 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.