Skip to content

Commit

Permalink
Update rocket_control.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Aug 29, 2023
1 parent f270c85 commit 6c970c1
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions docs/src/tutorials/nonlinear/rocket_control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import Plots

# * Thrust: $u_t(t)$.

# Our goal is thus to maximize $x_h(T)$.

# There are three equations that control the dynamics of the rocket:

# * Rate of ascent: $$\frac{d x_h}{dt} = x_v$$
Expand All @@ -57,23 +55,25 @@ import Plots

# We use a discretized model of time, with a fixed number of time steps, $T$.

# ## Constants
# Our goal is thus to maximize $x_h(T)$.

# ## Data

# All parameters in this model have been normalized to be dimensionless, and
# they are taken from [COPS3](https://www.mcs.anl.gov/~more/cops/cops3.pdf).

h_0 = 1 # Initial height
v_0 = 0 # Initial velocity
m_0 = 1.0 # Initial mass
m_f = 0.6 # Final mass
m_T = 0.6 # Final mass
g_0 = 1 # Gravity at the surface
h_c = 500 # Used for drag
c = 0.5 * sqrt(g_0 * h_0) # Thrust-to-fuel mass
D_c = 0.5 * 620 * m_0 / g_0 # Drag scaling
u_t_max = 3.5 * g_0 * m_0 # Maximum thrust
T_max = 0.2 # Number of seconds
T = 1_000 # Number of time steps
Δt = 0.2 / T # Time per discretized step
Δt = 0.2 / T; # Time per discretized step

# ## JuMP formulation

Expand All @@ -88,10 +88,10 @@ set_silent(model)
# `t`. It is good practice for nonlinear programs to always provide a starting
# solution for each variable.

@variable(model, 0 <= x_v[1:T], start = v_0) # Velocity
@variable(model, 0 <= x_h[1:T], start = h_0) # Height
@variable(model, m_f <= x_m[1:T] <= m_0, start = m_0) # Mass
@variable(model, 0 <= u_t[1:T] <= u_t_max, start = 0) # Thrust
@variable(model, x_v[1:T] >= 0, start = v_0) # Velocity
@variable(model, x_h[1:T] >= 0, start = h_0) # Height
@variable(model, x_m[1:T] >= m_T, start = m_0) # Mass
@variable(model, 0 <= u_t[1:T] <= u_t_max, start = 0); # Thrust

# We implement boundary conditions by fixing variables to values.

Expand All @@ -111,7 +111,7 @@ fix(u_t[T], 0.0; force = true)
D[t = 1:T],
D_c * x_v[t]^2 * exp(-h_c * (x_h[t] - h_0) / h_0),
)
@NLexpression(model, g[t = 1:T], g_0 * (h_0 / x_h[t])^2)
@NLexpression(model, g[t = 1:T], g_0 * (h_0 / x_h[t])^2);

# The dynamical equations are implemented as constraints.

Expand All @@ -121,7 +121,7 @@ fix(u_t[T], 0.0; force = true)
[t in 2:T],
(x_v[t] - x_v[t-1]) / Δt == (u_t[t-1] - D[t-1]) / x_m[t-1] - g[t-1],
)
@NLconstraint(model, [t in 2:T], (x_m[t] - x_m[t-1]) / Δt == -u_t[t-1] / c)
@NLconstraint(model, [t in 2:T], (x_m[t] - x_m[t-1]) / Δt == -u_t[t-1] / c);

# Now we optimize the model and check that we found a solution:

Expand Down

0 comments on commit 6c970c1

Please sign in to comment.