
A tough problem with interplanetary trajectories is integrating the equations of motion near Mars and Earth. You run into a “singularity,” and the solution blows up. My advisor, Dr. Roger Broucke, suggested one solution (PDF) to this problem – and in fact published a paper on his research right after I was expelled.
I didn’t want to solve one arcane problem. I wanted to get to Mars. I solved the problem by setting up a “trap” that stopped the iteration if it went past the target planet and iterated until the spacecraft ended up being drawn into an orbit around the planet. Here’s what it looks like:

I called the subroutine HAWKEYEM (for Mars – Hawkeye is the aircraft carrier term for the tail hook for jets used to land on the flight deck).
I used a similar method to integrate close to Earth. It was trickier because Earth is much more massive, but I got it to work fine.
With these two optimization routines, I was able to generate a seamless, start-to-finish closed solution to the Mars trajectory using every input you provide.