one should always do the ODE first, then calculate the residual. Not the other way around.
adding one more input argument to the calc_residual function.

Make it very clear that always use the xi_Kin_bar_old values as the starting values of ODE.
the variable n_unknowns is not necessary at all.
code cleaning. Remove a lot of redundent operations.
code cleaning.