I'm messing around a bit with using "thrust segments" on a point-mass in NDSolve and have run into a strange problem to do with using ArcTan inside NDSolve. The code below applies thrust to a point-mass for 30 seconds, and then lets it move in free-fall from then onward.
Remove["Global`*"]
g = 9.81; (*Gravitational acceleration*)
m0 = 50000; (*Initial mass*)
T = 1200000;(*Thrust*)
Isp = 300; (*Specific impulse*)
tmax = 1000;(*Maximum value for t*)
theta = 177;(*Thrust angle*)
Solution = NDSolve[{
x''[t] == If[t < 30, T/m[t] Cos[theta Degree], 0],
y''[t] == If[t < 30, T/m[t] Sin[theta Degree], 0] - g,
m'[t] == If[t < 30, -(T/(g Isp)), 0],
gamma[t] == ArcTan[x'[t], y'[t]],
x[0] == 100000, y[0] == 100000, x'[0] == 1900 Cos[70 Degree],
y'[0] == 1900 Sin[70 Degree], m[0] == m0}, {x[t], y[t], x'[t],
y'[t], m[t], gamma[t]}, {t, 0, tmax}, MaxSteps -> 1000000]
gammatable = Table[ArcTan[x'[t], y'[t]]*180/\[Pi] /. Solution, {t, 0, tmax}]
ParametricPlot[Evaluate[{x[t], y[t]} /. Solution], {t, 0, tmax},
AxesLabel -> {x, y}, PlotRange -> {{0, 900000}, {0, 300000}},
PlotStyle -> Automatic, ImageSize -> Large]
Animate[ParametricPlot[Evaluate[{x[t], y[t]} /. Solution], {t, 0, a},
AxesLabel -> {x, y}, PlotRange -> {{0, 900000}, {0, 300000}},
PlotStyle -> Automatic, ImageSize -> Large], {a, 0, tmax},
AnimationRate -> 5, AnimationRepetitions -> 1]
As can be seen, a term called gamma is calculated inside NDSolve, which is just the flight-path angle of the point mass, gamma[t] == ArcTan[x'[t], y'[t]]. It seems that when the y-velocity of the particle becomes zero (at the top of the free-fall arc), the calculation of gamma goes haywire and the subsequent plot and animation "break". Looking at the values that gamma attains at the point where the y-velocity is zero (produced using gammatable), we see that the value of ArcTan is very close to zero. However, from then onward the value stays close to zero, even though the particle gains negative y-velocity. As such, I'm wondering if there's a way to prevent this from happening so that an accurate value for gamma is given at each point in time from t = 0 to t = tmax. I tried using the following gamma[t] == If[y'[t] < Abs[0.01], \[Pi], ArcTan[x'[t], y'[t]]], which seemed to fix the problem. However this quick-fix is less than ideal and I was hoping there there is a cleaner way using no tricks to fix the problem. Any help would be great, thanks!




