"Suicide Burn" rocket problem THIS IS NOT HOMEWORK.
I'm actually in Physics I atm, and am an avid player of Kerbal Space Program. I hope to do my CS masters thesis several years from now on something space related. But none of that matters.
You're in a rocket, and one moment ago you were orbiting a nice moon (no atmosphere), magically all your horizontal velocity is gone and you are now falling directly towards the moon at the rate of g (which is technically a function of altitude).
Your rocket magically points straight up (with the engine pointing down) for you, at some point you know you want to fire the engine so you can land within some acceptable threshold so your ship doesn't explode on impact killing you.
But, you have no idea how much fuel you have, so you want to use the least amount of fuel possible, and you're smart enough to know that the longer you spend above the planet, the more fuel you're going to waste, so you want to start burning at the last second, leaving just enough time for you to get your downward velocity within that near zero margin so you don't die.
This is known as a "suicide burn."
Problem is, the moment you start burning (let's assume you either burn 100% or 0%), you immediately start increasing the time you spend above the planet, which changes when you should have started.
Let's also assume that your ship has a max thrust, which is greater than g but definitely not infinite.
So to be clear, we're looking for a time to start burning such that you land no harder than x m/s.
My question is, how would I approach such a problem?
I asked my physics professor this question and he said he'd "think on it" so I decided to ask the Internet, thanks!
 A: There's an accepted answer; I don't quite like it, so I'll take a whack.
Suppose that the start, you don't quite know the empty mass of the vehicle, the quantity of fuel in the fuel tanks, the specific impulse, or the mass flow rate. (The empty mass of the vehicle had better be a very good estimate. Otherwise we're toast.) The space has a radar altimeter that measures range and range rate. These measurements are of course, lies. They are noisy, and they might be biased.
This means we need a Kalman filter that can, over time, improve our knowledge of vehicle mass, fuel mass, mass flow rate, specific impulse, height, and velocity. This Kalman filter is the core of the spacecraft's navigation system. It only updates vehicle mass, fuel mass, mass flow rate, specific impulse when the vehicle is firing its thrusters. It updates height and velocity all of the time.
We also need a guidance system. The optimal control in this case is a bang-bang control. The vehicle falls ballistically for a bit (engines off), and then ignites the engines. The engines then fire continuously until the vehicle touches down at zero relative velocity. Off, on, off. Bang-bang.
How to tell where that magical point in time is where the thrusters change from off to on? Simple: We need a propagator. Propagate forward in time assuming the thrusters are turned on now, and remain on until the vehicle hits the ground or comes to a stop with respect to the ground. The differential equations are easy to write:
$$\begin{aligned}
\frac{dm}{dt} &= \dot m \\
F &= -\dot m u \\
\frac{dv}{dt} &= \frac F m - \frac{\mu}{(R+h)^2} \\
\frac{dh}{dt} & = v(t)
\end{aligned}$$
No realistic control system uses a pure optimal control. Optimal control leaves no room for errors, and you always need room for errors. The sensors lie, the Kalman filter lies, everything lies. We need a deadband. In this case, the deadband is given by the velocity that the vehicle and its contents (including the humans) can tolerate on contact with the ground. If the vehicle is firing its thrusters all the way to the ground, but the velocity at ground contact less then this limit at the point of ground contact, that's okay. If the vehicle reaches zero velocity at some point above the ground and then falls ballistically and hits with a velocity less than this limit, that's also okay.
There are two cases where this is not okay. One case occurs when the vehicle has to fire its thrusters all the way down to the ground and the velocity at ground contact is too high. That's crash and burn mode. Not good. The other case occurs when the zero relative velocity point is too far above the ground. Addressing that is simple: Don't start firing yet. Just stay in ballistic mode.
We need to temper the deadband a bit based on the uncertainties in the estimates of dry vehicle mass, fuel mass, mass flow rate, specific impulse, height, and velocity. When we start, we want an overly high estimate of fuel mass and overly low estimates of specific impulse and mass flow rate. This will force the guidance and control system to start firing a bit earlier than it optimally should. (Starting the burn late is not a good option. That results in crash and burn mode.) The filter will shortly arrive at better estimates of the masses, flow rate, and specific impulse. The guidance and control will make the vehicle (temporarily) stop firing. Rinse and repeat, but always be a bit on the conservative side. It's better to fire a bit early and waste some fuel than it is to fire late and enter crash and burn mode.
A: I will will assume that you fall perfectly vertical and that the celestial body does not rotate.
If you assume that the mass of the rocket stays constant, then you can find when to start with the suicide burn using time reversal. Namely you "start" st the surface with your desired final velocity, $v_f$, and thrust upwards until your (specific) orbital energy matches that of your current trajectory. You can simulate this as a function of time, however in this case it will be easier to use conservation of energy,
$$
\frac{v_f^2}{2} - \frac{\mu}{R_f} + \frac{Fh}{m} = \frac{v_i^2}{2} - \frac{\mu}{R_i},
$$
where $\mu$ is the gravitational parameter of the celestial body, $R_f$ the final radius (of the surface) relative to the center of mass of the celestial body, $h$ the altitude above the surface at which you have to start the suicide burn, $F$ the amount of thrust the rocket can provide, $m$ the mass of the rocket, $v_i$ the initial velocity of the rocket and $R_i$ the initial radius of the rocket relative to the center of mass of the celestial body.
In order to find the time, $T$, it takes to perform this burn you will have to calculate the integral,
$$
T = \int_0^h \left(v_f^2-\frac{2\mu x}{R_f(R_f+x)}+\frac{2Fx}{m}\right)^{-1/2}dx,
$$
however there is no general solution for this, so it will have to be calculated numerically. The used $\Delta v$ can be found with $\Delta v = \frac{F}{m}T$.
If you do want to take into account the variable mass of the rocket then you can also use time reversal and "start" at your final radius and velocity and go back in time, until your specific orbital energy matched that of your initial trajectory. For this you will have to initially guess what your final mass will be, such that at the start of your burn your total mass is equal to $m$.
