intro tom at lab
TRANSCRIPT
9/21/2015
1
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Introduction to MATLAB
Biosystems Analysis and Modeling
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Overview
• Introduction
• Plots
• Conditional statements
• Numerical method
• Initial value problems (ODE’s) – Single ODE
– 2 simultaneous ODE’s (predator-prey equations)
– 3 simultaneous ODE’s (Bioreactor problem)
• Simulink Basics
• Simulink Models
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Getting MATLAB
• Two choices
1) For engr students – access MATLAB through CITRIX
Environments – requires no local software. Access via the My-
CoE website:
(http://engr.oregonstate.edu/computing/personal/149
2) Install MATLAB locally using OSU’s 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
Introduction
• 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
Command
Window
Command
History
Workspace
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
MATLAB Variables
• Variable names in MATLAB are case sensitive
• Variables are not declared before they are used
• Variable names can contain up to 63 characters
(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
• ans Default variable name for results
• Comments can be added to MATLAB using (%) symbol
• Built-in variables: e.g. pi Value of Π
a=3
A=5
9/21/2015
2
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Basic Commands
• Define variables
• Calculating mode
(different arithmetic
operations)
• Rounding off
– Round
– Floor
– Ceil
– Fix
a=3
b=5
c=a+b
d=pi*b
d=pi/d
a=1.6689
round(a)
ceil(a)
floor(a)
fix(a)
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Matrices
>> A=[2 3 4; 5 6 7]
A =
2 3 4
5 6 7
A(i,j) gives the element of ith row
and jth column
A (2,3)=7
A(1,2) + A(2,3) = 9
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
>> ones(3,2)
ans =
1 1
1 1
1 1
>> zeros(2,3)
ans =
0 0 0
0 0 0
>> zeros(2)
ans =
0 0
0 0
>> rand(2,3)
ans =
0.6557 0.8491 0.6787
0.0357 0.9340 0.7577
The following functions populate matrices
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Matrix/Vector Operations
>> A*B
ans =
5 23 13
21 71 61
33 77 137
>> A.*B
ans =
3 8 21
5 30 63
0 27 0
>> A=[1 2 3; 5 6 7; 8 9 0]
A =
1 2 3
5 6 7
8 9 0
A’ =
1 5 8
2 6 9
3 7 0
>>B=[3 4 7; 1 5 9; 0 3 -4]
B =
3 4 7
1 5 9
0 3 -4
A+B =
4 6 10
6 11 16
8 12 -4
>> A/B
ans =
0.2885 0.1346 0.0577
1.8269 -0.4808 0.3654
3.2692 -1.8077 1.6538
>> A./B
ans =
0.3333 0.5000 0.4286
5.0000 1.2000 0.7778
Inf 3.0000 0
Populate a matrix
Transpose a matrix
Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
Matrix Addition
Matrix Multiplication Matrix Division
Element Multiplication Element Division
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Subsetting - Colon Operator (:)
• J:K is the same as [J, J+1, ..., K]
• J:D:K is the same as [J, J+D, ...,
J+m*D] where m = fix((K-J)/D)
• A(:) is all the elements of A,
regarded as a single column
• 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)]
A =
1 2
3
4 5
6
7 8
9
0 -1
-2
B =
4 5 6
7 8 9
Creating new matrix from existing matrix
A = [1,2,3; 4,5,6; 7,8,9; 0,-1,-2]
B = A(2:3,:) = [4 5 6; 7 8 9]
C = A(3:4,1:2) = [7 8; 0 -1]
C =
7
8
0
-1
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Some Useful Built-in Functions
• 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)
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Array Creation
• 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 4 6 8 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 10x1 and 10x2
>>logspace(-1,2,4)
ans=
0.1000 1.0000 10.0000 100.0000
9/21/2015
3
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Plots plot() (for plotting 2-D data) and plot3() (for plotting 3-
D data)
Example 1:
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)
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)
xlabel('x'), ylabel('y')
subplot(2,1,2)
plot3(x,y,z) xlabel('x'), ylabel('y'),zlabel('z')
title('Example','FontSize',12)
Example 2: 𝑥 = 𝑒−0.1𝑡 ∗ sin(𝑡)
𝑦 = 𝑒−0.1𝑡 ∗ cos(𝑡) 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥2
0 2 4 6 8 10 12-2
-1
0
1
2
3
4
5
6
7
8
-0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-1
-0.5
0
0.5
1
x
y
-1 -0.50 0.5
1
-1-0.5
00.5
10
50
x
Example
y
z
-0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-1
-0.5
0
0.5
1
x
y
-1 -0.50 0.5
1
-1-0.5
00.5
10
50
x
Example
y
z
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Mesh, surf and contours
-100
10
-10
0
10-1
0
1
-100
10
-10
0
10-1
0
1
-5 0 5
-5
0
5
-50
5
-50
5
-1
0
1
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
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Conditional Controls and Loops
for loop
i = 0;
for t=0:0.02:50
i = i+1;
y(i) = cos(t)
end
while loop
While condition
statements
end
Early termination
EXAMPLE:
x=24;
while(1)
x=x-5
if x<0, break
end
end
if
else
elseif
a = 4;
b = 4;
if (a<b)
j = -1;
elseif (a>b)
j = 2;
else
j = 3
end
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Using Function Files
• Solve a simple equation with 3
variables (free fall velocity)
𝑣 =𝑔 ∗ 𝑚
𝑐𝑑∗ tanh
𝑔 ∗ 𝑚
𝑐𝑑∗ 𝑡
function v=freefall(t,m,cd)
g=9.81; %acceleration
v=sqrt(g*m/cd)*tanh(sqrt(g*m/cd)*t);
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)
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Example - Numerical Differentiation
DIFFERENTIATION
• Forward difference approximation
𝑓′ 𝑥𝑖 =𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖
ℎ+ 𝑂(ℎ)
• Backward difference approximation
𝑓′ 𝑥𝑖 =𝑓 𝑥𝑖 − 𝑓 𝑥𝑖−1
ℎ+ 𝑂(ℎ)
• Centered difference approximation
𝑓′ 𝑥𝑖 =𝑓 𝑥𝑖+1 − 𝑓 𝑥𝑖−1
2ℎ+ 𝑂(ℎ2)
h h
Biosystems Analysis and Modeling – Department of Biological & Ecological Engineering
Example – Numerical Differentiation (continued)
𝑓 𝑥 = 𝑒−2𝑥 − 𝑥
Find f’(x) using centered approximation at x=2
clear all
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
Analytical solution
f’(2) = -1.0366
Given:
9/21/2015
4
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