# Simple Level Control Example

Consider the basic control scheme for a water tank in Figure 1, where the objective is to maintain the level $h(t)$ between a maximum $H{max}$ and a minimum $H{min}$  measured by two level switches. The tank is fed by an on-off pump that allows a inflow of  $Q_i$  and the outflow  $Q_o$  at the bottom is a function on the difference between the hydro static pressure and the pressure in the outlet pipe.

Figure 1.

The state of the pump will be changed when the maximum level is reached and the pump is on or when the minimum level is reached and the pump is off .
This simple system can be represented as an Hybrid System, as follows:
The state of the system is:
$x =\left[ \begin{array}{c} x_1\\ x_2\\ \end{array} \right]$

where $x_1:$ Tank level [m] and $x_2:$ State of the pump, where $x_2=0:$ pump off and  $x_2=1:$ pump on.

$f(x) =\left[ \begin{array}{c} \frac{1}{\pi r^2}\cdot (x_2\cdot Q_i-k_v\cdot \sqrt{gx_1})\\ 0\\ \end{array} \right]$

where $g$ : gravitational constant, $k_v$:  flow coefficient.

$C:=\{ x : x_1 < H{max }\} \cup \{x: x_1 \leq H{min}, x_2=1 \}$ $\cup \{x: x_1 \geq H{max}, x_2=0 \}$

$g(x) =\left[ \begin{array}{c} x_1\\ 1-x_2\\ \end{array} \right]$

$D:=\{ x: x_1 \geq H{max}, x_2=1 \} \cup \{x: x_1 \leq H{min}, x_2=0 \}$

Simulation instructions for Hybrid Equations Toolbox

Simulation results for $H{max}=1.2, H{min}=0.8, Qi=0.4[m3/s], kv=0.05, x_1(0,0)=1.8 [m]$ are shown in Figure 2 a) and b)

a)                                                                                             b)

Figure 2. a) Tank Level. b) Pump State

Source code for Simulation:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: run_tank
%
% Version 1.0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function run_tank
%constants
hmin=0.8;
hmax=1.2;
Qi=0.4;
kv=0.05;
A=0.5^2*pi;
global hmin hmax Qi kv A;

% clear all
% initial conditions
x0 = [1.8;1];

% simulation horizon
TSPAN=[0 20];
JSPAN = [0 20];

% rule for jumps
% rule = 1 -> priority for jumps
% rule = 2 -> priority for flows
rule = 1;

options = odeset('RelTol',1e-6,'MaxStep',1e-3);

% simulate
[t j x] = HyEQsolver( @f,@g,@C,@D,...
x0,TSPAN,JSPAN,rule,options);

% state
x1 = x(:,1);
x2 = x(:,2);

% plot solutions
figure(1)
clf
subplot(2,1,1),plotflows(t,j,x(:,1))
grid on
ylabel('Tank level')

subplot(2,1,2),plotjumps(t,j,x(:,1))
grid on
ylabel('x1')

figure(2)
clf
subplot(2,1,1),plotflows(t,j,x(:,2))
grid on
ylabel('Pump State')

subplot(2,1,2),plotjumps(t,j,x(:,2))
grid on
ylabel('x2')

% plot hybrid arc
plotHybridArc(t,j,x(:,1))
xlabel('j')
ylabel('t')
zlabel('x1')


Flow map (f.m)

function xdot = f(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Project: Simulation of a hybrid system
%
% Name: f.m
%
% Description: Flow map
%
% Version 1.0
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% flow map: xdot=f(x,u,parameters);
% Constants:
global Qi;
global kv;
global A;

% State
xi1 = x(1); % Tank level h
xi2 = x(2); % Pump state

% flow map
%xdot=f(x,u);
xi1dot = 1/A*(xi2*Qi-kv*sqrt(9.81*xi1));
xi2dot = 0;

xdot = [xi1dot;xi2dot];



Flow set (C.m)

function v  = C(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Project: Simulation of a hybrid system
%
% Name: C.m
%
% Description: Flow set
%
% Version: 1.0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constants
global hmin;
global hmax;

% State
xi1 = x(1);
xi2 = x(2);

% flow condition

if (((xi1 <= hmin ) && (xi2 == 1)) || ((xi1 >= hmax ) && (xi2 == 0)))  % flow condition
v = 1;  % report flow
elseif (xi1 < hmax )
v = 1;  % report flow
else
v = 0;   % do not report flow
end


Jump map (g.m)

function xplus = g(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Project: Simulation of a hybrid system
%
% Name: g.m
%
% Description: Jump map
%
% Version: 1.0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% jump map: xplus = g(x,u,parameters);
% state
xi1 = x(1);
xi2 = x(2);

xi1plus=xi1;
xi2plus=1-xi2;

xplus = [xi1plus;xi2plus];


Jump set (D.m)

function v  = D(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Project: Simulation of a hybrid system
%
% Name: D.m
%
% Description: Jump set
%
% Version: 1.0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Check on flow conditions
% % E.g.,
% if (x(1) <= u(1)) && (x(2) <= 0)   % jump condition
%     v = 1;  % report jump
% else
%     v = 0;
% do not report jump
% end
% Constants
global hmin;
global hmax;
% state
xi1 = x(1);
xi2 = x(2);

% Jump conditions
if ((xi1 >= hmax) && ( xi2 == 1))
v = 1;  % report jump
elseif ((xi1 <= hmin) && ( xi2 == 0)) % jump condition
v = 1;  % report jump
else
v = 0;   % do not report jump
end