% Set up exact and cascade simulations for mixing example
% 11 dec 2021
clc;clear;close all; warning off


%% Parameters
V   = 100;                                                %[kg] tank holdup
q0  = 10;                                       %[kg/s] nominal outlet flow
q10 = 5;                                             %[kg/s] inlet flow 1
q20 = 5;                                             %[kg/s] inlet flow 2
T0  = 35;                                %[degC] nominal outlet temperature
T10 = 20;                          %[degC] nominal inlet flow 1 temperature
T20 = 50;                          %[degC] nominal inlet flow 2 temperature
v10 = q0;                                              %transf input 1 bias
u0  = [q10;q20];                                               % input bias
d0  = [T10;T20];

%% Linear transformation tuning
A   = -q0/V*1;                 % tuning parameter for linear transformation
B   = -A;                                       % gives steady-state gain 1

%% Set up simulation
tsim     = 300; %[s] simulation time
tstep_q  = 50;  % [s] time for setpoint change T
tstep_T  = 100;  % [s] time for setpoint change q
tstep_T1 = 150;  % [s] time for disturbance change T1
tstep_T2 = 200;  % [s] time for disturbance change T2

Tstep  = T0 + 10;  % [degC] setpoint change T
qstep  = q0 + 2;  % [kg/s] setpoint change q
T1step = T10 + 10; % [degC] disturbance change T1
T2step = T20 + 10; % [degC] disturbance change T2

%% Disturbance measurment error
dmerr = 0; % 1-true, 0-false
if dmerr == 0
     T1m = T1step; T2m = T2step;
elseif dmerr == 1
     T1m = T10; T2m = T20;
end


%% Simulate with or without outer controller
OL = 1; % 0-false; 1-true


