I was trying to simulate the dynamics of a coin with numerical integration. Using Lagrangian mechanics, I found the EOMs and now I can numerically integrate the functions to find a solution.
I've tried to use Euler's Method, but to I've been having numerical precision errors. I've tried to upgrade the integration method for a Runge-Kutta algorithm, but it seems the definition requires the derivatives to be evaluated at a later instant of time. For example, calculating $k_2$ (following wikipedia's notation): $$ k_2 = f \left(t_n + \frac{h}{2}, y_n + \frac{k_1}{2} \right) $$ where: $$ f(t, y) = \frac{dy}{dt} $$ To calculate the integral, I should know the derivative at a later instant in time. The problem is, the only way I can find the derivative at a later instant in time is when I first calculate the integral, so it is basically it is a recursive cycle.
As you can see from this line of code:
d2theta = ((m * R * R) * dpsi * dphi * sin(theta) - (m * R * R + Ipsi - Itheta) * dphi * dphi * cos(theta) * sin(theta) - (m*g*R) * cos(theta)) / (m * R * R + Itheta);
dtheta += d2theta * dt;
theta += dtheta * dt;
I know the variables $\ddot \theta$ depends on, but I do not have an explicit form of the function. After this, I calculate $\dot \theta$ and $\theta$.
It is possible to implement a Runge-Kutta (or a similar method) in this case? If yes, how would I do it?
EDIT: for further calification here are the differential equations I'm trying to solve: $$ \ddot{\theta} = - \frac{m R^2 + I_{\psi} - I_{\theta}}{mR^2 + I_\theta} \dot{\varphi}^2 \cos \theta \sin \theta + \frac{m R^2}{mR^2 + I_\theta} \dot{\psi} \dot{\varphi} \sin \theta - \frac{m g R}{mR^2 + I_\theta} $$ $$ \dot{\varphi} = \frac{L_\varphi + m R^2 \dot{\psi} \cos \theta }{(m R^2 + I_\psi - I_\theta) \cos^2 \theta + I_\theta} $$ $$ \dot{\psi} = \frac{L_\psi + m R^2 \dot{\varphi} \cos \theta }{m R^2 \cos^2 \theta + I_\varphi} $$ where $\theta, \varphi$ and $\psi$ are variables. It turns out that $\theta$ is the most problematic variable due to being described by a second order differential equation.