This action might not be possible to undo. Are you sure you want to continue?
Vijay
8/29/2001
1
Tutorial on Matlab
This tutorial is intended to be a review of some concepts in Matlab about matrix algebra,
and ODE’s.
1. Matrix Algebra
I.1 Multiplication of matrices
Multiplication of matrices is defined in a way that reflects composition of the underlying
linear transformations and allows compact representation of systems of simultaneous
linear equations. The matrix product C = AB is defined when the column dimension of A
is equal to the row dimension of B, or when one of them is a scalar. If A is mbyp and B
is pbyn, their product C is mbyn. The product can actually be defined using
MATLAB's for loops, colon notation, and vector dot products.
for i = 1:m
for j = 1:n
C(i, j) = A(i,:)*B(:,j);
end
end
MATLAB uses a single asterisk to denote matrix multiplication. The next two examples
illustrate the fact that matrix multiplication is not commutative; AB is usually not equal to
BA.
A =
]
]
]
]
]
6 3 1
3 2 1
1 1 1
B =
]
]
]
]
]
2 9 4
7 5 3
6 1 8
X = A*B
X =
]
]
]
]
]
39 70 41
26 38 26
15 15 15
Y = B*A
MEEN 364
Vijay
8/29/2001
2
Y =
]
]
]
]
]
43 28 15
60 34 15
47 28 15
Rectangular matrix multiplications must satisfy the dimension compatibility conditions.
X = A*C
X=
]
]
]
]
]
70 51
41 31
19 17
Y = C*A gives
Error using ==> *
Inner matrix dimensions must agree. Anything can be multiplied by a scalar.
I.2 Solving Linear Equations
One of the most important problems in technical computing is the solution of
simultaneous linear equations. In matrix notation, this problem can be stated as follows.
Given two matrices A and B, does there exist a unique matrix X so that AX = B or
XA = B?
It is instructive to consider a 1by1 example.
Does the equation
have a unique solution? The answer, of course, is yes. The equation has the unique
solution x = 3. The solution is easily obtained by division.
The solution is not ordinarily obtained by computing the inverse of 7, that is 7

1
= 0.142857..., and then multiplying 7
1
by 21. This would be more work and, if 7
1
is
represented to a finite number of digits, less accurate. Similar considerations apply to sets
of linear equations with more than one unknown.
MATLAB solves such equations without computing the inverse of the matrix.
Although it is not standard mathematical notation, MATLAB uses the division
terminology familiar in the scalar case to describe the solution of a general system of
simultaneous equations. The two division symbols, slash, /, and backslash, \, are used for
the two situations where the unknown matrix appears on the left or right of the coefficient
matrix.
X = A\B Denotes the solution to the matrix equation AX = B.
X = B/A Denotes the solution to the matrix equation XA = B.
MEEN 364
Vijay
8/29/2001
3
You can think of "dividing" both sides of the equation AX = B or XA = B by A. The
coefficient matrix A is always in the "denominator."
The dimension compatibility conditions for X = A\B require the two matrices A and B to
have the same number of rows. The solution X then has the same number of columns as
B and its row dimension is equal to the column dimension of A. For X = B/A, the roles of
rows and columns are interchanged.
In practice, linear equations of the form AX = B occur more frequently than those of the
form XA = B. Consequently, backslash is used far more frequently than slash. The
remainder of this section concentrates on the backslash operator; the corresponding
properties of the slash operator can be inferred from the identity
(B/A)' = (A'\B')
The coefficient matrix A need not be square. If A is mbyn, there are three cases.
m = n
Square system. Seek an exact solution.
m > n
Over determined system. Find a least squares solution.
m < n
Underdetermined system. Find a basic solution with at most m nonzero
components.
I.3 Eigenvalues and Eigenvalue Decomposition
An eigenvalue and eigenvector of a square matrix A are a scalar and a vector v that
satisfy
With the eigenvalues on the diagonal of a diagonal matrix and the corresponding
eigenvectors forming the columns of a matrix V, we have
If V is nonsingular, this becomes the eigenvalue decomposition
Lets look at an example fro matrix A
A=
]
]
]
]
]
− −
−
− −
10 20 5
16 2 6
1 6 0
The statement lambda = eig (A) produces a column vector containing the eigenvalues.
For this matrix, the eigenvalues are complex.
MEEN 364
Vijay
8/29/2001
4
Lambda =
]
]
]
]
]
− −
+ −
−
i
i
6008 . 17 4645 . 2
0068 . 17 4645 . 2
0710 . 3
The real part of each of the eigenvalues is negative, so approaches zero as t increases.
The nonzero imaginary part of two of the eigenvalues, , contributes the oscillatory
component, , to the solution of the differential equation.
With two output arguments, eig computes the eigenvectors and stores the eigenvalues in a
diagonal matrix.
[V, D] = eig (A)
V =
]
]
]
]
]
− − −
+ − − − −
+ − −
6930 . 0 6930 . 0 4248 . 0
6447 . 0 2001 . 0 6447 . 0 2110 . 0 3553 . 0
1394 . 0 2003 . 0 1394 . 0 2003 . 0 8326 . 0
i i
i i
D=
]
]
]
]
]
− −
+ −
−
i
i
6008 . 17 4645 . 2 0 0
0 6008 . 17 4645 . 2 0
0 0 0710 . 3
II. ODEs
This section describes the process for solving ODE problems using one of the MATLAB
ODE solvers. It uses the Van Der Pol equation as an example. Solving ODE’s of the first
order using matlab is pretty straightforward. This section describes how to solve higher
order ODE’s.
1 Rewrite the Problem as a System of FirstOrder ODEs.
ODEs often involve a number of dependent variables, as well as derivatives of order
higher than one. To use the MATLAB ODE solvers, you must rewrite such equations as
an equivalent system of firstorder differential equations of the form
You can write any ordinary differential equation
as a system of firstorder equations by making the substitutions
MEEN 364
Vijay
8/29/2001
5
The result is an equivalent system of firstorder ODEs.
For example, you can rewrite the van der Pol equation (secondorder)
where is a scalar parameter, by making the substitution . The resulting
system of firstorder ODEs is
2 Code the System of FirstOrder ODEs in MATLAB.
Once you represent the equation as a system of firstorder ODEs, you can code it as a
function that a MATLAB ODE solver can use. The function must be of the form
dydt = odefun(t,y)
Although t and y must be the function's first two arguments, the function does not need to
use them. The output dydt, a column vector, is the derivative of y.
The code below represents the van der Pol system in a MATLAB function, vdp1. The
vdp1 function assumes that . and become elements y(1) and y(2) of a two
element vector.
function dydt = vdp1(t,y)
dydt = [y(2); (1y(1)^2)*y(2)y(1)];
Note that, although vdp1 must accept the arguments t and y, it does not use t in its
computations.
3 Apply a Solver to the Problem.
Decide which solver you want to use to solve the problem. Then call the solver and pass
it the function you created to describe the firstorder system of ODEs, the time interval on
which you want to solve the problem, and an initial condition vector.
For the van der Pol system, you can use ode45 on time interval [0 20] with initial values
y (1) = 2 and y (2) = 0.
MEEN 364
Vijay
8/29/2001
6
[t, y] = ode45 (@vdp1,[0 20],[2; 0]);
This example uses @ to pass vdp1 as a function handle to ode45. The resulting output is
a column vector of time point’s t and a solution array y. Each row in y corresponds to a
time returned in the corresponding row of t. The first column of y corresponds to , and
the second column to .
4 View the Solver Output.
You can simply use the plot command to view the solver output.
plot (t, y(:,1),'',t,y(:,2),'')
title ('Solution of van der Pol Equation, \mu = 1');
xlabel('time t');
ylabel('solution y');
legend('y_1','y_2')
Let us now consider solving more complicated ODE’s using Matlab. These are
differential equations whose coefficients are not constant. They generally represent non
linear systems.
Example A Bar supported by a wire and a horizontal plane
MEEN 364
Vijay
8/29/2001
7
Consider the system shown below. It consists of a bar BC of length l and mass m that is
supported by a mass less wire from point A to end B. End C of the bar rests on a
horizontal plane that lies a distance d below point A. The wire from point A to B has
length L and. Choose ‘l’, ‘L’, ‘m’ and ‘d’ as 1m, 1m, 5 Kg and 0.5m respectively. The
θ’and ‘φ’ are taken to be 0.4857 radians and 0.5233 radians respectively.
θ’and ‘φ’ with respect to time.
A
L
φ
B
d
C θ
Solution
The model for the above system is
Where T
c
represents the tension in the chord and N the normal reaction exerted on the bar
at the ground. Other parameters are clear from the diagram.
First we solve for
.
θ and θ. Then we substitute them into the following constraint
equations to obtain the values of
.
φ and φ. The constraint equations are the geometrical
relations, which can be derived from the above figure to be
The above model involves second derivatives of the variables and must be converted to
an equation involving first derivatives so that MATLAB can be used to solve the
differential equation. So with
¹
¹
¹
¹
¹
¹
¹
'
¹
¹
¹
¹
¹
¹
¹
¹
'
¹
+
−
+ −
·
¹
¹
¹
¹
¹
'
¹
¹
¹
¹
¹
¹
'
¹
]
]
]
]
]
]
]
]
]
−
− −
+
−
2
.
2
.
2
.
2
.
2
.
..
..
2
sin sin
cos
2
cos
sin
2
0
0 0 cos cos
0 cos sin sin
2
1 sin 0 cos
2
cos
2
) sin(
2
0
12
θ θ φ φ
θ θ φ φ
θ θ
φ
θ
φ θ
φ φ θ
φ θ
θ φ θ
l L
ml
mL
ml
w
N
T
L l
mL
ml
ml
l l ml
c
θ φ
θ θ φ φ
sin sin
0 cos cos
. .
l L d
l L
+ ·
· +
MEEN 364
Vijay
8/29/2001
8
); 4 (
); 3 (
); 2 (
); 1 (
.
.
y
y
y
y
·
·
·
·
φ
φ
θ
θ
The model becomes
¹
¹
¹
¹
¹
¹
¹
¹
¹
¹
¹
'
¹
¹
¹
¹
¹
¹
¹
¹
¹
¹
¹
¹
'
¹
+
−
+ − ·
¹
¹
¹
¹
¹
¹
¹
¹
¹
'
¹
¹
¹
¹
¹
¹
¹
¹
¹
¹
'
¹
]
]
]
]
]
]
]
]
]
]
]
−
− −
+
−
2
.
2
.
2
.
2
.
2
.
.
.
2
sin sin
cos
2
cos
sin
2
) 4 (
0
) 2 (
0 0 cos 0 cos 0
0 cos sin 0 sin
2
0
1 sin 0 0 cos
2
0
0 0 0 1 0 0
cos
2
) sin(
2
0 0
12
0
0 0 0 0 0 1
θ θ φ φ
θ θ φ φ
θ θ
φ
φ
θ
θ
φ θ
φ φ θ
φ θ
θ φ θ
l L
ml
mL
ml
w
y
y
N
T
L l
mL
ml
ml
l l ml
c
[T, Y] = ODEsolver ('F', TSPAN, Y0) with TSPAN = [T0 TFINAL] integrates
the system of differential equations y' = F (t,y) from time T0 to
TFINAL with initial conditions Y0. 'F' is a string containing the name
of an ODE file. Function F(T,Y) must return a column vector. Each row
in solution array Y corresponds to a time returned in column vector T.
To obtain solutions at specific times T0, T1, ..., TFINAL (all
increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL].
Some ODEsolvers can solve problems M(t,y)*y' = F(t,y) with a mass
matrix that is nonsingular.
Since the coefficient matrix is dependent on the states or the variables, a separate Mfile
has to be written which incorporates a switch/case programming, and a separate Mfile
for the mass matrix must also be written.
MATLAB Code
The code with the switch/case programming is given below.
% In order to solve this type of problem, the odefile needs
to have switch/case programming with a flag case of 'mass'.
When the flag is set to default (which means there is no
mass matrix) the function FF1.m is called. By setting the
option to ‘mass’ the function MM1.m is also called. As you
can see in the function indmot_ode1, when the flag is
'mass', the function MM1.m is called.
MEEN 364
Vijay
8/29/2001
9
function varargout=indmot_ode1(t,y,flag)
switch flag
case '' %no input flag
varargout{1}=FF1(t,y);
case 'mass' %flag of mass calls mass.m
varargout{1}=MM1(t,y);
otherwise
error(['unknown flag ''' flag '''.']);
end
To store the right hand side vector of the original model, a separate function file must be
written as shown below. Note that the name of the function is ‘FF1’, so this file must be
The following function contains the right hand side of the
differential equation of the form M(t,y)*y'=F(t,y) i.e. it
contains F(t,y).it is also stored in a separate file named,
FF1.m.
function yp=FF1(t,y)
l=1;
L=1;
m=5;
g=9.81;
w=m*g;
yp=zeros(6,1);
yp(1)=y(2);
yp(2)=0;
yp(3)=y(4);
yp(4)=w+(m*l/2)*sin(y(1))*(y(2)^2);
yp(5)=m*L*cos(y(3))*(y(4)^2)(m*l/2)*cos(y(1))*(y(2)^2);
yp(6)=L*sin(y(3))*(y(4)^2)+l*sin(y(1))*(y(2)^2);
Similarly, to store the coefficient matrix, a separate function file is written which is stored as
‘MM1.m’.
The following function contains the mass matrix. It is
separately stored in a file named, MM1.m
function n = MM1(t,y)
l=1;
L=1;
m=5;
g=9.81;
MEEN 364
Vijay
8/29/2001
10
w=m*g;
n1=[1 0 0 0 0 0];
n2=[0 (m*l^2)/12 0 0 (l/2)*sin(y(1)+y(3))
(l/2)*cos(y(1))];
n3=[0 0 1 0 0 0];
n4=[0 (m*l/2)*cos(y(1)) 0 0 sin(y(3)) 1];
n5=[0 (m*l/2)*sin(y(1)) 0 m*L*sin(y(3)) cos(y(3)) 0];
n6=[0 l*cos(y(1)) 0 L*cos(y(3)) 0 0];
n=[n1;n2;n3;n4;n5;n6];
To obtain the response, the main file should call the function ‘indmot_ode1.m’, which
has the switch/case programming which in turn calls the corresponding functions
depending on the value of the flag. For the main file to recognize the coefficient matrix,
the MATLAB command ODESET is used to set the mass to ‘M (t, y)’.
l=1;
L=1;
d=0.5;
tspan=[0 10]
options=odeset('mass','M(t,y)')
y0=[0.5233;0;1.0467;0;0;0]
[t,y]=ode113('indmot_ode1',tspan,y0,options)
%plot(t,y(:,1))
%grid
theta=y(:,1);
thetadot=y(:,2);
% solving the constraint equation to get the value of phi.
for i = 1:size(theta,1)
phi(i)=asin((dl*sin(theta(i)))/L);
end
% solving the constraint equation to get the value of
phidot.
for i = 1:size(theta,1)
phidot(i)=(l*(
thetadot(i))*cos(theta(i)))/(L*cos(phi(i)));
end
t1=t';
plot(t1,phi)
To plot the value of
.
θ with respect to time, change the variable in line 8 of the main code
from ‘y(:,1)’ to ‘y(:,2)’. This step is required because we initially assigned θ’ and
MEEN 364
Vijay
8/29/2001
11
y(2) to
.
θ. Once the values of ‘θ’ and
.
θ are known, they are substituted in the constraint
equation to get the values of ‘φ’ and
.
φ. The plots are attached below.
MEEN 364
Vijay
8/29/2001
12
MEEN 364
Vijay
8/29/2001
13
MEEN 364
Vijay
8/29/2001
14
III. FFT
Onedimensional fast Fourier transform.
Y = fft (X) returns the discrete Fourier transform (DFT) of vector X, computed with a fast
Fourier transform (FFT) algorithm = fft (X, n) returns the npoint DFT. If the length of X is
less than n, X is padded with trailing zeros to length n. If the length of X is greater than n,
the sequence X is truncated. When X is a matrix, the length of the columns are adjusted in
the same manner.
Examples
A common use of Fourier transforms is to find the frequency components of a signal
buried in a noisy time domain signal. Consider data sampled at 1000 Hz. Form a signal
containing 50 Hz and 120 Hz and corrupt it with some zeromean random noise:
t = 0:0.001:0.6;
x = sin(2*pi*50*t)+sin(2*pi*120*t);
y = x + 2*randn(size(t));
plot(y(1:50))
title('Signal Corrupted with ZeroMean Random Noise')
xlabel('time (seconds)')
It is difficult to identify the frequency components by looking at the original signal.
Converting to the frequency domain, the discrete Fourier transform of the noisy signal y
is found by taking the 512point fast Fourier transform (FFT):
Y = fft(y,512);
The power spectrum, a measurement of the power at various frequencies, is
Pyy = Y.* conj(Y) / 512;
MEEN 364
Vijay
8/29/2001
15
Graph the first 257 points (the other 255 points are redundant) on a meaningful frequency
axis.
f = 1000*(0:256)/512;
plot(f,Pyy(1:257))
title('Frequency content of y')
xlabel('frequency (Hz)')
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.