I am looking to solve an optimization problem, using a gradient based method. As such, I need to calculate the gradient of the objective function. The objective function, however, contains an ODE that's solved using Runge-Kutta 4th order. So I am wondering how I would analytically compute the gradient, without using finite difference.
Here's a simple example to illustrate that problem I am having. Let's assume I have an objective function that contains an ODE $$ \frac{dy}{dt} = \frac{k t^2-y}{e^{y+t}} $$ for which k is the design variable, and the objective function value is y(t=1), solved using RK4.
My problem is I am unsure of how I can solve for the gradient of the objective with respect to the design variable, namely $$ \frac{dy}{dk}(t=1) $$ I would like to solve for this analytically rather than relying on finite difference of any order.
Set $u=\frac{d y}{d k}$, then $u(0)=0$ if there is no previous dependence on $k$ and $$ \frac{d}{dt}u(t)=\frac{d}{dk}\frac{d}{dt}y=\frac{d}{dk}\frac{kt^2−y}{e^{y+t}} =\frac{t^2-u-(kt^2−y)u}{e^{y+t}} $$ so that you can compute the target value $u(1)$ by solving the coupled system.