%% Temperature Control in a Heat Exchanger
% This example shows how to design feedback and feedforward compensators
% to regulate the temperature of a chemical reactor through a heat
% exchanger.
%   example from MathWorks

clear all
close all

%% Heat Exchanger Process

theta_p=14.7;
tau_p=21.3;
s = tf('s');
Gp = exp(-theta_p*s)/(1+tau_p*s); % transfer from the voltage to the tank temperature

theta_d=35;
tau_d=25;
s = tf('s');
Gd = exp(-theta_d*s)/(1+tau_d*s); % transfer from the variation of the inflow temperature to the tank temperature


%% Feedback Control
% kp = ;
% ki = ;
% C = tf([kp ki],[1 0]);

% simulation
% Tfb = feedback(ss(Gp*C),1);
% step(Tfb), grid on
% title('Response to step change in temperature setpoint T_{sp}')
% ylabel('Tank temperature')
% xlabel('Time')

% Margins
% margin(Gp*C), grid

%% Feedforward Control
% Ideal feedforward for perfect disturbance rejection  
% Fideal = ;

%  more realistic feedforward
% F = 

% Tff = ;   % d->T transfer with feedforward control

% step(Tff), grid
% title('Effect of a step disturbance in inflow temperature')
% ylabel('Tank temperature')


%% Combined Feedforward-Feedback Control
% Transfer functions from (Tsp,d) to T (MIMO)
% feedback only
% Tfb = ;
% feedforward +feedback
% Tffb = ;

% Comparison
% step(Tfb,'b',Tffb,'r--'), grid
% title('Closed-loop response to setpoint and disturbance step change')
% ylabel('Tank temperature')
% legend('Feedback only','Feedforward + feedback')

% bodemag(Tfb,'b',Tffb,'r--',{1e-3,1e1})
% legend('Feedback only','Feedforward + feedback','Location','SouthEast')

