Nonlinear optimal control for multilevel inverters