An optimal control framework for estimating autopilot safety margins