www.gusucode.com > mpc 案例源码 matlab代码程序 > mpc/SimulationWithVaryingControllerPropertyExample.m
%% Simulation with Varying Controller Property % Vary a manipulated variable upper bound during a simulation. %% % Define the plant, which includes a 4-second input delay. Convert to a % delay-free, discrete, state-space model using a 2-second control % interval. Create the corresponding default controller and then specify % MV bounds at +/-2. Ts = 2; Plant = absorbDelay(c2d(ss(tf(0.8,[5 1],'InputDelay',4)),Ts)); MPCobj = mpc(Plant,Ts); MPCobj.MV(1).Min = -2; MPCobj.MV(1).Max = 2; %% % Create an empty |mpcmoveopt| object. During simulation, you can set % properties of the object to specify controller parameters. options = mpcmoveopt; %% % Pre-allocate storage and initialize the controller state. v = []; t = [0:Ts:20]; N = length(t); y = zeros(N,1); u = zeros(N,1); x = mpcstate(MPCobj); %% % Use |mpcmove| to simulate the following: %% % % * Reference (setpoint) step change from initial condition _r_ = 0 to _r_ = 1 (servo response). % * MV upper bound step decrease from 2 to 1, occurring at _t_ = 10. r = 1; for i = 1:N y(i) = Plant.C*x.Plant; if t(i) >= 10 options.MVMax = 1; end [u(i),Info] = mpcmove(MPCobj,x,y(i),r,v,options); end %% % As the loop executes, the value of |options.MVMax| is reset to 1 for all % iterations that occur after _t_ = 10. Prior to that iteration, % |options.MVMax| is empty. Therefore, the controller's value for |MVMax| % is used, |MPCobj.MV(1).Max = 2|. %% % Plot the results of the simulation. [Ts,us] = stairs(t,u); plot(Ts,us,'b-',t,y,'r-') legend('MV','OV') xlabel(sprintf('Time, %s',Plant.TimeUnit)) %% % From the plot, you can observe that the original MV upper bound is active % until _t_ = 4. After the input delay of 4 seconds, the output variable % (OV) moves smoothly to its new target of _r_ = 1. reaching the target at % _t_ = 10. The new MV bound imposed at _t_ = 10 becomes avtive % immediately. This forces the OV below its target, after the input delay % elapses. %% % Now assume that you want to impose an OV upper bound at a specified % location relative to the OV target. Consider the following constraint % design command: MPCobj.OV(1).Max = [Inf,Inf,0.4,0.3,0.2]; %% % This is a horizon-varying constraint. The known input delay makes it % impossible for the controller to satisfy an OV constraint prior to the % third prediction-horizon step. Therefore, a finite constraint during the first % two steps would be poor practice. For illustrative purposes, the above % constraint also decreases from 0.4 at step 3 to 0.2 at step 5 and % thereafter. %% % The following commands produce the same results shown in the % previous plot. The OV constraint is never active because it is being varied % in concert with the setpoint, _r_. x = mpcstate(MPCobj); OPTobj = mpcmoveopt; for i = 1:N y(i) = Plant.C*x.Plant; if t(i) >= 10 OPTobj.MVMax = 1; end OPTobj.OutputMax = r + 0.4; [u(i),Info] = mpcmove(MPCobj,x,y(i),r,v,OPTobj); end %% % The scalar value _r_ + 0.4 replaces the first finite value in the % |MPCobj.OV(1).Max| vector, and the remaining finite values adjust to % maintain the original profile, i.e., the numerical difference between % these values is unchanged. _r_ = 1 for the simulation, so the above use of % the mpcmoveopt object is equivalent to the command MPCobj.OV(1).Max = [Inf, Inf, 1.4, 1.3, 1.2]; %% % The use of the |mpcmoveopt| object involves much less computational overhead, however.