Introduction To MATLAB: Biosystems Analysis and Modeling
Introduction To MATLAB: Biosystems Analysis and Modeling
Overview
Introduction to MATLAB
Introduction
Plots
Conditional statements
Numerical method
Initial value problems (ODEs)
Single ODE
2 simultaneous ODEs (predator-prey equations)
3 simultaneous ODEs (Bioreactor problem)
Simulink Basics
Simulink Models
Getting MATLAB
Introduction
Two choices
1)
2)
MATLAB Variables
Workspace
Command
Window
a=3
A=5
Command
History
ans
Built-in variables:
e.g.
pi
Value of
9/21/2015
Basic Commands
Define variables
Calculating mode
(different arithmetic
operations)
Rounding off
Matrices
A(i,j) gives the element of ith row
and jth column
>> A=[2 3 4; 5 6 7]
A (2,3)=7
A=
a=3
b=5
c=a+b
d=pi*b
d=pi/d
Round
Floor
Ceil
Fix
2
5
3
6
A(1,2) + A(2,3) = 9
4
7
a=1.6689
round(a)
ceil(a)
floor(a)
fix(a)
>> eye(3)
>> ones(3,2)
>> zeros(2,3)
>> zeros(2)
ans =
ans =
ans =
ans =
1
0
0
0
1
0
0
0
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
>> rand(2,3)
ans =
0
0
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577
Matrix/Vector Operations
Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
Populate a matrix
>> A=[1 2 3; 5 6 7; 8 9 0]
A=
1 2 3
5 6 7
8 9 0
Transpose a matrix
A =
1
2
3
5
6
7
Matrix Multiplication
>>B=[3 4 7; 1 5 9; 0 3 -4]
B=
3 4 7
1 5 9
0 3 -4
>> A*B
ans =
5 23 13
21 71 61
33 77 137
Element Multiplication
Matrix Addition
A+B =
8
9
0
4
6
8
Matrix Division
>> A/B
ans =
0.2885 0.1346
1.8269 -0.4808
3.2692 -1.8077
3 8 21
5 30 63
0 27 0
B = A(2:3,:) = [4 5 6; 7 8 9]
C = A(3:4,1:2) = [7 8; 0 -1]
0.5000
1.2000
3.0000
0.4286
0.7778
0
A=
1
3
4
6
7
9
0
-2
>> A./B
ans =
0.3333
5.0000
Inf
Element Division
>> A.*B
ans =
6 10
11 16
12 -4
0.0577
0.3654
1.6538
A(J:K) is [A(J),A(J+1),...,A(K)]
A(:,J:K) is [A(:,J),A(:,J+1),...,A(:,K)]
B=
2
4
7
5
8
-1
56
89
C=
7
8
0
-1
Array Creation
mean(A)
max(A), min (A)
sum(A)
sqrt(A)
median(A)
eig(A)
rank(A)
rot90(A), flip(A)
std(A)
det(A)
inv(A)
tril(A)
triu(A)
10
9/21/2015
Plots
plot() (for plotting 2-D data) and plot3() (for plotting 3D data)
= 0.1 sin()
Example 2:
= 0.1 cos()
= + + 2
Example 1:
t= linspace(0,50,200);
x=exp(-0.1.*t).*sin(t);
y=exp(-0.1*t).*cos(t);
z=t;
subplot(2,1,1)
plot(x,y)
1
xlabel('x'), ylabel('y')
0.5
subplot(2,1,2)
0
plot3(x,y,z)
-0.5
xlabel('x'), ylabel('y'),zlabel('z')
-1
-0.8 -0.6 -0.4 -0.2
0
0.2
0.4
0.6
title('Example','FontSize',12)
y
0.8
Example
4
1
50
3
0.5
z
2
y
0
1
-0.5
-1
-0.8
-0.6
-0.4
-0.2
0.2
-1
0.4
0.6
0.8
0.5
10
12
-0.5
-1
-1
-0.5
0.5
-1
10
10
-1
10
10
0
-10 -10
0
-10 -10
1
0
-1
-5
5
0
-5
-5
-5
50
z
-2
Example
0
1
0.5
-0.5
-1
-1
-0.5
0.5
for loop
else
elseif
a = 4;
b = 4;
if (a<b)
i = 0;
for
t=0:0.02:50
i = i+1;
y(i) =
cos(t)
end
j = -1;
elseif (a>b)
j = 2;
else
while loop
While
condition
statements
end
tanh
function v=freefall(t,m,cd)
g=9.81; %acceleration
v=sqrt(g*m/cd)*tanh(sqrt(g*m/cd)*t);
Early termination
EXAMPLE:
x=24;
while(1)
x=x-5
if x<0,
break
j = 3
end
end
end
DIFFERENTIATION
Analytical solution
clear all
f(2) = -1.0366
f=@(x)exp(-2*x)-x; % Describe the f(x)
x=2; % What point do you want to find the derivative
h=[0.5:-0.05:0.1]; % make a vector [0.5 0.45 0.1 ]
n=length(h);
for i=1:1:n
df(i)=(f(x+h(i))-f(x-h(i)))/(2*h(i));% Centered
finite-difference approximation
end
fprintf('h
df\n');
for i=1:1:n
fprintf('%0.2f %0.4f\n',h(i),df(i))
end
9/21/2015
http://web.mit.edu/acmath/matlab/unified/fall07/UnifiedMatlabF07/UnifiedIntroMatlabF07.pdf