Matlab Tutorial, CDS110-101: Elisa Franco
Matlab Tutorial, CDS110-101: Elisa Franco
Introduction
Variables
Operations and functions
Graphics
Programming
Simulink
Elisa Franco
29 September 2006
Introduction
Variables
Arrays
Built-in variables
Operations and functions
Operations
Linear algebra
Polynomials
Functions
Graphics
Programming
Scripts and data management
Functions
Simulink
Elisa Franco Matlab Tutorial, CDS110-101
Outline
Introduction
Variables
Operations and functions
Graphics
Programming
Simulink
What is Matlab?
Matlab (stands for ”Matrix laboratory”) is an environment which is
suited to work with arrays and matrices, integrating visualization
and computation. Visit the website http://www.mathworks.com.
Several toolboxes (i.e. sets of precompiled routines) are available,
specifically suited for different engineering fields: statistics, signal
processing, control, data acquisition, symbiology ...
Files created in Matlab end in .m, data can be saved in files that
end in .mat (we will see how).
Command window
4
You can also use the transpose operation: a = [1 ; 2 ; 3 ; 4]0 ;
defines again a row vector.
Type >> t = [0 : .1 : 1] or >> t = 0 : .1 : 1. Defines a row vector
t whose elements range from 0 to 1 and are spaced by .1.
To get rid of a, write >> clear a. (Clear all - clears all vars)
Elisa Franco Matlab Tutorial, CDS110-101
Outline
Introduction
Variables
Arrays
Operations and functions
Built-in variables
Graphics
Programming
Simulink
Note: Matlab has a built in variable ans that stores the most
recent variable you defined.
Elisa Franco Matlab Tutorial, CDS110-101
Outline
Introduction
Variables
Arrays
Operations and functions
Built-in variables
Graphics
Programming
Simulink
Typing: p = [ 1 1 3 ];
defines a row vector p = ( 1 1 3 ), which the environment can
also interpret as a second order polynomial p(s) = s 2 + s + 3.
Typing: p = [ 1 0 3 1 ];
will define a third order polynomial p(s) = s 3 + 3s + 1.
To access the (i, j) element of A, just type A(i, j). For instance
>> A(2, 3) will return >> ans = 3. Elements of a matrix can also
be reassigned, e.g. >> A(2, 3) = 100; will transform A into:
1 2 3
A=
1 2 100
Built-in variables
There are seven built-in variables (which can be overwritten):
I ans, which we have seen already.
I eps is the floating point machine accuracy.
I i and j represent the imaginary unit. You can define complex
variables ( array, polynomial coefficients, matrices), e.g.
>> x = 5 + j ∗ 3 is a complex number.
I Inf is ”infinity”. 1/0, 22000 and exp(1000) all produce Inf ;
log (0) produces −Inf .
I pi is π.
I NaN is ”Not a Number”; this is generated when operations
have undefined numerical results, such as 0/0 or Inf /Inf .
Elisa Franco Matlab Tutorial, CDS110-101
Outline
Introduction
Operations
Variables
Linear algebra
Operations and functions
Polynomials
Graphics
Functions
Programming
Simulink
Elementary operations
Two types of arithmetic operations:
I matrix arithmetic operations: rules of linear algebra
I array arithmetic operations: carried out element by element.
A R B operates elementwise.
To compare expressions:
Linear algebra
Given a square matrix
1 2 3
M= 4 5 6
7 8 9
Linear algebra
...continued
I inverse: >> inv (M)... oops we obviously get a Warning:
Matrix is close to singular or badly scaled. Results may be
inaccurate. RCOND = 2.202823e-18. But if the matrix is
invertible, this won’t happen!
I eigenvalues and eigenvectors: >> [v , e] = eig (M) gives
−0.2320 −0.7858 0.4082 16.1168 0 0
v = −0.5253 −0.0868 −0.8165 , e = 0 −1.1168 0
−0.8187 0.6123 0.4082 0 0 −0.0000
Simple graphics
Surfaces
Example: >> x = [−2 : .1 : 2];
>> y = [−2 : .1 : 2];
>> [X , Y ] = meshgrid(x, y );
>> Z = X .∧ 2 + Y .∧ 2;
>> mesh(X , Y , Z )
>> xlabel(0 X0 )
>> ylabel(0 Y0 )
>> zlabel(0 Z0 )
>> title(0 My first surface0 )
Try also >> surf (X , Y , Z )! For
more info, type >> help mesh or
>> help surf .
Create a script
Go to File → New
M-file.
This will launch the
Matlab editor.
Control flow
I for:
I if–else:
for var = expr
if expr1 T
stat
stat
end
else if expr2 T
stat
I while: else
stat
while expr T
end
stat
end
end
Example
Data management
Functions
If you need to repeat similar operations,
consider defining a function. Example:
discretized equations for an oscillator.
Important: you need to save the file with
the function name!
What is Simulink?
Simulink is a software
package for modeling,
simulating, and
analyzing dynamic
systems. It supports
linear and nonlinear
systems, in continuous
or discrete time, or a
hybrid of the two. Type
>> simulink in the
comm. win. or click on
icon.
Create an example
Run a simulation
To run a simulation,
need to set the
simulation parameters.
Duration, ODE solver,
step size: depend on the
problem.
Scopes are needed to
plot the variables of
interest.
Conclusions