Specifications
4 Designing Controllers Using the Command Line
4-10
Example Code for Successive Linearization
In the following code, the simulation begins at the CSTR model's nominal operating point
(concentration = 8.57) and moves to a lower point (concentration = 2) where the reaction
rate is much higher. The required code is as follows:
[sys, xp] = CSTR_INOUT([],[],[],'sizes');
up = [10 298.15 298.15];
u = up(3);
tsave = []; usave = []; ysave = []; rsave = [];
Ts = 1;
t = 0;
while t < 40
yp = xp;
% Linearize the plant model at the current conditions
[a,b,c,d]=linmod('CSTR_INOUT', xp, up );
Plant = ss(a,b,c,d);
Plant.InputGroup.ManipulatedVariables = 3;
Plant.InputGroup.UnmeasuredDisturbances = [1 2];
Model.Plant = Plant;
% Set nominal conditions to the latest values
Model.Nominal.U = [0 0 u];
Model.Nominal.X = xp;
Model.Nominal.Y = yp;
dt = 0.001;
simOptions.StartTime = num2str(t);
simOptions.StopTime = num2str(t+dt);
simOptions.LoadInitialState = 'on';
simOptions.InitialState = 'xp';
simOptions.SaveTime = 'on';
simOptions.SaveState = 'on';
simOptions.LoadExternalInput = 'on';
simOptions.ExternalInput = '[t up; t+dt up]';
simOut = sim('CSTR_INOUT',simOptions);
T = simOut.get('tout');
XP = simOut.get('xout');
YP = simOut.get('yout');
Model.Nominal.DX = (1/dt)*(XP(end,:)' - xp(:));