I'm trying to find an optimal control to a simple nonlinear SIR model. I am trying to undersand the Pontryagin minimum principle but I don't understand why the costate equations must be solved backwards in time?
Another question must be why when using Euler method to find the optimal control $u^*$, we implement $u^*$ forward in time and the costate variables (adjoint variables) are implemented backwards in time while $u^*$ is expressed in function of them?
For example, if a $$u^*(t) = \left( p_1^*(t) - p_2^*(t) \right) x^*(t)$$ is the optimal control for an arbitrary system where $p1^*$ and $p2^*$ are the optimal costate variable and $x^*(t)$ is an optimal state variable. The implementation would look like:
u(i+1) = (p1(N-i) - p2(N-i))*x_star(i+1);
Concerning the theoretical part, I wouldn't say that the costate equations must be solved backwards. To take a step back, in the usual setting for the Lagrange problem, $x(0)$ is fixed and $x(T)$ is free. In this setting, for the costate, $p(0)$ is free and $p(T) = 0$. But for slightly different settings where part of the initial state $x(0)$ and part of the final state $x(T)$ are constrained (known) and others free (unknown), you would have complementary conditions on $p(0)$ and $p(T)$. This comes from the proof of the maximal principle and the "dual" nature of $p$.
At the heuristic level, you can somehow understand it. In Pontryagin's maximum principle, the costate variable is here precisely to help you transform a local-in-time maximization criterion into something that will turn out to be optimal for the whole time domain. In some sense, it thus embeds information about the future, i.e. $p(t)$ should contain information about what will happen on $[t,T]$.
Now for the numerical simulation,