This is a real can of worms. It's a system of 2 coupled differential equations, which are only numerically solvable. There's no way to find a reusable algorithm to solve for the ideal angle, each solution is going to be specific to a particular rocket. Wikipedia has the two basic equations of motions listed on its Gravity Turn page. (I'm new here, what's the best way to write/post equations?) There's also a useful paper, "Universal Gravity Turn Trajectories," from 1957, that describes in detail how to do this, but...it's confusing. I think I'd be able to crack through the math, but that would be a project for next week. One other thing: T:W ratio is actually lowest at launch. Both the mass of the rocket (due to losing fuel) and the gravitational acceleration drop off as altitude increases, losing to a drastic reduction in weight--thrust remains constant. It would probably be most efficient to pull back on the throttle as altitude increases, but since the fuel flow is currently bugged, this is 'cheating.' Anyway, the non-constant T:W ratio is the primary difficulty in solving the equations above.