A numerical simulation of magnetically driven coronal mass ejections