MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School Table of Contents Course Objectives ............................................................................................................... Housekeeping Commands .................................................................................................... Scalars, Vectors and Matrices ............................................................................................... Plotting ............................................................................................................................. Defining Functions ............................................................................................................. Solving ODE ..................................................................................................................... Acknowledgements ............................................................................................................. 1 1 1 2 6 7 9 Course Objectives At the end of the course, students should master basic MATLAB syntax and language needed to numerically solve a determinsitic mathematical model of an infectious disease. Housekeeping Commands Good practice to begin a new script this way unless you want to keep variables for some reason. clear; clc; clear + *varname*; Scalars, Vectors and Matrices Define some scalars a=10; b=5; c=0.5; Operations on scalars d=c*a+a/b; Define some vectors W=[1 4 2 -1 3 8 6 0 0 1 14]; X=0:1:a; Y=0:0.5:b; Z=linspace(a,b,11); X1=ones(1,11); X0=zeros(1,11); Operations on vectors Xt=X'; X2=b*X1-X; 1 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School XY=X.*Y; Define some matrices A=diag(Z); B=[W;X;Y]; C=zeros(11,11); C(d,:)=X; Xid=eye(10); Xuni=rand(3,4); Xnorm=randn(3,4); Bblock=[ones(3) zeros(3,2);zeros(2,3) 4*eye(2)]; Operations with Matrices D=A+C; Ax=A*Xt; AD=A*D; AD2=A.*D; Selecting elements of vectors and matrices: Using indicies x2=X2(2); A23=A(2,3); Vectorised approach for selecting elements of vectors and matrices X2indexed=X2(1:2:11); XYindexed=XY(1:2:11); Aindexed1=A(:,1:2:11); Using a vector of 1 and 0 indicator=(X2>=0); X2selected=X2(indicator); XYselected=XY(indicator); Aselected1=A(:,indicator); Aselected2=A(indicator,:); Aselected3=A(indicator,indicator); Plotting Basic Plotting figure; plot(X,Ax); xlabel('X'); ylabel('Ax'); title('Ax vs X') print('-depsc','AX') 2 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School Multiple plots on a single figure figure; plot(X,Ax); hold all plot(X, 2+Ax,'LineWidth',2) xlabel('X'); ylabel('Ax'); title('Multiple plots on a single figure') legend('AX','2+Ax','Location','NorthWest') print('-depsc','AXm') 3 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School Loops and Figures f1=figure(); f2=figure(); for i=1:11 if X2(i)>0 figure(f1); plot(X,i*Ax); hold on; else figure(f2); plot(X,-i*Ax); hold all; end end figure(f1); print('-depsc','iAXplus') figure(f2); print('-depsc','iAXminus') 4 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School 5 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School Defining Functions The function is defined in the file contact1.m. c1=contact1(2,2,2); c2=contact1(1,2,4); c3=contact1(16,3,2.75); The fuction in contact1.m is also written so that it can accept a vector for any of the parameters. This allows matlab to perform multiple computations efficiently and can shorten complicated code. C1=contact1(X,2,2); C2=contact1(2,X2,2); If more than one vector is used, they must be the same length. The function will be calculated componentwise. C3=contact1(X,X2,2); Another way to define simple functions F=@(p) p.^2.+1; f2=F(2); F2=F(X); Calling figure this way just starts a new figure figure; plot(F2,X,'r--') print('-depsc','Fx') 6 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School Solving ODE Defining the vector field: The SIR model Sdot=Lambda-lambda*S*I-dS*S; Idot=lambda*S*I-gamma*I-dI*I; Rdot=gamma*I-dR*R; is defined in the file SIRModel.m as follows: function f=SIRModel(t,y,par) %This function takes parameters %t in R %y in R3 %par a vector of 6 parameters. if length(par)<6 disp('Error:Not enough parameters') f=0; D=0; else %Relabel values of parameters Lambda = par(1); lambda = par(2); gamma = par(3); dS = par(4); dI = par(5); dR = par(6); 7 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School %Relabel values of y S=y(1); I=y(2); R=y(3); %Calculate N N=S+I+R; %Calculate RHS of differential equation Sdot=Lambda-lambda*S*I-dS*S; Idot=lambda*S*I-gamma*I-dI*I; Rdot=gamma*I-dR*R; f=[Sdot Idot Rdot]'; end Solving the ODE: If SIRModel.m and Solver.m are in the same directory, running Solver.m should give the following output. 8 MATLAB Crash Course for 2012 PIMS-MPrime-CDM Summer School Acknowledgements This tuorial is based on a one-class crash course given by R. de Boer in a Mathematical Epidemiology class. All the m-files used in this tutorial are de Boer's while S. Akinwumi used Matlab's publish function to organise the tutorial with minor addition. Complete matlab documentation can be found at http://www.mathworks.com/help/techdoc/. Published with MATLAB® 7.13 9