Lets consider the situation of a rocket launched from the ground onto some impact function $f(x)$. Assume that the impact function is just the $x$ axis, hence the projectile hit the ground at the same altitude in which launch occur.
Goal: minimise the flight time such that the rocket reach a prescribe distance $d$. What is it the launch angle that achieves this?
Let $x,y$ be the components of the position. By Newton's second law I have found the following couple second order ODEs
\begin{align*} \ddot{x}(t)&=\frac{100\cos(\theta)}{m(t)}-k(t)\dot{x}(t)\sqrt{\dot{x}^2(t)+\dot{y}^2(t)}\\ \ddot{y}(t)&=\frac{100\sin(\theta)}{m(t)}-g-k(t)\dot{y}(t)\sqrt{\dot{x}^2(t)+\dot{y}^2(t)} \end{align*}
where
\begin{equation} m(t) = m_0-\mu t \quad k(t)=\frac{C_d}{m(t)},\quad C_d,m_0,\mu\in\mathbb{R} \end{equation}
where $m_0$ is the initial mass and $\mu$ the burn rate of the fuel.
Let $X(t) = (x(t),y(t),\dot{x}(t),\dot{y}(t))^T$ and $\gamma(t) = \sqrt{\dot{x}^2(t)+\dot{y}^2(t)}$, then the system of ODEs is equivalent to \begin{align*} \dot{X}(t) &= \begin{bmatrix} 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & -k(t)\gamma(t) & 0\\ 0 & 0 & 0 & -k(t)\gamma(t) \end{bmatrix} X(t) + \begin{bmatrix} 0 \\ 0 \\ \frac{100\cos\theta}{m_0-\mu t}\\ \frac{100\sin\theta}{m_0-\mu t} - g \end{bmatrix}\\ &= A(t)X(t)+B(t)\\ &=F(t,X(t)) \end{align*} The initial conditions are (initial velocity is assumed to be zero) \begin{align*} x(0)=0&\quad y(0)=0\\ x'(0)=u_0\cos(\theta)=0&\quad y'(0)=v_0\sin(\theta)=0 \end{align*} where $\theta$ is the launch angle at $t=0$ and is the tangent angle to the curve when $t>0$ (we assumed that the thrust is always in the direction of motion)
How can I implement the Fourth-Order Runge–Kutta Method such that the rocket reach the prescribe distance $d$ in the small time period?
It would be helpful some advice, I don't want the problem solved (I want to do it by myself).