SlideShare a Scribd company logo
1 of 12
Download to read offline
MODELING IN SCILAB
APPROACH – PART
In this tutorial we show how to model a physical system described by ODE using
Scilab standard programming language. The same model solution is also
described in Xcos and Xcos + Modelica in two other tutorials.
Level
This work is licensed under a Creative Commons Attribution
www.openeering.com
CILAB: PAY ATTENTION TO THE RIGHT
PART 1
this tutorial we show how to model a physical system described by ODE using
Scilab standard programming language. The same model solution is also
described in Xcos and Xcos + Modelica in two other tutorials.
under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
powered by
RIGHT
this tutorial we show how to model a physical system described by ODE using
Scilab standard programming language. The same model solution is also
LHY Tutorial Scilab www.openeering.com page 2/12
Step 1: The purpose of this tutorial
In Scilab there are three different approaches (see figure) for modeling
a physical system which are described by Ordinary Differential Equations
(ODE).
For showing all these capabilities we selected a common physical
system, the LHY model for drug abuse. This model is used in our
tutorials as a common problem to show the main features of each
strategy. We are going to recurrently refer to this problem to allow the
reader to better focus on the Scilab approach rather than on mathematical
details.
In this first tutorial we show, step by step, how the LHY model problem
can be implemented in Scilab using standard Scilab programming. The
sample code can be downloaded from the Openeering web site.
1 Standard Scilab
Programming
2 Xcos Programming
3 Xcos + Modelica
Step 2: Model description
The considered model is the LHY model used in the study of drug abuse.
This model is a continuous-time dynamical system of drug demand for two
different classes of users: light users (denoted by ) and heavy users
(denoted by ) which are functions of time . There is another state in
the model that represents the decaying memory of heavy users in the
years (denoted by ) that acts as a deterrent for new light users. In
other words the increase of the deterrent power of memory of drug abuse
reduces the contagious aspect of initiation. This approach presents a
positive feedback which corresponds to the fact that light users promote
initiation of new users and, moreover, it presents a negative feedback
which corresponds to the fact that heavy users have a negative impact on
initiation. Light users become heavy users at the rate of escalation
and leave this state at the rate of desistance . The heavy users leave
this state at the rate of desistance .
LHY Tutorial Scilab www.openeering.com page 3/12
Step 3: Mathematical model
The mathematical model is a system of ODE (Ordinary Differential
Equation) in the unknowns:
• L t , number of light users;
• H t , number of heavy users;
• Y t , decaying of heavy user years.
The initiation function contains a “spontaneous” initiation τ and a
memory effect modeled with a negative exponential as a function of the
memory of year of drug abuse relative to the number of current light users.
The problem is completed with the specification of the initial conditions
at the time t .
The LHY equations system (omitting time variable t for sake of simplicity) is
L I L, Y a b L
H bL gH
Y H δY
where the initiation function is
I L, Y τ L max s"#$ , s · e'(
)
*+
The LHY initial conditions are
L t L
H t H
Y t Y
LHY Tutorial Scilab www.openeering.com page 4/12
Step 4: Problem data
(Model data)
a : the annual rate at which light users quit
b : the annual rate at which light users escalate to heavy use
g : the annual rate at which heavy users quit
δ : the forgetting rate
(Initiation function)
τ : the number of innovators per year
s : the annual rate at which light users attract non-users
q : the constant which measures the deterrent effect of heavy use
s"#$ : the maximum rate of generation for initiation
(Initial conditions)
t : the initial simulation time;
L : Light users at the initial time;
H : Heavy users at the initial time;
Y : Decaying heavy users at the initial time.
Model data
a 0.163
b 0.024
g 0.062
δ 0.291
Initiation function
τ 50000
s 0.610
q 3.443
s"#$ 0.1
Initial conditions
t 1970
L 1.4 7 108
H 0.13 7 108
Y 0.11 7 108
LHY Tutorial Scilab www.openeering.com page 5/12
Step 5: Scilab programming
With the standard approach, we solve the problem using the ODE function
available in Scilab. This command solves ordinary differential equations
with the syntax described in the right-hand column.
In our problem we have:
• Y0 equal to 9 , , : since the unknowns are , and
Y ;
• t0 equal to 1970;
• t generated using the command
t = Tbegin:Tstep:(Tend+100*%eps);
which generates a regularly spaced vector starting from Tbegin
and ending at Tend with a time-step Tstep. A tolerance is added
to ensure that the value Tend is reached. See also linspace
command for an alternative solution.
• f a function of three equations in three unknowns which
represents the system to be solved.
The syntax of the ode function is
y=ode(y0,t0,t,f)
where at least the following four arguments are required:
• Y0 : the initial conditions [vector] (the vector length should be
equal to the problem dimension and its values must be in the same
order of the equations.
• t0 : the initial time [scalar] (it is the value where the initial
conditions are evaluated);
• t : the times steps at which the solution is computed [vector]
(Typically it is obtained using the function LINSPACE or using the
colon : operator);
• f : the right-hand side of system to be solved [function,
external, string or list].
LHY Tutorial Scilab www.openeering.com page 6/12
Step 6: Roadmap
We implement the system in the following way:
• Create a directory where we save the model and its supporting
functions. The directory contains the following files:
- A function that implements the initiation function;
- A function that implements the system to be solved;
- A function that plots the results.
• Create a main program that solves the problem;
• Test the program and visualize the results.
• LHY_Initiation.sci : The initiation function
• LHY_System.sci : The system function
• LHY_Plot.sci : The plotting function
• LHY_MainConsole.sce : The main function.
Step 7: Create a working directory
We create a directory, name it "model" in the current working folder
(LHYmodel) as shown in figure.
LHY Tutorial Scilab www.openeering.com page 7/12
Step 8: Create the initiation function
Now, we implement the following formula
; , < max ="#$ , = · >'?
@
A+
as a Scilab function.
The function returns the number of initiated individuals ; per years using
the current value of , and its parameters <, =, B, ="#$ . We decide to
implement the function parameters as a Scilab mlist data structure.
The following code is saved in the file "LHY_Initiation.sci" in the
model directory.
function I=LHY_Initiation(L, H, Y, param)
// It returns the initiation for the LHY model.
//
// Input:
// L = number of light users,
// H = number of heavy users,
// Y = decaying heavy user years,
// param is a structure with the model parameters:
// param.tau = number of innovators per year,
// param.s = annual rate at which light users
// attract non-users,
// param.q = deterrent effect of heavy users constant,
// param.smax = maximum feedback rate.
//
// Output:
// I = initiation.
//
// Description:
// The initiation function.
// Fetching
tau = param.tau;
s = param.s;
q = param.q;
smax = param.smax;
// Compute s effective
seff = s*exp(-q*Y./L);
seff = max(smax,seff);
// Compute initiation (vectorized formula)
I = tau + seff.*L;
Endfunction
LHY Tutorial Scilab www.openeering.com page 8/12
Step 9: Create the system function
Now, we implement the right hand side of the ODE problem as requested
by the Scilab function ode:
; ,
C
All the model parameters are stored in the "param" variables in the proper
order.
The following code is saved in the file "LHY_System.sci" in the model
directory.
function LHYdot=LHY_System(t, LHY, param)
// The LHY system
// Fetching LHY system parameters
a = param.a;
b = param.b;
g = param.g;
delta = param.delta;
// Fetching solution
L = LHY(1,:);
H = LHY(2,:);
Y = LHY(3,:);
// Evaluation of initiation
I = LHY_Initiation(L, H, Y, param);
// Compute Ldot
Ldot = I - (a+b)*L;
Hdot = b*L - g*H;
Ydot = H - delta*Y;
LHYdot = [Ldot; Hdot; Ydot];
Endfunction
LHY Tutorial Scilab www.openeering.com page 9/12
Step 10: A data plot function
The following function creates a data plot, highlighting the maximum value
of each variables. The name of the function is "LHY_Plot.sci" and shall
be saved in the model directory.
This function draws the values of L, H, Y and I and also computes their
maximum values.
function LHY_Plot(t, LHY)
// Nice plot data for the LHY model
// Fetching solution
L = LHY(1,:);
H = LHY(2,:);
Y = LHY(3,:);
// Evaluate initiation
I = LHY_Initiation(L,H,Y, param);
// maximum values for nice plot
[Lmax, Lindmax] = max(L); tL = t(Lindmax);
[Hmax, Hindmax] = max(H); tH = t(Hindmax);
[Ymax, Yindmax] = max(Y); tY = t(Yindmax);
[Imax, Iindmax] = max(I); tI = t(Iindmax);
// Text of the maximum point
Ltext = msprintf(' ( %4.1f , %7.0f)',tL,Lmax);
Htext = msprintf(' ( %4.1f , %7.0f)',tH,Hmax);
Ytext = msprintf(' ( %4.1f , %7.0f)',tY,Ymax);
Itext = msprintf(' ( %4.1f , %7.0f)',tI,Imax);
// Plotting of model data
plot(t,[LHY;I]);
legend(['Light Users';'Heavy users';'Memory';'Initiation']);
// Vertical line
set(gca(),"auto_clear","off");
xpolys([tL,tH,tY,tI;tL,tH,tY,tI],[0,0,0,0;Lmax,Hmax,Ymax,Imax]);
// Text of maximum point
xstring(tL,Lmax,Ltext);
xstring(tH,Hmax,Htext);
xstring(tY,Ymax,Ytext);
xstring(tI,Imax,Itext);
xlabel('Year');
set(gca(),"auto_clear","on");
endfunction
LHY Tutorial Scilab www.openeering.com page 10/12
Step 11: The main program
The function implements the main program to solve the system.
The code is saved in the file "LHY_MainConsole.sce" in the working
directory.
In the main program we use the function GETD to import all the developed
functions contained in our model directory.
// Testing the model using US drug data
clc
// Import LHY functions
getd('model');
// Setting LHY model parameter
param = [];
param.tau = 5e4; // Number of innovators per year (initiation)
param.s = 0.61; // Annual rate at which light users attract
// non-users (initiation)
param.q = 3.443; // Constant which measures the deterrent
// effect of heavy users (initiation)
param.smax = 0.1;// Upper bound for s effective (initiation)
param.a = 0.163; // Annual rate at which light users quit
param.b = 0.024; // Annual rate at which light users escalate
// to heavy use
param.g = 0.062; // Annual rate at which heavy users quit
param.delta = 0.291; // Forgetting rate
// Setting initial conditions
Tbegin = 1970; // Initial time
Tend = 2020; // Final time
Tstep = 1/12 // Time step (one month)
L0 = 1.4e6; // Light users at the initial time
H0 = 0.13e6; // Heavy users at the initial time
Y0 = 0.11e6; // Decaying heavy user at the initial time
// Assigning ODE solver data
y0 = [L0;H0;Y0];
t0 = Tbegin;
t = Tbegin:Tstep:(Tend+100*%eps);
f = LHY_System;
// Solving the system
LHY = ode(y0, t0, t, f);
// Plotting of model data
LHY_Plot(t, LHY);
LHY Tutorial Scilab www.openeering.com page 11/12
Step 12: Running and testing
Change the Scilab working directory to the current project working
directory (e.g. cd 'D:ManoloLHYmodel') and run the command
exec('LHY_MainConsole.sce',-1).
This command runs the main program and plots a chart similar to the one
shown on the right.
Step 13: Exercise #1
Modify the main program file such that it is possible to compare the
“original model” with the same model where the parameter a 0.2. Try to
plot the original result and the new simulation results in the same figure for
a better comparison.
Hints: Some useful functions
scf(1) Set the current graphic figure
subplot(121) Divide a graphics window into a matrix of sub-windows
(1 row, 2 columns and plot data on first sub-window)
subplot(122) Divide a graphics window into a matrix of sub-windows
(1 row, 2 columns and plot data on second sub-window)
a=gca() Return handle to the current axes
a.data_bounds=[1970,0;2020,9e+6]; Set bound of axes data
LHY Tutorial Scilab www.openeering.com page 12/12
Step 14: Concluding remarks and References
In this tutorial we have shown how the LHY model can be implemented in
Scilab with a standard programming approach.
On the right-hand column you may find a list of references for further
studies.
1. Scilab Web Page: Available: www.scilab.org.
2. Scilab Ode Online Documentation:
http://help.scilab.org/docs/5.3.3/en_US/ode.html.
3. Openeering: www.openeering.com.
4. D. Winkler, J. P. Caulkins, D. A. Behrens and G. Tragler,
"Estimating the relative efficiency of various forms of prevention at
different stages of a drug epidemic," Heinz Research, 2002.
http://repository.cmu.edu/heinzworks/32.
Step 15: Software content
To report a bug or suggest some improvement please contact Openeering
team at the web site www.openeering.com.
Thank you for your attention,
Manolo Venturin
-------------------
LHY MODEL IN SCILAB
-------------------
----------------
Directory: model
----------------
LHY_Initiation.sci : Initiation function
LHY_Plot.sci : Nice plot of the LHY system
LHY_System.sci : LHY system
--------------
Main directory
--------------
ex1.sce : Solution of the exercise
LHY_MainConsole.sce : Main console program
license.txt : The license file

More Related Content

What's hot

How to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabHow to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabScilab
 
Scilab as a calculator
Scilab as a calculatorScilab as a calculator
Scilab as a calculatorScilab
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab
 
Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3Scilab
 
Scilabisnotnaive
ScilabisnotnaiveScilabisnotnaive
Scilabisnotnaivezan
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on SessionDharmesh Tank
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsRay Phan
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
Basics of MATLAB programming
Basics of MATLAB programmingBasics of MATLAB programming
Basics of MATLAB programmingRanjan Pal
 
Operators In Java Part - 8
Operators In Java Part - 8Operators In Java Part - 8
Operators In Java Part - 8MuhammadAtif231
 
Simple Scala DSLs
Simple Scala DSLsSimple Scala DSLs
Simple Scala DSLslinxbetter
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabBilawalBaloch1
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operatorsLuckshay Batra
 
Basics of programming in matlab
Basics of programming in matlabBasics of programming in matlab
Basics of programming in matlabAKANKSHA GUPTA
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMOHDRAFIQ22
 
Matlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLABMatlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLABreddyprasad reddyvari
 

What's hot (20)

How to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabHow to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in Scilab
 
Scilab as a calculator
Scilab as a calculatorScilab as a calculator
Scilab as a calculator
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
 
Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3
 
Scilabisnotnaive
ScilabisnotnaiveScilabisnotnaive
Scilabisnotnaive
 
What is matlab
What is matlabWhat is matlab
What is matlab
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & Scientists
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
Basics of MATLAB programming
Basics of MATLAB programmingBasics of MATLAB programming
Basics of MATLAB programming
 
Matlab commands
Matlab commandsMatlab commands
Matlab commands
 
Operators In Java Part - 8
Operators In Java Part - 8Operators In Java Part - 8
Operators In Java Part - 8
 
Simple Scala DSLs
Simple Scala DSLsSimple Scala DSLs
Simple Scala DSLs
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operators
 
Basics of programming in matlab
Basics of programming in matlabBasics of programming in matlab
Basics of programming in matlab
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On Plotting
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Matlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLABMatlab day 1: Introduction to MATLAB
Matlab day 1: Introduction to MATLAB
 

Similar to Modeling an ODE: 3 different approaches - Part 1

CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
Lhy tutorial gui(1)
Lhy tutorial gui(1)Lhy tutorial gui(1)
Lhy tutorial gui(1)Brijesh Naik
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8Raffi Khatchadourian
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning materialArthyR3
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Akhil Mittal
 
How to Analyze the Results of LinearPrograms—Part 1 Prelimi.docx
How to Analyze the Results of LinearPrograms—Part 1 Prelimi.docxHow to Analyze the Results of LinearPrograms—Part 1 Prelimi.docx
How to Analyze the Results of LinearPrograms—Part 1 Prelimi.docxpooleavelina
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in javasureshraj43
 
Programming II hiding, and .pdf
 Programming II hiding, and .pdf Programming II hiding, and .pdf
Programming II hiding, and .pdfaludin007
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalRxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalSidereo
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Haitham Raik
 
ObservabilityForModernApplications-Oslo.pdf
ObservabilityForModernApplications-Oslo.pdfObservabilityForModernApplications-Oslo.pdf
ObservabilityForModernApplications-Oslo.pdfAmazon Web Services
 
Control system Lab record
Control system Lab record Control system Lab record
Control system Lab record Yuvraj Singh
 

Similar to Modeling an ODE: 3 different approaches - Part 1 (20)

CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Lhy tutorial gui(1)
Lhy tutorial gui(1)Lhy tutorial gui(1)
Lhy tutorial gui(1)
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Chapitre 08
Chapitre 08Chapitre 08
Chapitre 08
 
Algoritmos
AlgoritmosAlgoritmos
Algoritmos
 
Unit-1 Mod-Sim.ppt
Unit-1 Mod-Sim.pptUnit-1 Mod-Sim.ppt
Unit-1 Mod-Sim.ppt
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
How to Analyze the Results of LinearPrograms—Part 1 Prelimi.docx
How to Analyze the Results of LinearPrograms—Part 1 Prelimi.docxHow to Analyze the Results of LinearPrograms—Part 1 Prelimi.docx
How to Analyze the Results of LinearPrograms—Part 1 Prelimi.docx
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Programming II hiding, and .pdf
 Programming II hiding, and .pdf Programming II hiding, and .pdf
Programming II hiding, and .pdf
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalRxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android Montréal
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2
 
Java8
Java8Java8
Java8
 
ObservabilityForModernApplications-Oslo.pdf
ObservabilityForModernApplications-Oslo.pdfObservabilityForModernApplications-Oslo.pdf
ObservabilityForModernApplications-Oslo.pdf
 
procedures
proceduresprocedures
procedures
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Control system Lab record
Control system Lab record Control system Lab record
Control system Lab record
 

More from Scilab

Statistical Analysis for Robust Design
Statistical Analysis for Robust DesignStatistical Analysis for Robust Design
Statistical Analysis for Robust DesignScilab
 
Electric motor optimization
Electric motor optimizationElectric motor optimization
Electric motor optimizationScilab
 
Asteroidlanding - Scilab conference 2019 Keynote
Asteroidlanding - Scilab conference 2019 KeynoteAsteroidlanding - Scilab conference 2019 Keynote
Asteroidlanding - Scilab conference 2019 KeynoteScilab
 
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...Scilab
 
Scilab and Xcos for Very Low Earth Orbits satellites modelling
Scilab and Xcos for Very Low Earth Orbits satellites modellingScilab and Xcos for Very Low Earth Orbits satellites modelling
Scilab and Xcos for Very Low Earth Orbits satellites modellingScilab
 
X2C -a tool for model-based control development and automated code generation...
X2C -a tool for model-based control development and automated code generation...X2C -a tool for model-based control development and automated code generation...
X2C -a tool for model-based control development and automated code generation...Scilab
 
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...Scilab
 
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCosAircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCosScilab
 
Scilab for real dummies j.heikell - part 1
Scilab for real dummies j.heikell - part 1Scilab for real dummies j.heikell - part 1
Scilab for real dummies j.heikell - part 1Scilab
 
Multiobjective optimization and Genetic algorithms in Scilab
Multiobjective optimization and Genetic algorithms in ScilabMultiobjective optimization and Genetic algorithms in Scilab
Multiobjective optimization and Genetic algorithms in ScilabScilab
 
Scilab optimization workshop
Scilab optimization workshop Scilab optimization workshop
Scilab optimization workshop Scilab
 
INRA @ Scilab Conference 2018
INRA @ Scilab Conference 2018INRA @ Scilab Conference 2018
INRA @ Scilab Conference 2018Scilab
 
Qualcomm @ Scilab Conference 2018
Qualcomm @ Scilab Conference 2018Qualcomm @ Scilab Conference 2018
Qualcomm @ Scilab Conference 2018Scilab
 
Sanofi @ Scilab Conference 2018
Sanofi @ Scilab Conference 2018Sanofi @ Scilab Conference 2018
Sanofi @ Scilab Conference 2018Scilab
 
University of Applied Science Esslingen @ Scilab Conference 2018
University of Applied Science Esslingen @ Scilab Conference 2018University of Applied Science Esslingen @ Scilab Conference 2018
University of Applied Science Esslingen @ Scilab Conference 2018Scilab
 
DLR @ Scilab Conference 2018
DLR @ Scilab Conference 2018DLR @ Scilab Conference 2018
DLR @ Scilab Conference 2018Scilab
 
Fraunhofer IIS @ Scilab Conference 2018
Fraunhofer IIS @ Scilab Conference 2018Fraunhofer IIS @ Scilab Conference 2018
Fraunhofer IIS @ Scilab Conference 2018Scilab
 
Arcelormittal @ Scilab Conference 2018
Arcelormittal @ Scilab Conference 2018Arcelormittal @ Scilab Conference 2018
Arcelormittal @ Scilab Conference 2018Scilab
 
CNES @ Scilab Conference 2018
CNES @ Scilab Conference 2018CNES @ Scilab Conference 2018
CNES @ Scilab Conference 2018Scilab
 
Scilab Conference 2018 - Welcome to the Community
Scilab Conference 2018 - Welcome to the CommunityScilab Conference 2018 - Welcome to the Community
Scilab Conference 2018 - Welcome to the CommunityScilab
 

More from Scilab (20)

Statistical Analysis for Robust Design
Statistical Analysis for Robust DesignStatistical Analysis for Robust Design
Statistical Analysis for Robust Design
 
Electric motor optimization
Electric motor optimizationElectric motor optimization
Electric motor optimization
 
Asteroidlanding - Scilab conference 2019 Keynote
Asteroidlanding - Scilab conference 2019 KeynoteAsteroidlanding - Scilab conference 2019 Keynote
Asteroidlanding - Scilab conference 2019 Keynote
 
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
 
Scilab and Xcos for Very Low Earth Orbits satellites modelling
Scilab and Xcos for Very Low Earth Orbits satellites modellingScilab and Xcos for Very Low Earth Orbits satellites modelling
Scilab and Xcos for Very Low Earth Orbits satellites modelling
 
X2C -a tool for model-based control development and automated code generation...
X2C -a tool for model-based control development and automated code generation...X2C -a tool for model-based control development and automated code generation...
X2C -a tool for model-based control development and automated code generation...
 
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
 
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCosAircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
 
Scilab for real dummies j.heikell - part 1
Scilab for real dummies j.heikell - part 1Scilab for real dummies j.heikell - part 1
Scilab for real dummies j.heikell - part 1
 
Multiobjective optimization and Genetic algorithms in Scilab
Multiobjective optimization and Genetic algorithms in ScilabMultiobjective optimization and Genetic algorithms in Scilab
Multiobjective optimization and Genetic algorithms in Scilab
 
Scilab optimization workshop
Scilab optimization workshop Scilab optimization workshop
Scilab optimization workshop
 
INRA @ Scilab Conference 2018
INRA @ Scilab Conference 2018INRA @ Scilab Conference 2018
INRA @ Scilab Conference 2018
 
Qualcomm @ Scilab Conference 2018
Qualcomm @ Scilab Conference 2018Qualcomm @ Scilab Conference 2018
Qualcomm @ Scilab Conference 2018
 
Sanofi @ Scilab Conference 2018
Sanofi @ Scilab Conference 2018Sanofi @ Scilab Conference 2018
Sanofi @ Scilab Conference 2018
 
University of Applied Science Esslingen @ Scilab Conference 2018
University of Applied Science Esslingen @ Scilab Conference 2018University of Applied Science Esslingen @ Scilab Conference 2018
University of Applied Science Esslingen @ Scilab Conference 2018
 
DLR @ Scilab Conference 2018
DLR @ Scilab Conference 2018DLR @ Scilab Conference 2018
DLR @ Scilab Conference 2018
 
Fraunhofer IIS @ Scilab Conference 2018
Fraunhofer IIS @ Scilab Conference 2018Fraunhofer IIS @ Scilab Conference 2018
Fraunhofer IIS @ Scilab Conference 2018
 
Arcelormittal @ Scilab Conference 2018
Arcelormittal @ Scilab Conference 2018Arcelormittal @ Scilab Conference 2018
Arcelormittal @ Scilab Conference 2018
 
CNES @ Scilab Conference 2018
CNES @ Scilab Conference 2018CNES @ Scilab Conference 2018
CNES @ Scilab Conference 2018
 
Scilab Conference 2018 - Welcome to the Community
Scilab Conference 2018 - Welcome to the CommunityScilab Conference 2018 - Welcome to the Community
Scilab Conference 2018 - Welcome to the Community
 

Recently uploaded

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 

Recently uploaded (20)

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 

Modeling an ODE: 3 different approaches - Part 1

  • 1. MODELING IN SCILAB APPROACH – PART In this tutorial we show how to model a physical system described by ODE using Scilab standard programming language. The same model solution is also described in Xcos and Xcos + Modelica in two other tutorials. Level This work is licensed under a Creative Commons Attribution www.openeering.com CILAB: PAY ATTENTION TO THE RIGHT PART 1 this tutorial we show how to model a physical system described by ODE using Scilab standard programming language. The same model solution is also described in Xcos and Xcos + Modelica in two other tutorials. under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. powered by RIGHT this tutorial we show how to model a physical system described by ODE using Scilab standard programming language. The same model solution is also
  • 2. LHY Tutorial Scilab www.openeering.com page 2/12 Step 1: The purpose of this tutorial In Scilab there are three different approaches (see figure) for modeling a physical system which are described by Ordinary Differential Equations (ODE). For showing all these capabilities we selected a common physical system, the LHY model for drug abuse. This model is used in our tutorials as a common problem to show the main features of each strategy. We are going to recurrently refer to this problem to allow the reader to better focus on the Scilab approach rather than on mathematical details. In this first tutorial we show, step by step, how the LHY model problem can be implemented in Scilab using standard Scilab programming. The sample code can be downloaded from the Openeering web site. 1 Standard Scilab Programming 2 Xcos Programming 3 Xcos + Modelica Step 2: Model description The considered model is the LHY model used in the study of drug abuse. This model is a continuous-time dynamical system of drug demand for two different classes of users: light users (denoted by ) and heavy users (denoted by ) which are functions of time . There is another state in the model that represents the decaying memory of heavy users in the years (denoted by ) that acts as a deterrent for new light users. In other words the increase of the deterrent power of memory of drug abuse reduces the contagious aspect of initiation. This approach presents a positive feedback which corresponds to the fact that light users promote initiation of new users and, moreover, it presents a negative feedback which corresponds to the fact that heavy users have a negative impact on initiation. Light users become heavy users at the rate of escalation and leave this state at the rate of desistance . The heavy users leave this state at the rate of desistance .
  • 3. LHY Tutorial Scilab www.openeering.com page 3/12 Step 3: Mathematical model The mathematical model is a system of ODE (Ordinary Differential Equation) in the unknowns: • L t , number of light users; • H t , number of heavy users; • Y t , decaying of heavy user years. The initiation function contains a “spontaneous” initiation τ and a memory effect modeled with a negative exponential as a function of the memory of year of drug abuse relative to the number of current light users. The problem is completed with the specification of the initial conditions at the time t . The LHY equations system (omitting time variable t for sake of simplicity) is L I L, Y a b L H bL gH Y H δY where the initiation function is I L, Y τ L max s"#$ , s · e'( ) *+ The LHY initial conditions are L t L H t H Y t Y
  • 4. LHY Tutorial Scilab www.openeering.com page 4/12 Step 4: Problem data (Model data) a : the annual rate at which light users quit b : the annual rate at which light users escalate to heavy use g : the annual rate at which heavy users quit δ : the forgetting rate (Initiation function) τ : the number of innovators per year s : the annual rate at which light users attract non-users q : the constant which measures the deterrent effect of heavy use s"#$ : the maximum rate of generation for initiation (Initial conditions) t : the initial simulation time; L : Light users at the initial time; H : Heavy users at the initial time; Y : Decaying heavy users at the initial time. Model data a 0.163 b 0.024 g 0.062 δ 0.291 Initiation function τ 50000 s 0.610 q 3.443 s"#$ 0.1 Initial conditions t 1970 L 1.4 7 108 H 0.13 7 108 Y 0.11 7 108
  • 5. LHY Tutorial Scilab www.openeering.com page 5/12 Step 5: Scilab programming With the standard approach, we solve the problem using the ODE function available in Scilab. This command solves ordinary differential equations with the syntax described in the right-hand column. In our problem we have: • Y0 equal to 9 , , : since the unknowns are , and Y ; • t0 equal to 1970; • t generated using the command t = Tbegin:Tstep:(Tend+100*%eps); which generates a regularly spaced vector starting from Tbegin and ending at Tend with a time-step Tstep. A tolerance is added to ensure that the value Tend is reached. See also linspace command for an alternative solution. • f a function of three equations in three unknowns which represents the system to be solved. The syntax of the ode function is y=ode(y0,t0,t,f) where at least the following four arguments are required: • Y0 : the initial conditions [vector] (the vector length should be equal to the problem dimension and its values must be in the same order of the equations. • t0 : the initial time [scalar] (it is the value where the initial conditions are evaluated); • t : the times steps at which the solution is computed [vector] (Typically it is obtained using the function LINSPACE or using the colon : operator); • f : the right-hand side of system to be solved [function, external, string or list].
  • 6. LHY Tutorial Scilab www.openeering.com page 6/12 Step 6: Roadmap We implement the system in the following way: • Create a directory where we save the model and its supporting functions. The directory contains the following files: - A function that implements the initiation function; - A function that implements the system to be solved; - A function that plots the results. • Create a main program that solves the problem; • Test the program and visualize the results. • LHY_Initiation.sci : The initiation function • LHY_System.sci : The system function • LHY_Plot.sci : The plotting function • LHY_MainConsole.sce : The main function. Step 7: Create a working directory We create a directory, name it "model" in the current working folder (LHYmodel) as shown in figure.
  • 7. LHY Tutorial Scilab www.openeering.com page 7/12 Step 8: Create the initiation function Now, we implement the following formula ; , < max ="#$ , = · >'? @ A+ as a Scilab function. The function returns the number of initiated individuals ; per years using the current value of , and its parameters <, =, B, ="#$ . We decide to implement the function parameters as a Scilab mlist data structure. The following code is saved in the file "LHY_Initiation.sci" in the model directory. function I=LHY_Initiation(L, H, Y, param) // It returns the initiation for the LHY model. // // Input: // L = number of light users, // H = number of heavy users, // Y = decaying heavy user years, // param is a structure with the model parameters: // param.tau = number of innovators per year, // param.s = annual rate at which light users // attract non-users, // param.q = deterrent effect of heavy users constant, // param.smax = maximum feedback rate. // // Output: // I = initiation. // // Description: // The initiation function. // Fetching tau = param.tau; s = param.s; q = param.q; smax = param.smax; // Compute s effective seff = s*exp(-q*Y./L); seff = max(smax,seff); // Compute initiation (vectorized formula) I = tau + seff.*L; Endfunction
  • 8. LHY Tutorial Scilab www.openeering.com page 8/12 Step 9: Create the system function Now, we implement the right hand side of the ODE problem as requested by the Scilab function ode: ; , C All the model parameters are stored in the "param" variables in the proper order. The following code is saved in the file "LHY_System.sci" in the model directory. function LHYdot=LHY_System(t, LHY, param) // The LHY system // Fetching LHY system parameters a = param.a; b = param.b; g = param.g; delta = param.delta; // Fetching solution L = LHY(1,:); H = LHY(2,:); Y = LHY(3,:); // Evaluation of initiation I = LHY_Initiation(L, H, Y, param); // Compute Ldot Ldot = I - (a+b)*L; Hdot = b*L - g*H; Ydot = H - delta*Y; LHYdot = [Ldot; Hdot; Ydot]; Endfunction
  • 9. LHY Tutorial Scilab www.openeering.com page 9/12 Step 10: A data plot function The following function creates a data plot, highlighting the maximum value of each variables. The name of the function is "LHY_Plot.sci" and shall be saved in the model directory. This function draws the values of L, H, Y and I and also computes their maximum values. function LHY_Plot(t, LHY) // Nice plot data for the LHY model // Fetching solution L = LHY(1,:); H = LHY(2,:); Y = LHY(3,:); // Evaluate initiation I = LHY_Initiation(L,H,Y, param); // maximum values for nice plot [Lmax, Lindmax] = max(L); tL = t(Lindmax); [Hmax, Hindmax] = max(H); tH = t(Hindmax); [Ymax, Yindmax] = max(Y); tY = t(Yindmax); [Imax, Iindmax] = max(I); tI = t(Iindmax); // Text of the maximum point Ltext = msprintf(' ( %4.1f , %7.0f)',tL,Lmax); Htext = msprintf(' ( %4.1f , %7.0f)',tH,Hmax); Ytext = msprintf(' ( %4.1f , %7.0f)',tY,Ymax); Itext = msprintf(' ( %4.1f , %7.0f)',tI,Imax); // Plotting of model data plot(t,[LHY;I]); legend(['Light Users';'Heavy users';'Memory';'Initiation']); // Vertical line set(gca(),"auto_clear","off"); xpolys([tL,tH,tY,tI;tL,tH,tY,tI],[0,0,0,0;Lmax,Hmax,Ymax,Imax]); // Text of maximum point xstring(tL,Lmax,Ltext); xstring(tH,Hmax,Htext); xstring(tY,Ymax,Ytext); xstring(tI,Imax,Itext); xlabel('Year'); set(gca(),"auto_clear","on"); endfunction
  • 10. LHY Tutorial Scilab www.openeering.com page 10/12 Step 11: The main program The function implements the main program to solve the system. The code is saved in the file "LHY_MainConsole.sce" in the working directory. In the main program we use the function GETD to import all the developed functions contained in our model directory. // Testing the model using US drug data clc // Import LHY functions getd('model'); // Setting LHY model parameter param = []; param.tau = 5e4; // Number of innovators per year (initiation) param.s = 0.61; // Annual rate at which light users attract // non-users (initiation) param.q = 3.443; // Constant which measures the deterrent // effect of heavy users (initiation) param.smax = 0.1;// Upper bound for s effective (initiation) param.a = 0.163; // Annual rate at which light users quit param.b = 0.024; // Annual rate at which light users escalate // to heavy use param.g = 0.062; // Annual rate at which heavy users quit param.delta = 0.291; // Forgetting rate // Setting initial conditions Tbegin = 1970; // Initial time Tend = 2020; // Final time Tstep = 1/12 // Time step (one month) L0 = 1.4e6; // Light users at the initial time H0 = 0.13e6; // Heavy users at the initial time Y0 = 0.11e6; // Decaying heavy user at the initial time // Assigning ODE solver data y0 = [L0;H0;Y0]; t0 = Tbegin; t = Tbegin:Tstep:(Tend+100*%eps); f = LHY_System; // Solving the system LHY = ode(y0, t0, t, f); // Plotting of model data LHY_Plot(t, LHY);
  • 11. LHY Tutorial Scilab www.openeering.com page 11/12 Step 12: Running and testing Change the Scilab working directory to the current project working directory (e.g. cd 'D:ManoloLHYmodel') and run the command exec('LHY_MainConsole.sce',-1). This command runs the main program and plots a chart similar to the one shown on the right. Step 13: Exercise #1 Modify the main program file such that it is possible to compare the “original model” with the same model where the parameter a 0.2. Try to plot the original result and the new simulation results in the same figure for a better comparison. Hints: Some useful functions scf(1) Set the current graphic figure subplot(121) Divide a graphics window into a matrix of sub-windows (1 row, 2 columns and plot data on first sub-window) subplot(122) Divide a graphics window into a matrix of sub-windows (1 row, 2 columns and plot data on second sub-window) a=gca() Return handle to the current axes a.data_bounds=[1970,0;2020,9e+6]; Set bound of axes data
  • 12. LHY Tutorial Scilab www.openeering.com page 12/12 Step 14: Concluding remarks and References In this tutorial we have shown how the LHY model can be implemented in Scilab with a standard programming approach. On the right-hand column you may find a list of references for further studies. 1. Scilab Web Page: Available: www.scilab.org. 2. Scilab Ode Online Documentation: http://help.scilab.org/docs/5.3.3/en_US/ode.html. 3. Openeering: www.openeering.com. 4. D. Winkler, J. P. Caulkins, D. A. Behrens and G. Tragler, "Estimating the relative efficiency of various forms of prevention at different stages of a drug epidemic," Heinz Research, 2002. http://repository.cmu.edu/heinzworks/32. Step 15: Software content To report a bug or suggest some improvement please contact Openeering team at the web site www.openeering.com. Thank you for your attention, Manolo Venturin ------------------- LHY MODEL IN SCILAB ------------------- ---------------- Directory: model ---------------- LHY_Initiation.sci : Initiation function LHY_Plot.sci : Nice plot of the LHY system LHY_System.sci : LHY system -------------- Main directory -------------- ex1.sce : Solution of the exercise LHY_MainConsole.sce : Main console program license.txt : The license file