0% found this document useful (0 votes)
36 views

Introduction To MATLAB: Biosystems Analysis and Modeling

This document provides an overview of MATLAB and its capabilities for biosystems analysis and modeling. It introduces MATLAB basics like variables, matrices, plots, conditional statements, loops, and numerical methods. It also describes how to access and install MATLAB through Oregon State University's site license. Examples shown include plotting functions, matrix operations, conditional logic, for/while loops, and numerical differentiation. Useful MATLAB links are provided for additional tutorials, examples, and documentation.

Uploaded by

Neal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Introduction To MATLAB: Biosystems Analysis and Modeling

This document provides an overview of MATLAB and its capabilities for biosystems analysis and modeling. It introduces MATLAB basics like variables, matrices, plots, conditional statements, loops, and numerical methods. It also describes how to access and install MATLAB through Oregon State University's site license. Examples shown include plotting functions, matrix operations, conditional logic, for/while loops, and numerical differentiation. Useful MATLAB links are provided for additional tutorials, examples, and documentation.

Uploaded by

Neal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

9/21/2015

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

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)

Biosystems Analysis and Modeling

Simulink Basics
Simulink Models

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Getting MATLAB

Introduction

Two choices
1)

2)

For engr students access MATLAB through CITRIX


Environments requires no local software. Access via the MyCoE website:
(http://engr.oregonstate.edu/computing/personal/149
Install MATLAB locally using OSUs site license. Instructions
are at: http://engr.oregonstate.edu/computing/mathworks. You
will have to provide your ONID email as described in the
instructions. You will be able to use MATLAB on your local
machine.

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

MATLAB stands for MATrix LABoratory

High performance language for technical computing

MATLAB is both computer programming language and


software environment

The MATLAB environment is command oriented

There are a number of add-on software modules, called


toolbox , available for MATLAB, that are used to for
specialized computations or specific tasks

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

MATLAB Variables

Workspace

Command
Window

Variable names in MATLAB are case sensitive

Variables are not declared before they are used

Variable names can contain up to 63 characters

a=3
A=5

(MATLAB 6.5 and newer)

Variables names cannot start with number. They must


start with a letter and can be followed by digits, letters or
underscore

Command
History

ans

Comments can be added to MATLAB using (%) symbol

Built-in variables:

Default variable name for results

e.g.

pi

Value of

9/21/2015

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

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

The following functions populate matrices

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

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

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

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Subsetting - Colon Operator (:)

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

J:D:K is the same as [J, J+D, ...,

B = A(2:3,:) = [4 5 6; 7 8 9]

J+m*D] where m = fix((K-J)/D)

C = A(3:4,1:2) = [7 8; 0 -1]

A = [1,2,3; 4,5,6; 7,8,9; 0,-1,-2]

0.5000
1.2000
3.0000

0.4286
0.7778
0

A=
1
3
4
6
7
9
0
-2

A(:) is all the elements of A,


regarded as a single column

>> A./B
ans =
0.3333
5.0000
Inf

Creating new matrix from existing matrix

J:K is the same as [J, J+1, ..., K]

Element Division

>> A.*B
ans =

6 10
11 16
12 -4

0.0577
0.3654
1.6538

A(:,J) is the Jth column of A

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

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Some Useful Built-in Functions

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)

linspace (Linearly spaced points)


linspace(x1,x2) will generate 100 points between x1 and x2
linspace(x1,x2,n) will generate n points between x1 and x2
>>linspace(0,10,6)
ans=
0 2

10

logspace (logarithmically spaced points)


logspace(x1,x2) will generate 100 points between 10x1 and 10x2
logspace(x1,x2,n) will generate n points between 10 x1 and 10x2
>>logspace(-1,2,4)
ans=
0.1000

1.0000 10.0000 100.0000

9/21/2015

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Plots

Mesh, surf and contours

plot() (for plotting 2-D data) and plot3() (for plotting 3D data)
= 0.1 sin()

x=-7.5:0.5:7.5; % generate vector x


y=x;
% generate vector y
[X,Y]=meshgrid(x,y); % generate evenly
spaced data points in x-y plane (grid
points)
r=sqrt(X.^2+Y.^2)+eps; % eps is a small no.
(built in) to avoid division by zero
z=sin(r)./r;
subplot(2,2,1); mesh(X,Y,z); % mesh plot of
z
subplot(2,2,2);
surf(X,Y,z); % surface plot of z
subplot(2,2,3);
contour(X,Y,z,20); % generate 20 no. of 2-D
contour lines for z
subplot(2,2,4);
contour3(X,Y,z,20); % Same contour plot in
3-D

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

x = 0:0.1:12; % defines x for value from 0


to 12 by 0.1
a = 3; b =2; c = -.2; % parameter values
y = a + b*x + c*x.*x; % values for y. Note
x.*x because we want scalar multiplication
plot(x,y)
8
7
6

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

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Conditional Controls and Loops


if

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

Solve a simple equation with 3


variables (free fall velocity)

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

Using Function Files

Save the file as freefall.m

Call the function in the MATLAB command window


v=freefall(10,70,.25);
fprintf('The velocity is %0.2f m/s \n',v)

end
end

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Example - Numerical Differentiation

Example Numerical Differentiation (continued)


2
Given: =

DIFFERENTIATION

Forward difference approximation


+1
=
+ ()

Backward difference approximation


1
=
+ ()

Centered difference approximation


+1 1
=
+ (2 )
2

Find f(x) using centered approximation at x=2

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

Biosystems Analysis and Modeling Department of Biological & Ecological Engineering

Some Useful Matlab Links


General Overview of Matlab:

http://web.mit.edu/acmath/matlab/unified/fall07/UnifiedMatlabF07/UnifiedIntroMatlabF07.pdf

http://www.mathworks.com/help/index.html - Matlab web site

http://www.mathworks.com/products/matlab/examples.html - many programming examples

http://www.mathworks.com/help/index.html - Step-by-Step guides to Matlab and Simulink

http://www.math.mtu.edu/~msgocken/intro/intro.html - many programming examples

http://people.duke.edu/~hpgavin/matlab.html - links to additional Matlab tutorials


Regression:

MIT: http://web.mit.edu/acmath/matlab/course16/16.62x/16.62x_Matlab.pdf - slide deck that


includes general overview of MatLab + data analysis functions + regression functions

MathWorks: http://www.mathworks.com/help/matlab/data_analysis/linear-regression.html - covers


linear regression only
Videos:

http://www.mathworks.com/products/matlab/videos.html - From the makers of Matlab

https://www.youtube.com/playlist?list=PL7CAABC40B2825C8B YouTube Matlab playlist

You might also like