# Pendulum Example for Hybrid Equations Toolbox

Instruction to simulate pendulum example are in the following video:

Notice that this example uses the simulink HyEQ simulator. Thus, you may create the files from scratch, or download the files here. Please, change the extension of the downloaded file from -zip.doc to .zip, then you can unzip and browse the files for the example.
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
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% flow map: xdot=f(x,u,parameters);

x1=x(1);
x2=x(2);

length=1;
gamma=-9.81;
xdot=[x(2);gamma/length*sin(x(1))];
```

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
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Check on flow conditions
% E.g.,
% if (x(1) >= u(1))  % flow condition
%     v = 1;  % report flow
% else
%     v = 0;   % do not report flow
% end

x1=x(1);
x2=x(2);

if x1 >= 0;
v = 1;
else
v=0;
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
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% jump map: xplus = g(x,u,parameters);

x2=x(2);

xplus = [0.001;-.8*x2];
```

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
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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

x1=x(1);
x2=x(2);

if x1 <= 0 && x2 <= 0
v=1;
else
v = 0;
end
```

Initialization script: initialization_pendulum.m

```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: initialization.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% initialization for bouncing ball example

% clear all
clc
% initial conditions
x0 = [pi/4;0];

% physical variables
global gamma lambda
gamma = -9.81;  % gravity constant
lambda = 0.8;   % restitution coefficent

% simulation horizon
T = 10;
J = 20;

% rule for jumps
% rule = 1 -> priority for jumps
% rule = 2 -> priority for flows
% rule = 3 -> no priority, random selection when simultaneous conditions
rule = 1;

%solver tolerances
RelTol = 1e-6;
MaxStep = 1e-3;
```

Plot script: postprocessing_pendulum.m

```%postprocessing for the bouncing ball example

% plot solution
figure(1)
clf
subplot(2,1,1),plotflows(t,j,x(:,1))
grid on
ylabel('x1')

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('x2')

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