rem: Closed Loop Control for a Sliding Mass System
Find \(u\) over \(t \in [0,t_f]\) to minimize \[ J = \int_0^{t_f} (y_1^2 + y_2^2 + u^2) \ \mathrm{d}t \] subject to: \[ \dot{y}_1 = y_2 \\ \dot{y}_2 = u - y_2 |y_2| \\ y_1(0) = 2, \ \ y_2(0) = -2 \]
par: # declare parameters: here the final time
tf = 1
dyn: # declare the dynamic variables
y{1:2}
J u
t=t0: # set the initial conditions
y{1:2} = {2 -2}
J = 0
equ: # define the equations
y1´ == y2 # character ´ (hexadecimal B4, decimal 180) denotes the derivative
y2´ == u - y2*y2*sign(y2)
J´ == 0.5 * (sqr(y1) + sqr(y2) + sqr(u))
obj: # set the objective and select the solver to use (in this case a discontinuous NLP solver)
minimize final(J) using dnlp