In the previous section, we derived the equations of motion we need to perform the simulation:
$$ \begin{align} & \frac{\mathrm{d}\dot{x}}{\mathrm{d}t} = \frac{m g}{m + 4I/r^2} \frac{w'}{1+w'^2} - \frac{F_{fric}(x,\dot{x})}{m + 4I/r^2} \frac{1}{\sqrt{1+w'^2}} - \dot{x}^2 \frac{w' w'' }{1+w'^2}\\ & \frac{\mathrm{d}x}{\mathrm{d}t} = \dot{x} \\ & \dot{x}_{t=0} = 0 \\ & x_{t=0} = 0 \end{align} $$These form a system of first-order ordinary differential equations which can be solved by a variety of numerical methods. Because of its accuracy and ease of implementation, we chose the Runge-Kutta (RK4) method to propagate $x$ and $\dot{x}$ through time. (See the implementation)
First we feed in the initial position and speed of the car. The method then advances forward in time by very short steps, re-calculating the acceleration at each step, and calculating the position of the car at the end of each time step.
The simulation is stopped once the car has traveled a distance along the track equal to the track length. The last time step is chosen so as to put the final value of the car exactly at the finish line. Because the track is curved, the final x-value of the car will not be equal to the track length.
Cars are modeled as point masses, so to account for the center of mass distance from the front of the car, we translate the car backward from the starting line by that same distance. We also translate the finish line forward by that same distance.
Finally, we plot the kinetic and potential energies calculated from the trajectory at each position on the track.