Let us consider a wagon moving along a rail, whom acceleration can be controlled by a force
<img src="./assets/chariot.png" style="display: block; margin: 0 auto 20px auto;" width="300px">
We assume that the mass is constant and unitary and that there is no friction. The dynamics we consider is given by
which is simply the double integrator system.
Les us consider a transfer starting at time
starting from the condition
!!! note "Solution and details"
See the page
[Double integrator: energy minimisation](https://control-toolbox.org/docs/ctproblems/stable/problems/double_integrator_energy.html#DIE)
for the analytical solution and details about this problem.
using Plots
using Plots.PlotMeasures
plot(args...; kwargs...) = Plots.plot(args...; kwargs..., leftmargin=25px)
First, we need to import the OptimalControl.jl
package:
using OptimalControl
Then, we can define the problem
ocp = Model() # empty optimal control problem
time!(ocp, [ 0, 1 ]) # time interval
state!(ocp, 2) # dimension of the state
control!(ocp, 1) # dimension of the control
constraint!(ocp, :initial, [ -1, 0 ]) # initial condition
constraint!(ocp, :final, [ 0, 0 ]) # final condition
dynamics!(ocp, (x, u) -> [ x[2], u ]) # dynamics of the double integrator
objective!(ocp, :lagrange, (x, u) -> 0.5u^2) # cost in Lagrange form
nothing # hide
!!! note "Nota bene"
There are two ways to define an optimal control problem:
- using functions like in this example, see also the [`Model` documentation](https://control-toolbox.org/docs/ctbase/stable/api-model.html) for more details.
- using an abstract formulation. You can compare both ways taking a look at the abstract version of this [basic example](@ref basic).
Solve it
sol = solve(ocp)
nothing # hide
and plot the solution
plot(sol, size=(600, 450))