# Simple Level Control Example for Hybrid Equations Toolbox

Instruction to simulate Simple level control example are in the following video:

Code for simulations
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:
Qi=0.4;
kv=0.05;
A=0.5^2*pi;

% 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
hmin=0.8;
hmax=1.2;

% 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
hmin=0.8;
hmax=1.2;

% 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
```

Initialization script: tank_ini.m

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

% simulation horizon
T=20;
J=20;

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

%solver parameters
RelTol=1e-6
MaxStep=1e-3
```

Plot script: tank_plot.m

```% 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')
```