www.gusucode.com > mpc_featured 案例源码 matlab代码程序 > mpc_featured/mpcsequence.m

    %% Understanding Control Behavior by Examining Optimal Control Sequence
% This example shows how to inspect the optimized sequence of manipulated
% variables computed by model predictive controller at each sample time.
%
% The plant is a double integrator subject to input saturation.

% Copyright 1990-2014 The MathWorks, Inc.

%% Design MPC Controller
%
% The basic setup of the MPC controller includes: 
%
% * A double integrator as the prediction model
%
% * Prediction horizon of 20
%
% * Control horizon of 10
%
% * Input constraints |-1 <= u(t) <= 1|
%
% Define the MPC controller setup:
Ts = 0.1;                                       % Sampling time
p = 20;                                         % Prediction horizon
m = 10;                                         % Control horizon
mpcobj = mpc(tf(1,[1 0 0]),Ts,p,m);     % MPC object
mpcobj.MV = struct('Min',-1,'Max',1);   % Input saturation constraints
nu=1;                                           % Number of manipulated variables

%% Simulate Model in Simulink(R)
% To run this example, Simulink(R) is required.
if ~mpcchecktoolboxinstalled('simulink')
    disp('Simulink(R) is required to run this example.')
    return
end
%% 
% Open the Simulink(R) model. You see an outport |mv.seq| in the MPC
% Controller block. Selecting *Enable control sequence outport* option in
% the MPC Controller Block Parameters dialog box creates this new outport.
% The outport exports the optimal control sequence computed by the
% controller at each sample time to MATLAB Workspace. The logged signal has
% size of [p, Nu, samples] where p is prediction horizon and Nu is the
% number of manipulated variables.  
mdl = 'mpc_sequence';
open_system(mdl);   % Open Simulink(R) Model              
sim(mdl);           % Start simulation

%% Analyze Optimal Control Sequences
% Plot the optimal control sequence at specific time instants:
times = [0 0.2 1 2 2.1 2.2 3 3.5 5];            
figure('Name','Optimal sequence history');
for t = 1:9
    ct = times(t)*10+1;
    subplot(3,3,t);
    h = stairs(0:p-1,useq.signals.values(ct,:));
    h.LineWidth = 1.5;
    hold on
    plot((0:p-1)+.5,useq.signals.values(ct,:),'*r');
    xlabel('prediction step')
    ylabel('u')
    title(sprintf('Simulation time t=%5.1f',useq.time(ct)));
    grid
    axis([0 p -1.1 1.1]);
    hold off
end
%%
% From the plot, you see that the MPC controller uses the first two seconds
% to bring the output very close to the new set point. The controller
% output is at the high limit (+1) for one second and then switched to the
% low limit (-1) for the next second, which is the best control strategy
% under the input constraint limits.
  
%%
bdclose(mdl);