Changed line 218 from:
Simulate the performance of the explicit controller in 20 samples and plot the closed loop trajectories
to:
Simulate the performance of the explicit controller in 20 samples
Deleted lines 226-227:
subplot(2, 1, 1); plot(1:Nsim, data.X(1:Nsim)); title('state')
subplot(2, 1, 2); plot(1:Nsim, data.U); title('input')
Changed lines 228-233 from:
[[#integrator]]
!!! Tracking with builtin integrator
The tracking of a time-varying reference can be achieved by augmenting the state with an integrator state that predicts the steady state values. In MPT this can be achieved with the help of "integrator" filter that extends the model with an integrator state.
Consider the unstable LTI system as in the previous example that operates under input and state constraints
to:
and plot the closed loop trajectories
Changed lines 230-234 from:
model = LTISystem('A', 1.5 , 'B', 1 );
model.x.min = -10;
model.x.max = 10;
model.u.min = -5;
model.u.max = 5;
to:
subplot(2, 1, 1); hold on;
plot(1:Nsim, data.X(1:Nsim), 'linewidth', 2 );
plot(1:Nsim, model.x.max*ones(1,Nsim), 'k--', 'linewidth', 2);
plot(1:Nsim, model.x.min*ones(1,Nsim), 'k--', 'linewidth', 2);
stairs(1:Nsim, xref, 'r--', 'linewidth', 2);
axis([1, Nsim, -11, 11]);
title('state')
subplot(2, 1, 2); hold on;
stairs(1:Nsim, data.U, 'linewidth', 2);
plot(1:Nsim, model.u.max*ones(1, Nsim), 'k--', 'linewidth', 2);
plot(1:Nsim, model.u.min*ones(1, Nsim), 'k--', 'linewidth', 2);
axis([1, Nsim, -6, 6]);
title('input')
Changed lines 244-249 from:
Activate the time-varying reference
to:
%width=500% Attach:trackdu.jpg %%
[[#integrator]]
!!! Tracking with builtin integrator
The tracking of a time-varying reference can be achieved by augmenting the state with an integrator state that predicts the steady state values. In MPT this can be achieved with the help of "integrator" filter that extends the model with an integrator state.
Consider the unstable LTI system as in the previous example that operates under input and state constraints
Changed lines 251-252 from:
model.x.with('reference');
model.x.reference = 'free';
to:
model = LTISystem('A', 1.5 , 'B', 1 );
model.x.min = -10;
model.x.max = 10;
model.u.min = -5;
model.u.max = 5;
Changed line 257 from:
To add the integrator, one need to active the "integrator" filter as follows.
to:
Activate the time-varying reference
Changed lines 259-260 from:
model.with('integrator');
to:
model.x.with('reference');
model.x.reference = 'free';
Changed line 262 from:
In the remainder of MPC set, the penalty on the predicted difference of the integrator state and the reference is provided
to:
To add the integrator, one need to active the "integrator" filter as follows.
Changed line 264 from:
model.x.penalty = QuadFunction( 5 );
to:
model.with('integrator');
Changed line 266 from:
as well as the penalty on the inputs
to:
In the remainder of MPC set, the penalty on the predicted difference of the integrator state and the reference is provided
Changed line 268 from:
model.u.penalty = QuadFunction( 1 );
to:
model.x.penalty = QuadFunction( 5 );
Changed line 270 from:
The resulting controller is exported to an explicit form
to:
as well as the penalty on the inputs
Changed lines 272-273 from:
ctrl = MPCController(model, 4);
ectrl = ctrl.toExplicit();
to:
model.u.penalty = QuadFunction( 1 );
Changed line 274 from:
and a closed form is created with the help of @@ClosedLoop@@ object:
to:
The resulting controller is exported to an explicit form
Changed lines 276-277 from:
loop = ClosedLoop( ectrl, model);
to:
ctrl = MPCController(model, 4);
ectrl = ctrl.toExplicit();
Changed line 279 from:
The simulation results are the same as in the previous example, but this time obtained with the integrator state
to:
and a closed form is created with the help of @@ClosedLoop@@ object:
Added lines 281-284:
loop = ClosedLoop( ectrl, model);
@]
The simulation results are the same as in the previous example, but this time obtained with the integrator state
(:source lang=MATLAB -getcode:) [@
Deleted lines 291-292:
subplot(2, 1, 1); plot(1:Nsim, data.X(1:Nsim) ); title('state')
subplot(2, 1, 2); plot(1:Nsim, data.U); title('input')
Changed lines 293-308 from:
to:
The simulated data can be plotted together with the reference signal and constraints
(:source lang=MATLAB -getcode:) [@
subplot(2, 1, 1); hold on;
plot(1:Nsim, data.X(1:Nsim), 'linewidth', 2 );
plot(1:Nsim, model.x.max*ones(1,Nsim), 'k--', 'linewidth', 2);
plot(1:Nsim, model.x.min*ones(1,Nsim), 'k--', 'linewidth', 2);
stairs(1:Nsim, xref, 'r--', 'linewidth', 2);
axis([1, Nsim, -11, 11]);
title('state')
subplot(2, 1, 2); hold on;
stairs(1:Nsim, data.U, 'linewidth', 2);
plot(1:Nsim, model.u.max*ones(1, Nsim), 'k--', 'linewidth', 2);
plot(1:Nsim, model.u.min*ones(1, Nsim), 'k--', 'linewidth', 2);
axis([1, Nsim, -6, 6]);
title('input')
@]
Changed line 49 from:
subplot(2, 1, 2); plot(1:30, data.U, 'linewidth', 2);
to:
subplot(2, 1, 2); plot(1:Nsim, data.U, 'linewidth', 2);
Changed lines 63-64 from:
subplot(2, 1, 1); plot(1:31, data.X); title('states')
subplot(2, 1, 2); plot(1:30, data.U); title('inputs')
to:
subplot(2, 1, 1); hold on; grid on;
plot(1:Nsim, data.X(:,1:Nsim), 'linewidth', 2);
stairs(1:Nsim, xref(2,:), 'k--', 'linewidth', 2);
axis([1, Nsim, -3, 4]);
title('states')
subplot(2, 1, 2); hold on; grid on;
plot(1:Nsim, data.U, 'linewidth', 2);
axis([1, Nsim, -1, 1]);
title('inputs')
Changed lines 136-137 from:
subplot(2, 1, 1); plot(1:Nsim, data.Y); title('output')
subplot(2, 1, 2); stairs(1:Nsim, data.U); title('input')
to:
subplot(2, 1, 1); plot(1:Nsim, data.Y, 'linewidth', 2);
hold on; grid on;
plot(1:Nsim, model.y.max*ones(1, Nsim), 'k--', 'linewidth', 2);
plot(1:Nsim, model.y.min*ones(1, Nsim), 'k--', 'linewidth', 2);
stairs(1:Nsim, yref, 'r--', 'linewidth', 2);
axis([1, Nsim, -12, 12]);
title('output')
subplot(2, 1, 2); stairs(1:Nsim, data.U, 'linewidth', 2);
hold on; grid on;
plot(1:Nsim, model.u.max*ones(1, Nsim), 'k--', 'linewidth', 2);
plot(1:Nsim, model.u.min*ones(1, Nsim), 'k--', 'linewidth', 2);
axis([1, Nsim, -4, 4]);
title('input')
Changed line 116 from:
The closed-loop simulation can be evaluated using the @@ClosedLoop@@ object
to:
Changed line 118 from:
loop = ClosedLoop(ectrl, model);
to:
Changed lines 120-121 from:
by providing the time varying reference signal @@yref@@
to:
%width=500% Attach:trackypartition.jpg %%
The closed-loop simulation can be evaluated using the @@ClosedLoop@@ object
Added lines 123-126:
loop = ClosedLoop(ectrl, model);
@]
by providing the time varying reference signal @@yref@@
(:source lang=MATLAB -getcode:) [@
Changed line 137 from:
subplot(2, 1, 2); plot(1:Nsim, data.U); title('input')
to:
subplot(2, 1, 2); stairs(1:Nsim, data.U); title('input')
Added line 139:
%width=500% Attach:tracky1.jpg %%
Changed line 157 from:
subplot(2, 1, 2); plot(1:40, U); title('input')
to:
subplot(2, 1, 2); stairs(1:40, U); title('input')
Changed line 187 from:
to:
Deleted line 189:
xref = [repmat( xref1, 1, 10), repmat(xref2, 1, 10)];
Changed lines 191-192 from:
data = loop.simulate(x0, Nsim, 'u.previous', u, 'x.reference', xref);
to:
xref = [repmat( xref1, 1, Nsim/2), repmat(xref2, 1, Nsim/2)];
data = loop.simulate(x0, Nsim, 'u.previous', u0, 'x.reference', xref);
Changed line 231 from:
The simulation results are the same as in the previous example, but this time obtained with the integrator state
to:
and a closed form is created with the help of @@ClosedLoop@@ object:
Changed lines 233-249 from:
x0 = 0;
u = 0;
X = []; U = [];
model.initialize( x0 );
for i=1:20
if i<10
xref = 5;
else
xref = -3;
end
X = [X, x0];
u = ectrl.evaluate(x0, 'u.previous', u, 'x.reference', xref);
U = [U, u];
x0 = model.update(u);
end
subplot(2, 1, 1); plot(1:20, X); title('state')
subplot(2, 1, 2); plot(1:20, U); title('inputs')
to:
loop = ClosedLoop( ectrl, model);
Changed lines 235-246 from:
to:
The simulation results are the same as in the previous example, but this time obtained with the integrator state
(:source lang=MATLAB -getcode:) [@
x0 = 0;
u0 = 0;
xref1 = 5;
xref2 = -3;
Nsim = 20;
xref = [repmat( xref1, 1, Nsim/2), repmat(xref2, 1, Nsim/2)];
data = loop.simulate(x0, Nsim, 'u.previous', u0, 'x.reference', xref);
subplot(2, 1, 1); plot(1:Nsim, data.X(1:Nsim) ); title('state')
subplot(2, 1, 2); plot(1:Nsim, data.U); title('input')
@]
Changed line 180 from:
Simulate the performance of the explicit controller in 20 samples
to:
For simulation purposes, the @@ClosedLoop@@ object is created with the explicit controller and the same model that was used for MPC design
Changed lines 182-186 from:
to:
loop = ClosedLoop( ectrl, model);
@]
Simulate the performance of the explicit controller in 20 samples and plot the closed loop trajectories
(:source lang=MATLAB -getcode:) [@
x0 = 0;
Changed lines 188-202 from:
X = []; U = [];
model.initialize(x0);
for i=1:20
if i<10
xref = 5;
else
xref = -3;
end
X = [X, x0];
u = ectrl.evaluate(x0, 'u.previous', u, 'x.reference', xref);
U = [U, u];
x0 = model.update(u);
end
subplot(2, 1, 1); plot(1:20, X); title('state')
subplot(2, 1, 2); plot(1:20, U); title('inputs')
to:
xref1 = 5;
xref2 = -3;
xref = [repmat( xref1, 1, 10), repmat(xref2, 1, 10)];
Nsim = 20;
data = loop.simulate(x0, Nsim, 'u.previous', u, 'x.reference', xref);
subplot(2, 1, 1); plot(1:Nsim, data.X(1:Nsim)); title('state')
subplot(2, 1, 2); plot(1:Nsim, data.U); title('input')
Deleted line 195:
Changed lines 103-104 from:
model.y.penalty = OneNormFunction(3);
model.u.penalty = OneNormFunction(0.5);
to:
model.y.penalty = OneNormFunction( 3 );
model.u.penalty = OneNormFunction( 0.5 );
Changed line 108 from:
ctrl = MPCController(model, 3)
to:
ctrl = MPCController(model, 3);
Changed lines 130-131 from:
subplot(2,1,1); plot(1:40,Y); title('output')
subplot(2,1,2); plot(1:40,U); title('inputs')
to:
subplot(2, 1, 1); plot(1:40, Y); title('output')
subplot(2, 1, 2); plot(1:40, U); title('inputs')
Changed lines 179-180 from:
subplot(2,1,1); plot(1:20,X); title('state')
subplot(2,1,2); plot(1:20,U); title('inputs')
to:
subplot(2, 1, 1); plot(1:20, X); title('state')
subplot(2, 1, 2); plot(1:20, U); title('inputs')
Changed line 223 from:
to:
Changed lines 235-236 from:
subplot(2,1,1); plot(1:20,X); title('state')
subplot(2,1,2); plot(1:20,U); title('inputs')
to:
subplot(2, 1, 1); plot(1:20, X); title('state')
subplot(2, 1, 2); plot(1:20, U); title('inputs')
Changed lines 130-133 from:
subplot(2,1,1);
plot(1:40,Y); title('output')
subplot(2,1,2);
plot(1:40,U); title('inputs')
to:
subplot(2,1,1); plot(1:40,Y); title('output')
subplot(2,1,2); plot(1:40,U); title('inputs')
Changed lines 179-182 from:
subplot(2,1,1);
plot(1:20,X); title('state')
subplot(2,1,2);
plot(1:20,U); title('inputs')
to:
subplot(2,1,1); plot(1:20,X); title('state')
subplot(2,1,2); plot(1:20,U); title('inputs')
Changed line 216 from:
to:
ectrl = ctrl.toExplicit();
Changed lines 235-238 from:
subplot(2,1,1);
plot(1:20,X); title('state')
subplot(2,1,2);
plot(1:20,U); title('inputs')
to:
subplot(2,1,1); plot(1:20,X); title('state')
subplot(2,1,2); plot(1:20,U); title('inputs')
Changed lines 28-29 from:
model.x.penalty = OneNormFunction(diag([0.5, 1, 0]) );
model.u.penalty = OneNormFunction(eye(2));
to:
model.x.penalty = OneNormFunction( diag([0.5, 1, 0]) );
model.u.penalty = OneNormFunction( eye(2) );
Changed line 37 from:
cl = ClosedLoop(ctrl,model);
to:
loop = ClosedLoop(ctrl, model);
Changed line 41 from:
data = cl.simulate(x0, Nsim, 'x.reference', xref)
to:
data = loop.simulate(x0, Nsim, 'x.reference', xref)
Changed line 53 from:
data = cl.simulate(x0, Nsim, 'x.reference', xref);
to:
data = loop.simulate(x0, Nsim, 'x.reference', xref);
Changed lines 75-78 from:
subplot(2,1,1);
plot(1:30,X); title('states')
subplot(2,1,2);
plot(1:30,U); title('inputs')
to:
subplot(2,1,1); plot(1:30,X); title('states')
subplot(2,1,2); plot(1:30,U); title('inputs')
Changed line 18 from:
to:
Changed lines 41-45 from:
d = cl.simulate(x0, Nsim, 'x.reference', xref)
subplot(2,1,1);
plot(1:31,d.X); title('states')
subplot(2,1,2);
plot(1:30,d.U); title('inputs')
to:
data = cl.simulate(x0, Nsim, 'x.reference', xref)
subplot(2, 1, 1); plot(1:31, data.X); title('states')
subplot(2,1,2); plot(1:30, data.U); title('inputs')
Changed lines 53-57 from:
d = cl.simulate(x0, Nsim, 'x.reference', xref);
subplot(2,1,1);
plot(1:31,d.X); title('states')
subplot(2,1,2);
plot(1:30,d.U); title('inputs')
to:
data = cl.simulate(x0, Nsim, 'x.reference', xref);
subplot(2,1,1); plot(1:31, data.X); title('states')
subplot(2,1,2); plot(1:30, data.U); title('inputs')
Changed lines 38-41 from:
d = cl.simulate(zeros(3,1),30,'x.reference',[0; 1; 0])
to:
x0 = [0; 0; 0];
Nsim = 30;
xref = [0; 1; 0];
d = cl.simulate(x0, Nsim, 'x.reference', xref)
Changed line 47 from:
For a time-varying referece, the simulation is decomposed into a for-loop and the reference is changing step-wise after 10 samples
to:
The @@simulate@@ method also allows to provide time-varying profiles of references, e.g.
Added lines 49-62:
xref1 = [0; 1; 0];
xref2 = [0; 2; 0];
xref3 = [0; 3; 0];
xref = [repmat(xref1, 1, 10), repmat(xref2, 1, 10), repmat(xref3, 1, 10)];
x0 = [0; 0; 0];
Nsim = 30;
d = cl.simulate(x0, Nsim, 'x.reference', xref);
subplot(2,1,1);
plot(1:31,d.X); title('states')
subplot(2,1,2);
plot(1:30,d.U); title('inputs')
@]
The same result can be obtained manually using a for-loop as follows:
(:source lang=MATLAB -getcode:) [@
Changed line 69 from:
to:
Changed line 71 from:
to:
Changed line 73 from:
to:
Changed line 75 from:
u = ctrl.evaluate(x0, 'x.reference', yref);
to:
u = ctrl.evaluate(x0, 'x.reference', xref);
Changed line 118 from:
to:
ectrl = ctrl.toExplicit();
Changed line 168 from:
to:
ectrl = ctrl.toExplicit();
Changed lines 69-70 from:
!!!! Input and output tracking
to:
!!!! Output tracking
Consider a simple PWA system that comprises of two modes and has one output
(:source lang=MATLAB -getcode:) [@
mode1 = LTISystem('A', [ 0.4 0.69; -0.69 0.4], 'B', [0;1], 'C', [1, 0]);
mode1.setDomain('x', Polyhedron('A', [1 0], 'b', 0) );
mode2 = LTISystem('A', [ 0.4 -0.69; 0.69 0.4], 'B', [0;1], 'C', [1, 0]);
mode2.setDomain('x', Polyhedron('A', [-1 0], 'b', 0) );
model = PWASystem([mode1, mode2]);
@]
The model is subject to input/output constraints
(:source lang=MATLAB -getcode:) [@
model.u.min = -3;
model.u.max = 3;
model.y.min = -10;
model.y.max = 10;
@]
The objective is to track the time varying output while satisfying the constraints. To incorporate a time varying signal one needs to active the appropriate reference filter and mark it as "free"
(:source lang=MATLAB -getcode:) [@
model.y.with('reference');
model.y.reference = 'free';
@]
To finalize the MPC setup, penalty on inputs and outputs are provided
(:source lang=MATLAB -getcode:) [@
model.y.penalty = OneNormFunction(3);
model.u.penalty = OneNormFunction(0.5);
@]
and the online @@MPCController@@ object is constructed with horizon 3
(:source lang=MATLAB -getcode:) [@
ctrl = MPCController(model, 3)
@]
After testing the performance of the online-controller, one can export it to the explicit form as follows:
(:source lang=MATLAB -getcode:) [@
ectrl = ctrl.toExplicit;
@]
The following simulation shows step-wise changes in the output reference over 30 samples that is obtained by evaluating the explicit controller
(:source lang=MATLAB -getcode:) [@
x0 = zeros(2,1);
Y = []; U = [];
model.initialize(x0);
for i=1:40
if i<20
yref = 1;
else
yref = -2;
end
u = ectrl.evaluate(x0, 'y.reference', yref);
U = [U, u];
[x0, y] = model.update(u);
Y = [Y, y];
end
subplot(2,1,1);
plot(1:40,Y); title('output')
subplot(2,1,2);
plot(1:40,U); title('inputs')
@]