Skip to content

Latest commit

 

History

History
69 lines (51 loc) · 1.82 KB

tutorial-basic-example.md

File metadata and controls

69 lines (51 loc) · 1.82 KB

[Basic example](@id basic)

Let us consider a wagon moving along a rail, whom acceleration can be controlled by a force $u$. We denote by $x = (x_1, x_2)$ the state of the wagon, that is its position $x_1$ and its velocity $x_2$.

<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

$$\dot x_1(t) = x_2(t), \quad \dot x_2(t) = u(t), , \quad u(t) \in \R,$$

which is simply the double integrator system. Les us consider a transfer starting at time $t_0 = 0$ and ending at time $t_f = 1$, for which we want to minimise the transfer energy

$$\frac{1}{2}\int_{0}^{1} u^2(t) \, \mathrm{d}t$$

starting from the condition $x(0) = (-1, 0)$ and with the goal to reach the target $x(1) = (0, 0)$.

!!! 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

@def ocp begin
    t ∈ [ 0, 1 ], time
    x ∈ R², state
    u ∈ R, control
    x(0) == [ -1, 0 ]
    x(1) == [ 0, 0 ]
    ẋ(t) == [ x₂(t), u(t) ]
    ∫( 0.5u(t)^2 ) → min
end
nothing # hide

Solve it

sol = solve(ocp)
nothing # hide

and plot the solution

plot(sol, size=(600, 450))