An algorithm for numerical synthesis of optimal control