Matrix methods for solving nonlinear dynamic optimization models