Cyclic redundancy check in HyEQ V2.01 installation

Regarding the release of the HyEQ toolbox V2.01, some users found a glitch when installing the new version into an iMac computer. The error is the following,

>> install
———————————–
 INSTALLATION HyEQ_Toolbox_V2_01
———————————–
 Reading the archiv
—————————-
      ERROR OCCURED
   during installation
—————————-
Error using install (line 80)
The installation file is corrupt!
Ensure that the archive container was not modified (check FTP/
proxy/ firewall settings, anti-virus scanner for emails etc.)!

Thus, to solve this problem and install the toolbox, it is necessary that the user create its own install file in her/his computer. Here are the steps how to create and install the installation file.

  1. You need to know which is your matlab’s user userpath folder, for this, type in your matlab’s command window >>userpath
  2. Download the makeinstall program at http://www.mathworks.com/matlabcentral/fileexchange/1529-makeinstall and uncompress the content in your userpath folder.
  3. Locate and open the makeinstall folder in matlab. Run the script >> makeinstall, and then install the makeinstall program, typing >> install. Now you can create your own install files!
  4. Download the HyEQ V2.01 source files here (please change the .doc extension by .zip)
  5. Uncompress the file and copy the folder in any place but your userpath folder.
  6. Locate the folder in matlab, and then create the install file typing >> makeinstall. Thus, if you didn’t get any error, you can install the HyEQ toolbox V2.01 typing >> install.
  7. Follow the instructions for the installation.

I hope this is usefull for those who found the error, please if you have questions leave a comment, and I’ll try to help you.

Best,

Pablo.

PD: Thanks Sean and Yuchun for all your help.

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

Updating to mac OSX 10.9 (Mavericks) and Matlab 2013b and the required “fixes” for the HyEQ simulator.

I just updated my OSX to Mavericks 10.9 and Matlab 2013b, and I had some issues trying to use the HyEQ simulator. I just want to share how I fix a couple of problems in the case that anyone has the same issues.

The steps that I suggested to update the system are the following:

    1. Update to Mavericks.
    2. Update Xcode to the newest version (I have 5.0.1).
    3. Update or install Matlab 2013b.
    4. Open Matlab, if you having warnings such as “Warning: Name is nonexistent or not a directory”, please fix those warnings following the steps in: http://www.mathworks.com/support/solutions/en/data/1-1BUE0/?product=ML
    5. Setup the C-compiler in matlab following the steps in the instructions (in the HyEQ instructions file).

    6. Download and install the HyEQ simulator from: http://www.mathworks.com/matlabcentral/fileexchange/41372-hybrid-equations-toolbox-v2-0
    7. Open Simulink and try to run an example:

      it worked? yes = you are done :), no= continue with step 8 :S

    8. If you get the following error “xcodebuild: error: SDK “macosx10.7” you need to change some lines in your file “mexopts.sh”, follow the instructions in http://www.mathworks.com/support/solutions/en/data/1-OAMYBZ/ or http://www.cyberbotics.com/forum?message=2406
    9. In the Matlab’s command prompt >> mex -setup, and follow the steps in the instructions file.

    10. Reboot matlab and try again 7.
    • it worked? yes = you are wellcome 🙂
    • no= sorry 😦

I hope this could be useful for someone, (now my Maverics+Matlab2013b+HyEQ simulator is working)

bouncing ball with Lite HyEQ Solver

Consider a model for a bouncing ball system where {x_1} is the distance of the ball regarding the floor and {x_2} is its speed. Now consider the hybrid model that describes the bouncing ball written as

Example_1_20x

where {\gamma >0} is the gravity constant and {\lambda \in [0,1)} is the restitution coefficient. Consider the ball to be bouncing on a floor at zero height. The constants for the bouncing ball system are {\gamma = 9.81} and {\lambda=0.8}.
The following procedure is used to simulate this example in the Lite HyEQ Solver:

  • Inside the MATLAB script run.m, initial conditions, simulation horizons, a rule for jumps, ode solver options, and a step size coefficient are defined. The function HyEQsolver.m is called in order to run the simulation, and a script for plotting solutions is included.
  • Then the MATLAB functions f.m, C.m, g.m, D.m are edited according to the data given above.
  •  Finally, the simulation is run by clicking the run button in run.m or by calling run.m in the MATLAB command window.

The simulation results are shown in the following images, and the example code for each of the MATLAB files run.m, f.m, C.m, g.m, and D.m is given below.

fig1BouncingBallfig2BouncingBall

Simulation code


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: run.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Bouncing ball example - Lite simulator version
%% initial conditions
clear all
close all
clc

x1_0 = 1;
x2_0 = 0;
x0 = [x1_0;x2_0];
% simulation horizon
TSPAN=[0 10];
JSPAN = [0 20];
% rule for jumps
% rule = 1 -> priority for jumps
% rule = 2 -> priority for flows
rule = 1;
options = odeset('RelTol',1e-6,'MaxStep',.1);
% constants
global gamma lambda;
    gamma = 9.81; %# gravity constant
    lambda = 0.8; % restitution coefficient

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

%% plot solution
figure(1) % position
clf
subplot(2,1,1),plotHarc(t,j,x(:,1))
grid on
ylabel('x1')
subplot(2,1,2),plotjumps(t,j,x(:,1))
grid on
ylabel('x1')
figure(2) % velocity
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)
xlabel('j')
ylabel('t')
zlabel('x1')

Flow map


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
%
% Filename: f.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Flow map
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global gamma;

% state
x1 = x(1);
x2 = x(2);
% differential equations
xdot = [x2 ; -gamma];
end

Flow set


function [value] = C(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: C.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Flow set
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x1 = x(1);
if x1 >= 0% flow condition
    value = 1;% report flow
else
    value = 0;% do not report flow
end
end

Jump map


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
%
% Filename: g.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Jump map
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global lambda;

% state
x1 = x(1);
x2 = x(2);
xplus = [-x1 ; -lambda*x2];
end

Jump set


function inside = D(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: D.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Jump set
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x1 = x(1);
x2 = x(2);
if (x1 <= 0 && x2 <= 0)% jump condition
    inside = 1;% report jump
else
    inside = 0;% do not report jump
end
end

biological example: synchronization of n fireflies

Consider a biological example of the synchronization of n fireflies flashing. The fireflies can be modeled mathematically as periodic oscillators which tend to synchronize their flashing until they are flashing in phase with each other. A state value of {\tau_i=1} corresponds to a flash, and after each flash, the firefly automatically resets its internal timer (periodic cycle) to {\tau_i=0}. The synchronization of the fireflies can be modeled as a hybrid system because every time one firefly flashes, the other firefly notices and jumps ahead in its internal timer {\tau_i} by {(1+\varepsilon)\tau_i}, where {\varepsilon} is a biologically determined coefficient. This happens until eventually all fireflies synchronize their internal timers and are flashing simultaneously. We assume that the fireflies are fully interconnected as in Figure 1.

NetworkTopology-FullyConnected

Now consider a population of n fireflies, then the population can be represented with a vector state {x = [\tau_1,\tau_2,\ldots,\tau_n]^\top\in\Re^n}.

Furthermore, the fireflies population can be modeled as a hybrid system given by

Example_1_71x

Notice that here no external inputs are being considered. The only event that affects the flashing of a firefly {\tau_i} is the flashing of the other firefly.

A solution to the hybrid system with {T=7,\ J=100,\ r=1,\ \varepsilon=0.2} is depicted in Figure 2. Both the projection onto {t} and {j} are shown.

SimulationResult

These simulations reflect the expected behavior of the hybrid system. The fireflies initially flash out of phase with one another and then synchronize to flash in the same phase.

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.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Fireflies example - Lite simulator version
%% initial conditions
clear all
close all
clc

% initial conditions
N = 20; % Poblation size
x0 = rand(N,1);
% simulation horizon
T = 7;
J = 100;
% simulation horizon
TSPAN = [0 T];
JSPAN = [0 J];

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

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

% constants
global n epsilon;
    n = N; %# of state components
    epsilon = 0.2; % Bilogical parameter

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

%%
% plot solution
figure(1)
clf
subplot(2,1,1),plotHarc(t,j,x);
grid on
title(['Evolution of firefly oscilation for a ',num2str(n), ' population size (flow)'])
xlabel('time')
ylabel('x')

subplot(2,1,2),plotjumps(t,j,x)
grid on
ylabel('x')
xlabel('jumps')
title(['Evolution of firefly oscilation for a ',num2str(n), ' population size (jump)'])
%% plot hybrid arc
plotHybridArc(t,j,x);
xlabel('j')
ylabel('t')
zlabel('x')

Flow map


function out = f(z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: f.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Flow map
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global  n;

% state
x =z(1:n);

% flow map
%xdot=f(x,u);
xdot = ones(n,1);

out = xdot;

Jump map

function out = g(z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: g.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Jump map
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global  n epsilon;

% state
x = z(1:n);

xplus = x;
for ix = 1:n
    if (1+epsilon)*x(ix)<1
        xplus(ix) = (1+epsilon)*x(ix);
    else
        xplus(ix) = 0;
    end
end

out = xplus;

Flow set


function [v] = C(z) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: C.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Flow set
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% constants
global  n;

% state
x =z(1:n);


if (sum(x<1)==n)  % flow condition
    v = 1;  % report flow
else
    v = 0;   % do not report flow
end

Jump set

 

function [v] = D(z) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: D.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Jump set
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% constants
global  n;

% state
x =z(1:n);

if (sum(x>=1)>0)  % jump condition
    v = 1;  % report jump
else
    v = 0;   % do not report jump
end