You are on page 1of 42

Modeling Physical & Biological

Systems Using MATLAB

Examples
Simple Pendulum

ⅆ𝟐 𝜽 𝒈
𝟐
+ 𝐬𝐢𝐧 𝜽 = 𝟎
ⅆ𝒕 𝒍
Cycloid

𝑥(𝑡) = 𝑟 𝜃(𝑡) − sin 𝜃(𝑡)

𝑦(𝑡) = 𝑟 1 − cos 𝜃(𝑡)


Brownian Motion

Wiener process

Represented by stochastic process


Simulation of Earth’s orbit around the Sun

Application of Kepler's law

𝟐
𝟒𝝅
𝒑𝟐 = 𝒂𝟑
𝑮 𝑴𝟏 + 𝑴𝟐
Double Pendulum

ⅆ𝟐 𝜽𝟏 ⅆ𝟐 𝜽𝟐
𝒎𝟏 + 𝒎𝟐 𝒍𝟏 𝟐
+ 𝒎𝟐 𝒍𝟐 𝟐
𝐜𝐨𝐬 𝜽𝟐 − 𝜽𝟏
ⅆ𝒕 ⅆ𝒕
ⅆ𝟐 𝜽𝟐
= 𝒎𝟐 𝒍𝟐 − 𝒎𝟏 + 𝒎𝟐 𝒈 𝐬𝐢𝐧 𝜽𝟏
ⅆ𝒕𝟐

--------------- (1)

ⅆ𝟐 𝜽𝟐 ⅆ𝟐 𝜽𝟏
𝒍𝟐 𝟐 + 𝒍𝟏 𝟐 𝐜𝐨𝐬 𝜽𝟐 − 𝜽𝟏 =
ⅆ𝒕 ⅆ𝒕

ⅆ𝜽𝟐𝟏 --------------- (2)


−𝒍𝟏 𝐬𝐢𝐧 𝜽𝟐 − 𝜽𝟏 − 𝒈 𝐬𝐢𝐧 𝜽𝟐
ⅆ𝒕
Epidemic Models
Anderson & May 1975, 1991

SIR (Susceptible-Infected-Recovered) Model

ⅆ𝒔
= −𝜷𝒔𝑰
ⅆ𝒕 • Rabies
ⅆ𝑰 • Tuberculosis
= 𝜷𝒔𝑰 − 𝜸𝑰
ⅆ𝒕 • Small pox

ⅆ𝑹 • Rubella
= 𝜸𝑰
ⅆ𝒕 • Measles
Epidemic Models
Anderson & May 1975, 1991

SEIR (Susceptible-Exposed-Infected-Recovered) Model

ⅆ𝐒 𝑺𝑰 𝜷 ∶Parameter controlling how often a


= 𝝁 𝑵 − 𝑺 − 𝜷 − 𝝂𝑺
ⅆ𝒕 𝑵 susceptible infected contact results in
a new exposure
ⅆ𝑬 𝒔𝑰
=𝜷 − 𝝁+𝝈 𝑬
ⅆ𝒕 𝑵 𝜸 :The rate an infected recovers and
moves into the resistance phase.
ⅆ𝑰
= 𝝈𝑬 − 𝝁 + 𝜸 𝐈 𝝈: The rate at which exposed person
ⅆ𝒕
becomes infective.
ⅆ𝑹
= 𝜸𝑰 − 𝝁𝑹 + 𝝂𝑺 𝝁 :The natural mortality rate
ⅆ𝒕 (unrelated to disease).

𝑵= 𝑺+𝑬+𝑰+𝑹 𝝂 : Vaccination rate


Bacterial Growth (Growth of Microorganisms)

𝟏 N: Bacterial density
ⅆ𝑵 𝑪
= 𝜶𝟏 𝑵−𝑵
ⅆ𝒕 𝟏+𝑪
C : Nutrient concentration

ⅆ𝑪 𝑪
=− 𝑵 − 𝑪 + 𝜶𝟐
ⅆ𝒕 𝟏+𝑪
Red Blood Cell Production Model

𝑹𝒏+𝟏 = 𝟏 − 𝒇 𝑹𝒏 + 𝑴𝒏

𝑴𝒏+𝟏 = 𝜸𝒇𝑹𝒏

• Rn : No. of RBC’s in circulation on day n


• Mn : No. of RBC’s produced by marrow on day n
• f : fraction of RBC removed by spleen
• 𝜸 : production constant
Lorenz Attractor

ⅆ𝒙
= 𝒑𝒚 − 𝒑𝒙
ⅆ𝒕

ⅆ𝒚
= −𝒙𝒛 + 𝒓𝒙 − 𝒚
ⅆ𝒕

ⅆ𝒛
= 𝒙𝒚 − 𝒃𝒛
ⅆ𝒕
Basics of MATLAB Programming
MATLAB: Salient features

➢ MATLB is a high-level computing language optimized for scientific

computation. It makes many common mathematical operations used by

scientists& engineers simple and fast.

➢ MATLAB is an interpreted language – code is compiled each time it is run.

➢ Excellent at manipulating tabular data (arrays, matrices etc.).

➢ MATLAB has lots of built-in functions for data analysis, interpolation,

linear algebra, statistical analysis, image and signal processing etc.


Introduction to MATLAB environment
An interactive computing environment

MATLAB desktop

MATLAB works in two modes


1. Interactive mode
2. Script mode
Introduction to MATLAB environment
An interactive computing environment

MATLAB desktop

MATLAB works in two modes


1. Interactive mode
2. Script mode

Main Area
Current working
directory

Tool bar
Command
window

workspace

Command
history
MATLAB data types

MATLAB supports variety of data types.

• Integer.
• Real (double and single precision).
• Complex number.
• Character strings.
• Structures: mixed data types.

Specialty of MATLAB: unlike many low-level programming languages,


Variables and data types do not have to be declared before hand.
Important MATLAB built-in operators & functions
Arithmetic operators

+ : summation
- : subtraction
* : multiplication
/ :division
= :equal
^ :power
sqrt :square root
Important MATLAB built-in operators & functions
Arithmetic operators Relational operators
+ : summation == : equal
- : subtraction ~= : not equal
* : multiplication < : less than
/ :division > : greater than
<= : less than or equal
= :equal
>= : greater than or equal
^ :power
sqrt :square root
Important MATLAB built-in operators & functions
Arithmetic operators Relational operators
+ : summation == : equal
- : subtraction ~= : not equal
* : multiplication < : less than
> : greater than
/ :division
<= : less than or equal
= :equal
>= : greater than or equal
^ :power
sqrt :square root Logical operators
& : logical AND
~ : logical NOT
! : logical OR
all : determine if all array elements are nonzero
any : determine if any array elements are nonzero
false : logical FALSE
true : logical TRUE
find : find indices and values of nonzero elements
islogical : determine if input is logical
logical : converts numeric values to logical
Important MATLAB built-in operators & functions
Trigonometric functions
Arithmetic operators Relational operators
sin : sine function in radian
cos : cosine function in radian + : summation == : equal
asin : inverse sin - : subtraction ~= : not equal
acos : inverse cosine * : multiplication < : less than
sinh : Hyperbolic sin > : greater than
/ :division
cosh : Hyperbolic cosine <= : less than or equal
= :equal
asinh : inverse hyperbolic sin >= : greater than or equal
^ :power
acosh : inverse hyperbolic cosine
deg2rad : convert angle from degree to radian
sqrt :square root

Logarithmic functions Logical operators


log : natural logarithm
& : logical AND
log10 : base 10 logarithm
~ : logical NOT
log2 : base 2 logarithm
! : logical OR
exp : exponential function
all : determine if all array elements are nonzero
nthroot : real n th root of real numbers
any : determine if any array elements are nonzero
reallog : natural logarithm for nonengative real arrays
false : logical FALSE
Constants true : logical TRUE
inf : infinity find : find indices and values of nonzero elements
NaN : not a number islogical : determine if input is logical
pi : Ratio of circles circumference to its diameter logical : converts numeric values to logical
Variable creation & their manipulations
• MATLAB is designed for performing matrix operations.
• All MATLAB variables are matrices.

Scalar: Matrix with one row and one column.


Vector: Matrix with one column or one row.

Variables/Matrices can be created using different ways.

1. Manual entry
2. using built-in functions
3. Passing output from user defined/built-in function.
Variable Naming

o MATLAB variables are case sensitive

o MATLAB file/variable name must start with an alphabet

o Variables are matrices


1. Manual entry
1. Manual entry

Scalar variable
creation
1. Manual entry

Row vector
creation
1. Manual entry

Column vector
creation
1. Manual entry

Matrix creation
1. Manual entry MATLAB reserves i and j for
representing complex numbers

Try i^2

Matrix with
complex
elements
2. Matrix creation using built-in functions
Vector/Matrix creation using built-in functions

varName = eye(nRow, nColumn) % create Identity matrix

varName = zeros(nRow, nColumn) % create matrix with zero elements

varName = diag(vector or matrix) % create or extract diagonal elements

varName = rand(nRow, nColumn) % create matrix with random numbers

varName = ones(nRow, nColumn) % create matrix filled with 1

Best practice: create matrices using built-in function


Vectorization
Vectorization is the use of single compact expressions that operates on all elements of a vector
without explicitly writing the code for a loop.

▪ Powerful feature of MATLAB


▪ Useful method for efficient and optimal processing of matrices.
▪ Compact & faster execution of MATLAB code.

Applications
o Array operations
o Finding part of vector
o 2D array indexing
o Getting a row
o Getting a column
o Using indexing to assign values to rows and columns
Vector Operators
Symbol Operation
.* Element-by-element multiplication
./ Element-by-element “right” division
.\ Element-by-element “left” division
.^ Element-by-element power

▪ Use “.” when performing element-by-element vector multiplication, division,


and power operations

▪ Addition & subtraction always perform element-by-element operation

What is the difference between

a .* b and a * b operations?
Colon Notation
It is a powerful and important tool in the effective use of MATLAB

Application of Colon Notation


o Create vectors
o Refer to or extract range of matrix elements

Default increment = 1
Syntax But start value,
start value : increment : end value increment, and end
value need not to be
integers
examples x = 0 : 0.1 : 1 % create an 11 element row vector
x = (1 : 5)’ % create a 5 element column vector
‘ --- MATLAB operator to calculate transpose
Find out the difference between (1 : 5)’ and 1:5’
Use colon notation to refer to an entire column or row
x =[ 1 2 3; 4 5 6; 7 8 9];
a = x( :, 1 ) % extract entire first column
b = x( :, 2 ) % extract entire 2nd column
c = x( 3, : ) % extract entire 3rd row

Referring subsets of columns and/or rows


d = x( 2:3, 1) % extract 2nd and 3rd elements of 1st column
e = x( 1:2, 2:3 ) % create a subset consists of 1st two rows & 2nd and 3rd column
Create compact expressions
f = ones(10,10); % create 10 by 10 matrix with ones
f( 3:6,3:6 ) = rand( 4, 4 ) % reassign random values to 3rd to 6th row and column elements

Convert a matrix to column vector


g = rand( 3, 4 )
h = g( : ) % create column vector by appending the columns of matrix g
Delete Row or Column of a matrix

x = rand(10, 8); % create 10 by 8 matrix filled with random numbers


x(9:10,:) = []; % Remove 9th and 10th row of matrix x

y = rand(6, 8);
y(:, 7:8) = [];

Append Row or Column

x =[ 1 2 3; 1 4 2;4 3 1];
x(4,:) = [6 8 1]; try
x = -2*pi : pi/10 : 2*pi;
x=x’;
y = [ 2 3; 4 5; 6 7]; [x sin(x) cos(x)]
y(:,3)=[1; 2; 3];
Linear Algebra: Built-in MATLAB functions

sum : sum of vector elements


x = [ 1 4 5 7 8.5 9 0.25 3]; eig : find eigen values and eigen vectors

sum(x) [eigVal, eigVec]=eig(y)

mean : mean (average) of a vector rank : calculate rank of a matrix


rank(y)
mean(x)
inv : calculate inverse of a matrix
length : find length of the vector
inv(y)
length(x)
lu : factorize a matrix into lower and upper triangular
det : calculate determinant
[l, u]=lu(y)
y=[1 2 3; 4 5 6; 7 8 9];
det(y)
Solving system of linear equations

6x + 5y + 3z = 4 --------- (1)
2x + 4y + 7z = 2.3 --------- (2)
9x + 2y + 5z = 6.7 --------- (3)

How do we find x, y, & z?


Solving system of linear equations

6x + 5y + 3z = 4 --------- (1)
2x + 4y + 7z = 2.3 --------- (2)
9x + 2y + 5z = 6.7 --------- (3)

1) Using Matrix Inversion AC=B

a = [ 6 5 3; 2 4 7; 9 2 5];
b = [4; 2.3; 6.6];
c = inv(a)* b
2) Gaussian elimination

c = a\b
It returns a column vector x, y, & z

‘\’ operator solves system of eqns using Gaussian elimination


Thank You!

You might also like