# Using Scale Factors to Facilitate Weight Tuning

This example shows how to specify scale factors in MPC controller to make weight tuning easier.

## Contents

## Define Plant Model

The discrete-time, linear, state-space plant model has 10 states, 5 inputs, and 3 outputs.

[plant,Ts] = mpcscalefactor_model; [ny,nu] = size(plant.D);

The plant inputs include manipulated variable (MV), measured disturbance (MD) and unmeasured disturbance (UD). The plant outputs include measured outputs (MO) and unmeasured outputs (UO).

mvindex = [1, 3, 5]; mdindex = 4; udindex = 2; moindex = [1 3]; uoindex = 2; plant = setmpcsignals(plant,'MV',mvindex,'MD',mdindex,'UD',udindex,'MO',moindex,'UO',uoindex);

The nominal values and operating ranges of plant model are as follows:

- Input 1: nominal value is 100, range is [50 150]
- Input 2: nominal value is 10, range is [5 15]
- Input 3: nominal value is 0.01, range is [0.005 0.015]
- Input 4: nominal value is 0.1, range is [0.05 0.15]
- Input 5: nominal value is 1, range is [0.5 1.5]

- Output 1: nominal value is 0.01, range is [0.005 0.015]
- Output 2: nominal value is 1, range is [0.5 1.5]
- Output 3: nominal value is 100, range is [50 150]

Use `lsim` command to run an open loop linear simulation to verify that plant outputs are within the range and their average are close to the nominal values when input signals vary randomly around their nominal values.

Unominal = [100;10;0.01;0.1;1]; Ynominal = [0.01;1;100]; Uspan = Unominal; Yspan = Ynominal; t = (0:1000)'*Ts; nt = length(t); Uol = (rand(nt,nu)-0.5).*(ones(nt,1)*Uspan'); % design input signal Yol = lsim(plant,Uol,t); % compute plant output fprintf('The difference between average output values and the nominal values are %.2f%%, %.2f%%, %.2f%% respectively.\n',... abs(mean(Yol(:,1)))/Ynominal(1)*100,abs(mean(Yol(:,2)))/Ynominal(2)*100,abs(mean(Yol(:,3)))/Ynominal(3)*100);

The difference between average output values and the nominal values are 2.25%, 3.53%, 2.47% respectively.

## Evaluate MPC with Default MPC Weights

When plant input and output signals have different orders of magnitude, default MPC weight settings often give poor performance.

Create MPC controller with default weights: Weight.MV = 0, Weight.MVRate = 0.1 and Weight.OV = 1.

C = mpc(plant); Xnominal = zeros(10,1); Unominal(udindex) = 0; % Nominal values for unmeasured disturbance must be 0 C.Model.Nominal = struct('X',Xnominal,'DX',Xnominal,'Y',Ynominal,'U',Unominal);

-->The "PredictionHorizon" property of "mpc" object is empty. Trying PredictionHorizon = 10. -->The "ControlHorizon" property of the "mpc" object is empty. Assuming 2. -->The "Weights.ManipulatedVariables" property of "mpc" object is empty. Assuming default 0.00000. -->The "Weights.ManipulatedVariablesRate" property of "mpc" object is empty. Assuming default 0.10000. -->The "Weights.OutputVariables" property of "mpc" object is empty. Assuming default 1.00000.

First, test a sequence of step setpoint changes in three reference signals.

nStepLen = 15; T1 = nStepLen*ny; r1 = ones(T1,1)*Ynominal(:)'; ii = 1; for i = 1:ny r1(ii:end,i) = r1(ii:end,i) + Ynominal(i); ii = ii + nStepLen; end sim(C,T1,r1);

-->The "Model.Disturbance" property of "mpc" object is empty: Assuming unmeasured input disturbance #2 is integrated white noise. -->Assuming output disturbance added to measured output channel #1 is integrated white noise. Assuming no disturbance added to measured output channel #3. -->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.

The tracking response of the first output is very poor. The reason is that its range is very small compared to other outputs. If same weights are used like the default settings, MPC controller does not pay much attention to regulate that output because the associated penalty is so smalled compared to other outputs in the objective function.

Second test is an unmeasured disturbance.

SimOpt = mpcsimopt; SimOpt.UnmeasuredDisturbance = Uspan(udindex)'; T2 = 100; r2 = ones(T2,1)*Ynominal(:)'; sim(C,T2,r2,[],SimOpt);

The disturbance rejection response is also very poor. None of the outputs go back to their setpoints.

## Evaluate MPC with Default MPC Weights After Using Scale Factor

By specifying scale factor for MPC controller to scale plant input and output signals, it would make it more likely that the default weights would give good performance and meanwhile improve the numerical quality of the optimization and state estimation calculations.

Create MPC controller with default weights: Weight.MV = 0, Weight.MVRate = 0.1 and Weight.OV = 1.

C2 = C;

It is a good practice to specify ranges as the scale factors for all the inputs and outputs.

for i = 1:length(mvindex) C2.MV(i).ScaleFactor = Uspan(mvindex(i)); end nmd = length(mdindex); for i = 1:nmd C2.D(i).ScaleFactor = Uspan(mdindex(i)); end for i = 1:length(udindex) C2.D(i+nmd).ScaleFactor = Uspan(udindex(i)); end for i = 1:ny C2.OV(i).ScaleFactor = Yspan(i); end

Repeat the first test. It is a sequence of step setpoint changes in three reference signals.

sim(C2,T1,r1);

-->The "Model.Disturbance" property of "mpc" object is empty: Assuming unmeasured input disturbance #2 is integrated white noise. -->Assuming output disturbance added to measured output channel #1 is integrated white noise. Assuming no disturbance added to measured output channel #3. -->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.

Repeat the second test. It is an unmeasured disturbance.

sim(C2,T2,r2,[],SimOpt);

Both setpoint tracking and disturbance rejection responses are very good even without tuning MPC weights.