Numerical solution of the variational PDEs arising in optimal control theory