% function uout = MPCController(currentx,currentr,t)function uout = MPCController(t, inXY, oX )
persistent Controller
if t == 0 % Compute discrete-time dynamics % MIMO % 3 in 6 out % Numerator = {[1 -1 0], 1, 1; 1, [1 -1 0], 1; 1, 1, [1 -1 0] }; % Denominator = {[1 2 0], 1, 1; 1, [1 -1 0], 1; 1, 1, [1 -1 0] };
% 2 in 2 out Numerator = {[1 -1], 2; [1 0], 1}; Denominator = {[1 2], 2; [1 0], 1};
H = tf(Numerator,Denominator) ;
% Plant = ss(tf(1,[1 0 0])); Plant = ss(H);
A = Plant.A; B = Plant.B; C = Plant.C; D = Plant.D; Ts = 0.1; Gd = c2d(Plant,Ts); Ad = Gd.A; Bd = Gd.B; % Define data for MPC controller N = 10; Q = 10; R = 0.1; % Avoid explosion of internally defined variables in YALMIP yalmip('clear') % Setup the optimization problem % u = sdpvar(repmat(1,1,N),repmat(1,1,N)); u1 = sdpvar(repmat(1,1,N),repmat(1,1,N)); u2 = sdpvar(repmat(1,1,N),repmat(1,1,N)); % x = sdpvar(repmat(2,1,N+1),repmat(1,1,N+1)); x1 = sdpvar(repmat(2,1,N+1),repmat(1,1,N+1)); x2 = sdpvar(repmat(2,1,N+1),repmat(1,1,N+1)); % sdpvar r sdpvar r1 sdpvar r2 % Define simple standard MPC controller % Current state is known so we replace this constraints = []; objective = 0; for k = 1:N %objective = objective + (r-C*x{k})'*Q*(r-C*x{k})+u{k}'*R*u{k}; objective = objective + (r1-C*x1{k})'*Q*(r1-C*x1{k})+u1{k}'*R*u1{k}; objective = objective + (r2-C*x2{k})'*Q*(r2-C*x2{k})+u2{k}'*R*u2{k}; %constraints = [constraints, x{k+1} == Ad*x{k}+Bd*u{k}]; constraints = [constraints, -5 <= u1{k}<= 5]; constraints = [constraints, -5 <= u2{k}<= 5]; end % Define an optimizer object which solves the problem for a particular % initial state and reference % Controller = optimizer(constraints,objective,[],{x{1},r},u{1}); inXY_2optimizer = [{x1{1},r1}, {x2{1},r2}] ; oX_2optimizer = [u1{1}, u2{1}] ; Controller = optimizer(constraints, objective, [], inXY_2optimizer, oX_2optimizer ); % And use it here too % uout = Controller{{currentx, currentr}}; % uout = Controller{{inX, inY, currentx}}; uout = Controller{{inXY', oX'}}; else % Almost no overhead % uout = Controller{{currentx, currentr}}; % uout = Controller{{inX, inY, currentx}}; uout = Controller{{inXY, oX}};end Simulation 1 Clear Save4:16:05 PM Apr 6, 2017 Elapsed: 0.886 sec
An error occurred while running the simulation and the simulation was terminated
Error due to multiple causes.
The number of cell elements in input does not match OPTIMIZER declaration
Error in 'MPCSimulation/MPC1' while evaluating expression.Component: Simulink | Category: Model errorA = [0 1;0 -1] ;
B = [1 0;0 1] ;
C = [0 1;1 0] ;
D = [0 0;0 0] ;
Plant = ss (A,B,C,D) ;
% Global sampling-time
Ts = 0.1;
% Initial state for simulation
x0 = [1;1];function uout = MPCController(currentx,currentr,t)
persistent Controller
if t == 0
A = [0 1;0 -1] ;
B = [1 0;0 1] ; % B = sdpvar(2,2); % I tried that too, from one of examples, just in case C = [0 1;1 0] ; D = [0 0;0 0] ;
Plant = ss (A,B,C,D) ;
Ts = 0.1; Gd = c2d(Plant,Ts); Ad = Gd.A; Bd = Gd.B; % Define data for MPC controller N = 10; Q = 10; R = 0.1; % Avoid explosion of internally defined variables in YALMIP yalmip('clear') nu = 2; % Number of inputs nx = 2; % Number of states ny = 2; % Number of outputs ? % Setup the optimization problem u = sdpvar(repmat(nu,1,N),repmat(1,1,N)); x = sdpvar(repmat(nx,1,N+1),repmat(1,1,N+1));
% tried differen definitions of r, but either failing with some errors
r = sdpvar( repmat(ny,1,N+1), repmat(1,1,N+1) ); % example % r = sdpvar( repmat(ny,1,N+1) ) % r = sdpvar( repmat(ny,1,N) ) % Define simple standard MPC controller % Current state is known so we replace this constraints = []; objective = 0; for k = 1:N objective = objective + (r{k}-C*x{k})'*Q*(r{k}-C*x{k})+u{k}'*R*u{k}; constraints = [constraints, x{k+1} == Ad*x{k}+Bd*u{k}]; constraints = [constraints, -5 <= u{k}<= 5]; end parameters_in = {x{:},r{:}} ; solutions_out = {u{:}}; % Define an optimizer object which solves the problem for a particular % initial state and reference
ops = sdpsettings('verbose',2); Controller = optimizer(constraints, objective, ops, parameters_in, solutions_out ); % And use it here too uout = Controller{{currentx,currentr}}; else % Almost no overhead uout = Controller{{currentx,currentr}};endAn error occurred while running the simulation and the simulation was terminated
Caused by:Error due to multiple causes.The number of cell elements in input does not match OPTIMIZER declarationError in 'MPCSimulationV4/MPC yalmipsimulinkAdapt5' while evaluating expression.Component: Simulink | Category: Model error