60
SHORT COURSE (2) ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool Masoud Jamei, PhD, SMIEEE [email protected] , [email protected] Senior Scientific Advisor and Head of M&S at Simcyp Honorary Lecturer, School of Medicine, The University of Sheffield

½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

SHORT COURSE (2)

½ Day – Introduction to MATLAB as a

General Modelling and Simulation Tool

Masoud Jamei, PhD, SMIEEE [email protected], [email protected]

Senior Scientific Advisor and Head of M&S at Simcyp Honorary Lecturer, School of Medicine, The University of Sheffield

Page 2: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Module MED603 - Data Analysis in PKPD - Introduction to MATLAB Masoud Jamei 2

14:50 Overview and MATLAB syntax 15:40 Break 15:50 Programming In MATLAB 16:40 Break 16:50 Visualisation, algorithm and ordinary

differential equations 17:50 End of the short course

AGENDA

5th January 2009

Page 3: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Lecture 1 – Overview and MATLAB Syntax

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 3

MATLAB can be accessed via the University managed computers

MATLAB and Simulink Student Version Release 2007 (£40-£50) Some limitations, such as the allowable size of a matrix

MATLAB is a program for doing technical computing.

Page 4: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

MATLAB (MATrix LABoratory) History

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 4

(pre-MATLAB) Fortran subroutines for solving linear

(LINPACK) and eigenvalue (EISPACK) problems

MATLAB developed as an interactive system to access

LINPACK and EISPACK

In the 1980’s, MATLAB was rewritten in C with more

functionality (such as plotting routines)

The Mathworks, Inc. was created in 1984

Page 5: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

What Applications MATLAB is Used For

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 5

Control Design

Signal Processing and Communications

Image Processing

Test and Measurement

Financial Modeling

Statistics, optimization and curve fitting

Add-on toolboxes

Bioinformatics

PK/PD Modelling

Page 6: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Advantages & Disadvantages

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 6

Relatively easy to learn

Its code is optimized to be relatively quick when performing matrix operations

Can be used like a calculator or as a programming language

Since interpreted, errors are easier to find and fix

Primarily procedural, though it has some object-oriented elements

It is NOT a general purpose programming language

Since it is an interpreted language (generally slower than a compiled language such as C++)

Page 7: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

MATLAB Windows

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 7

Command window

Editor window

Command History window

Current Directory and Workspace

windows

Figure window

Help window

Array Editor window

Page 8: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Command Window

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 8

MATALB can be used as a calculator

>> 5+2 ans = 7

>> 5*4+2^3-9 ans = 19

>> 5*4^2+3 ans = 83

>> 4/5*3 ans = 2.4000

>> (5*4)+(2^3)-9 ans = 19

>> 4/(5*3) ans = 0.2667

Page 9: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Variables

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 9

MATLAB allows you to assign a value to a variable

x=10 - Should be read as: “x is assigned a value of 10”

Variables are case sensitive, X is not the same as x!

You can use the variables in subsequent calculations

Unassigned variables cause the following error:

??? Undefined function or variable 'z'.

Page 10: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Data Types

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 10

Array

Logical Char Numeric

Double

Single

int8, uint8,

int16, uint16,

int32, uint32

int64, uint64

Cell Structure

User classes

Java classes Function handle

Page 11: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

MATLAB Special Variables

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 11

ans Default variable name for results

pi Value of π

eps Smallest incremental number (2.2204e-016)

inf Infinity

NaN Not a number e.g. 0/0

realmin The smallest usable positive real number (1.7977e+308)

realmax The largest usable positive real number (2.2251e-308)

Page 12: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Variables

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 12

>> x=3 x = 3

>> x+3 ans = 6

>> x=x*3 x = 9

>>y= x ^ 2 y = 81

Variable names must start with a letter

It can be a combination of letters, underscore, numbers

Some names are reserved MATLAB commands/variables, for

example pi, if, for, etc

White space between calculations does not matter – can be used

to make them clear

Page 13: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Variables - Matrices

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 13

Almost all variables in MATLAB are matrices

Group of numbers arranged into rows and columns

Single value or scalar means a matrix with one row and one

column

A vector is a one dimensional matrix either one row or one

column

A matrix is usually two dimensional but can have more!

Page 14: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Variables - Matrices

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 14

>> x=[1 2 3] x = 1 2 3

>> x=[1,2,3] x = 1 2 3

>> x=[1;2;3] x = 1 2 3

>> x=1:3 x = 1 2 3

>> x=1:1:3 x = 1 2 3

>> x=[1 2 3 ] x = 1 2 3

>> x=[1 2 3; 4 5 6] x = 1 2 3 4 5 6

>> x=[1 2 3 4 5 6] x = 1 2 3 4 5 6

>> x(1,2) ans = 2

>> x(1:2, 2:3) ans = 2 3 5 6

Page 15: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Variables - Matrices

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 15

>> x=ones(2) x = 1 1 1 1

>> x=ones(2,3) x = 1 1 1 1 1 1

>> x=zeros(2) x = 0 0 0 0

>> y=zeros(2,3, 2) y(:,:,1) = 0 0 0 0 0 0 y(:,:,2) = 0 0 0 0 0 0

>> [r, c, d]=size(y) r=2 c=3 d=2

>> size(y,2) ans = 3

>> x=diag([2;3]) x = 2 0 0 3

Page 16: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

MATLAB Windows

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 16

Page 17: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

.mat files

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 17

Saved version of MATLAB variables

MATLAB-specific

save(‘MyData.mat’);

save MyData x z

load(‘MyData.mat’);

load MyData

Page 18: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Arithmetic Operators

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 18

Plus +

Minus -

Matrix multiply *

Array multiply .*

Matrix power ^

Array power .^

Slash or right matrix divide /

Right array divide ./

Page 19: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Relational and Logical Operators

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 19

Equal ==

Not equal ~=

Less than <

Greater than >

Less than or equal <=

Greater than or equal >=

Element-wise logical AND &

Element-wise logical OR |

Logical NOT ~

Page 20: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Built-in Elementary Functions

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 20

square root sqrt(x)

real nth root nthroot(x,n)

base 10 log log10(x)

natural log log(x)

absolute value abs(x)

exponential (ex) exp(x)

sign of x sign (x)

round towards infinity

ceil(x)

round towards minus infinity

floor(x)

sin(x) cos(x) tan(x) asin(x) sinh(x)

Trigonometric Functions Elementary Math Functions

Page 21: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Lecture 2 – Programming in MATLAB

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 21

MATLAB can be used for specific tasks such as:

• Performing calculations for different sets of parameters

• Visualisation and plot of data

• Statistical analysis

• Solving differential equations simulating models

• Parameter estimation and fitting models to data

Programming is the preparation of a step-by-step instruction for a computer to follow.

Page 22: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Structured Programming

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 22

MATLAB starts executing each statement from beginning to the end.

However, we in certain condition need to change the execution sequence.

Sometime we need to repeat a specific segment of statements over and over again.

We may also need to repeat a specific segment of statements over and over again but for different sets of parameters.

Page 23: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Elements of MATLAB Programming Language

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 23

Expressions

Flow Control blocks

Conditional

Loops (Iterations)

Scripts

Functions

Page 24: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Programming in MATLAB

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 24

Use the input command to accept information

Use disp and fprintf to control the output

>> CL=input('Enter CL ?') Enter CL ? 4 CL = 4

The fprintf command is more flexible than the disp command, and allows you to put both variables and text onto the same line

>> fprintf('CL= %2.2f \n',CL) CL= 4.00

>> disp(CL) 4

Page 25: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Flow Control-if Statement

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 25

A>B C<1

An if statement is used to determine the computing path based on a given condition.

x=2;y=4 if x<y disp(x) else disp(y) end

x=2;y=4; if x<y disp(x) elseif x==y disp([x,y]) else disp(y) end

D~=2 E>1 & E<5

When a variable represents an array, the if statement is only true if all the members of the array meet the criteria.

Page 26: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Flow Control-switch Statement

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 26

Switch among several cases based on expression.

switch switch_expr case case_expr, statement, ..., statement case {case_expr1, case_expr2, case_expr3,...} statement, ..., statement ... otherwise, statement, ..., statement end

method = 'Bilinear'; switch lower(method) case {'linear','bilinear'} disp('Method is linear') case 'cubic' disp('Method is cubic') case 'nearest' disp('Method is nearest') otherwise disp('Unknown method.') end Method is linear

Page 27: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Flow Control- for Loops

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 27

A for loop allows you to repeat a set of statements for a specified number of times.

for variable = expr statement; ... statement; end

A = ones(4,8); sum = 0; for i = 1:4 for j = [1:4, 5,7:8] sum = sum + A(i,j); end end fprintf(’sum = %d\n’,sum);

Page 28: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Flow Control- while Loops

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 28

A while loop allows you to repeat a set of statements until a specific condition is met.

while expr statement; ... statement; end

A = ones(4,8); sum = 0; while i <= 4 while j < 6 sum = sum + A(i,j); j = j+1; end i = i+1; end fprintf(’sum = %d\n’,sum);

Avoid loops whenever you can!

Page 29: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Scripts

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 29

A script is a set of statements/commands without input arguments, they do not return any value.

A = ones(4,8); sum = 0; while i <= 4 while j < 6 sum = sum + A(i,j); j = j+1; end i = i+1; end fprintf(’sum = %d\n’,sum);

Page 30: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

M Files

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 30

m-files contain scripts or functions to do some tasks.

If you want to keep your work, you need to create an m-file

File->New->m-file (or any other simple editor like notepad)

Page 31: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

M Files

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 31

Type your commands in the edit window that opens

m-files are called by their names and they can be nested

Give m-files suitable names

m-files can be groups in different folders (should be added to the

MATLAB path)

Avoid having m-files with the same name in your folders

Don’t forget to save your m file (after any modifications it should

be saved again)

Page 32: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

M Files – An Example

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 32

Page 33: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Set Path

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 33

MATALB looks for functions in its path, so don’t forget to use “Set Path” command to add your function location.

Page 34: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Functions

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 34

Functions are reusable building blocks for more complex programs

Functions are useful to modularise your code

MATALB has many built-in functions

Some of the MATALB functions belong to specific toolboxes

User-defined functions can be added to MATLAB

Usually one file for each function is defined

Two important types of MATLAB functions are:

Named functions defined in m-files

Anonymous function defined via function handles

Page 35: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

User Defined Functions

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 35

User defined functions consist of

• Name

• Input argument(s)

• Output

All variables defined inside a function are ONLY locally known

Functions are ONLY aware of the input arguments

Functions name follow the same rules as variables

Save the function in an m-file using the function name

A function can be called inside another function

Page 36: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

User Defined Functions

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 36

function s=myFunc(x) % This function double the input values % of an array x s=x*2;

Input variable which can be an array

The output (return) variable which should be assigned inside the function

The function name

Page 37: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

A Function Example

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 37

function [avg, stdev, num] = sum_stat(X, Y) Z=X+Y; avg= mean(Z); stdev= std(Z); num = length(Z);

>> sum_stat(3,4) ans = 7

If output variables are not defined then only the first one is shown.

>> [a b c]=sum_stat(3,4) a = 7 b = 0 c = 1

Page 38: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

A Function Example

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 38

>> [a b c]=sum_stat(3,4) a = 7 b = 0 c = 1

The original output parameter names defined in the function is of NO significance. New variable names are defined as the function is called! The ORDER of the parameters is key!!!!

>> Z=3; [a b c]=sum_stat(Z,4);

Some input parameters can of course be variables

Z outside the function is independent of Z inside the function. The scope (the range of validity of variables) of internal parameters is limited to inside the function.

Page 39: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Anonymous Functions

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 39

kel= @(CL, V) CL/V

Parameter list Name of function handle

Function body Function handle constructor

>> kel(10,20) ans = 0.5000

Note: Avoid using too much anonymous functions

Page 40: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Lecture 3 – Visualisation, Algorithm and ODE

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 40

Figures

Axes

Handles (a handle is a variable that refers to an object)

If you can see it, it has a handle.

Some things you can’t see have handles, too.

Can use handles to manipulate properties.

Overview of MATLAB Graphics

Page 41: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Handles & Properties

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 41

Figures, axes, and graphics have handles

These objects have properties

These properties can be altered

The figure window is erased every time a new graph is drawn

Use the hold on command to draw additional plots onto an existing graph

Don’t forget using hold off!

Page 42: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

MATALB Graphs

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 42

2D line plots

Pseudocolor Images

Histograms

3D shapes

Page 43: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

2D Plots

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 43

>> plot([1:10])

plot(x, y) x=1:50; plot(x, x.^2 )

x=1:50; plot(x, x.^2,'r', x, 10*x, '*')

title('This is my figure') xlabel('X') ylabel('Y') axis([1 10 -1 30])

Page 44: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

2D Plots

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 44

x=[1 1 1 2 3 3 4 4 5 6 6 6 6 7 7 7 ]; hist(x,7)

hist(x, m) produces a histogram bar plot of the results - where m is a scalar, uses m bins

x = -2.9:0.2:2.9; bar(x,exp(-x.*x));

t=0:.01:2*pi; polar(t,abs(sin(2*t).*cos(2*t)));

x=-2:0.1:2; y=sin(x); e = rand(size(x))/8; errorbar(x,y,e);

Page 45: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

3D Plots

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 45

Surface plots z=peaks(25); surf(z); colormap(jet);

Surface plots with shading z=peaks(25); surfl(z); shading interp; colormap(pink);

Mesh plots z=peaks(25); mesh(z); colormap(hsv)

Contour

Slice

010

2030

0

10

20

30-10

-5

0

5

10

010

2030

0

10

20

30-10

-5

0

5

10

010

2030

0

10

20

30-10

-5

0

5

10

Page 46: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Other Handy Functions

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 46

subplot(rows,columns,number)

axis([xmin xmax ymin max])

set(handle,’PropertyName’,value, . . .);

get(handle,’PropertyName’)

Figure(#) %creates new figure window

Logarithmic Plots

• semilogx

• semilogy

• loglog

Page 47: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Algorithm

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 47

Flow chart: a graphic representation of the logical sequence of instructions.

Algorithm: a sequence of instructions designed to solve a specific problem.

Process

Decision

Terminal

X=0;

X>2

Start

Page 48: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Algorithm

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 48

Start

Input X

X>0

X=X/2

X=X*2

end

Output X

X=input(‘enter X? ‘); If (X>0) X=X*2; else X=X/2; end disp(X);

Yes

No

Page 49: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Solving Linear First Order Differential Equations

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 49

Generally, a system of differential equations looks like:

yi’(t)= fi (y1, y2, ..., yn, t) i=1 ...n

SaS Ak

dt

dA AS(0) = Dose

AS’(t)= -kaAS The objective is to find As(t)?

tk

SSae)0(AA

Page 50: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Solving ODE

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 50

Two well-known numerical methods to solver ODE: Euler Method Runge-Kutta Methods

The most commonly used one is the 4th order Runge-Kutta method.

Runge-Kutta methods are implemented in MATLAB as ODE23 and ODE45 functions.

Page 51: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Using MATLAB to Solve ODE

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 51

Define your ODE in a function and save it in an m-file.

Call ode45 (ode23) using the function handle: [t, As]=ode45(@oral, [0, 10], [10]);

function dAs=oral(t,As) dAs=-2*As;

Plot the results: Plot(t, As);

Page 52: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Using MATLAB to Solve a Two-Compartment Model

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 52

A2 = C2V2 A1 = C1V1

R k12

k21

k10

dA1/dt = R + k21A2 – k12A1 – k10A1

dA2/dt = k12A1 – k21A2

Page 53: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Using MATLAB to Solve a Two-Compartment Model

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 53

>> [t, As]=ode45(@two_comp, [0, 10], [0 0]); >> plot(t, As) >> xlabel ('time (h)') >> ylabel('Amount in both compartments (mg)')

function dA=two_comp(t,A) k10=1; k12=1; k21=.1; dA = zeros(2,1); % a column vector dA(1) = 1 + k21*A(2)- k12*A(1)- k10*A(1); dA(2)= k12*A(1)-k21*A(2);

Page 54: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Using MATLAB to Solve a Two-Compartment Model

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 54

Page 55: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Using MATLAB to Solve a Two-Compartment Model

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 55

0 2 4 6 8 100

0.5

1

1.5

2

2.5

3

3.5

4

time (h)

Am

ount

in b

oth

com

part

ments

(m

g)

Page 56: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Using MATLAB to Solve a Two-Compartment Model

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 56

function dA=two_comp(t,A) k10=1; k12=1; k21=.1; dA = zeros(2,1); % a column vector if (t>4); R=0; else R=1; end dA(1) = R + k21*A(2)- k12*A(1)- k10*A(1); dA(2)= k12*A(1)-k21*A(2);

>> [t, As]=ode45(@two_comp, [0, 10], [0 0]); plot(t, As(:,1))

Page 57: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Outputs on Separate Graphs

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 57

0 1 2 3 4 5 6 7 8 9 100

0.2

0.4

0.6

0.8

Am

ount

in c

om

part

ment

1 (

mg)

0 1 2 3 4 5 6 7 8 9 100

0.5

1

1.5

2

Time (h)

Am

ount

in c

om

part

ment

2 (

mg)

Page 58: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

Passing Parameters into ode45

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 58

function dA=two_comp(t,A, flag, InfusionDuration) k10=1; k12=1; k21=.1; dA = zeros(2,1); % a column vector if (t> InfusionDuration); R=0; else R=1; end dA(1) = R + k21*A(2)- k12*A(1)- k10*A(1); dA(2)= k12*A(1)-k21*A(2);

An indicator for defining parameters

An input parameter

>> *t, As+=ode45(‘two_comp’, *0, 10+, *0 0+, [], 4);

(an empty array)

Input parameter

Don’t function use handler anymore

Page 60: ½ Day Introduction to MATLAB as a General Modelling and Simulation Tool … · 2016-12-21 · SHORT COURSE (2) ½ Day – Introduction to MATLAB as a General Modelling and Simulation

End of Short Course 2

Masoud Jamei Module MED603 - Data Analysis in PKPD - Introduction to MATLAB 60

If you have any more questions drop me an email at:

[email protected], [email protected]