Optimization in Dynamical Systems: Theory and Application