Magnetohydrodynamic simulations of the ejection of a magnetic flux rope