You are on page 1of 35

Introduction to Matlab

Matlab Basics

John J. H. Oh
Pohang Univ. Of Science and Technology

Based on lectures by F. Gustafsson, Linköping University


Introduction to Matlab 2

What is Matlab?
A software environment for interactive numerical computations

Examples:
 Matrix computations and linear algebra
 Solving nonlinear equations
 Numerical solution of differential equations
 Mathematical optimization
 Statistics and data analysis
 Signal processing
 Modelling of dynamical systems
 Solving partial differential equations
 Simulation of engineering systems
Introduction to Matlab 3

Matlab Background

Matlab = Matrix Laboratory


Originally a user interface for numerical linear algebra
routines (Lapak/Linpak)
Commercialized 1984 by The Mathworks
Since then heavily extended (defacto-standard)

Alternatives Complements
Matrix-X Maple (symbolic)
Octave (free; GNU) Mathematica (symbolic)
Lyme (free; Palm)
Introduction to Matlab 4

Construction
Core functionality: compiled C-routines
Most functionality is given as m-files, grouped into toolboxes
 m-files contain source code, can be copied and altered
 m-files are platform independent (PC, Unix/Linux, MAC)
Simulation of dynamical systems is performed in Simulink

Sig. Proc Contr. Syst.

Simulink
C-kernel m-files
Introduction to Matlab 5

Matlab Desktop

Launch Pad

Command
Window

History
Introduction to Matlab 6

Matlab Desktop – cont’d

Workspace

Command
Window

Current
DIrectory
Introduction to Matlab 7

The Matlab Environment

Matlab is an interpreted language


 Commands are typed into the COMMAND Window and
executed immediately
 Variables are allocated in memory as soon as they are first
used in an expression
 Commands must be re-entered to be re-executed
All variables created in the Command Window are in what
is called the Base Workspace
 Variables can be reassigned new values as needed
 Variables can be selectively cleared from the workspace
The Workspace can be saved to a data file
 File extension is .mat (ex: mydata.mat)
 File is in binary and essentially unreadable by humans
 .mat files can be reloaded back into the Matlab Workspace
Introduction to Matlab 8

Matlab Help
Introduction to Matlab 9

The Help System

Search for appropriate function


>> lookfor keyword

Rapid help with syntax and function definition


>> help function

An advanced hyperlinked help system is launched by


>> helpdesk

Complete manuals as PDF files


Introduction to Matlab 10

Matlab Editor Access to


commands

Color keyed
text with auto
indents

tabbed sheets for other


files being edited
Introduction to Matlab 11

Interactive Calculations
Matlab is interactive, no need to declare variables
>> 2+3*4/2
>> a=5e-3; b=1; a+b

Most elementary functions and constants are already defined


>> cos(pi)
>> abs(1+i)
>> sin(pi)

Last call gives answer 1.2246e-016 !?


Introduction to Matlab 12

Floating point numbers in Matlab


IEEE Standard for double precision numbers

s e f
1 2 12 13 64

Round-off: eps = 2-52


Underflow: realmin = 2-1022
Overflow: realmax = (2-eps) ·21023
Introduction to Matlab 13

Variable and Memory Management


Matlab uses double precision (approx. 16 significant digits)
>> format long
>> format compact

All variables are shown with


>> who
>> whos

Variables can be stored on file


>> save filename
>> clear
>> load filename
Introduction to Matlab 14

Vectors and Matrices


Vectors (arrays) are defined as
>> v = [1, 2, 4, 5]
>> w = [1; 2; 4; 5]

Matrices (2D arrays) defined similarly


>> A = [1,2,3;4,-5,6;5,-6,7]
Introduction to Matlab 15

Matrix Operators
All common operators are overloaded
>> v + 2

Common operators are available


>> B = A’
>> A*B
>> A+B

Note:
 Matlab is case-sensitive
A and a are two different variables
• Transponate conjugates complex entries; avoided by
>> B=A.’
Introduction to Matlab 16

Indexing Matrices
Indexing using parentheses
>> A(2,3)

Index submatrices using vectors


of row and column indices
>> A([2 3],[1 2])

Ordering of indices is important!


>> B=A([3 2],[2 1])
>> B=[A(3,2),A(3,1);A(2,2);A(2,1)]
Introduction to Matlab 17

Indexing Matrices
Index complete row or column using
the colon operator
>> A(1,:)

Can also add limit index range


>> A(1:2,:)
>> A([1 2],:)

General notation for colon operator


>> v=1:5
>> w=1:2:5
Introduction to Matlab 18

Matrix Functions
Many elementary matrices predefined
>> help elmat;
>> I=eye(3)

Elementary functions are often overloaded


>> help elmat
>> sin(A)

Specialized matrix functions and operators


>> As=sqrtm(A)
>> As^2
>> A.*A

Note: in general, ”.<operator>” is elementwise operation


Introduction to Matlab 19

Numerical Linear Algebra


Basic numerical linear algebra
>> z=[1;2;3]; x=inv(A)*z
>> x=A\z

Many standard functions predefined


>> det(A)
>> rank(A)
>> eig(A)

The number of input/output arguments can often be varied


>> [V,D]=eig(A)
Introduction to Matlab 20

Inner (dot) Product

v α
w v.w = ( x1 , x2 ).( y1 , y2 ) = x1 y1 + x2 . y2

The inner product is a SCALAR!

v.w = ( x1 , x2 ).( y1 , y2 ) =|| v || ⋅ || w || cos α

v.w = 0 ⇔ v ⊥ w
Introduction to Matlab 21

Matrices

Sum:
Cn×m = An×m + Bn×m
 a11 a12  a1m 
a
 21 a22  a2 m  cij = aij + bij
An×m = a31 a32  a3m 
  A and B must have the same
    
dimensions
an1 an 2  anm 
Introduction to Matlab 22

Matrices

Product: A and B must have


Cn× p = An×m Bm× p compatible dimensions
m
cij = ∑ aik bkj
k =1 An×n Bn×n ≠ Bn×n An×n

1 0  0
Identity Matrix:
 
0 1  0
I = IA = AI = A
   
 
0 0  1
Introduction to Matlab 23

Matrices
Transpose:
Cm×n = A T
n× m ( A + B) = A + B
T T T

cij = a ji ( AB)T = B T AT

If A =A
T
A is symmetric
Introduction to Matlab 24
Matrices

Determinant: A must be square

 a11 a12  a11 a12


det   = = a11a22 − a21a12
a21 a22  a21 a22

 a11 a12 a13 


a22 a23 a21 a23 a21 a22
det a21 a22 
a23  = a11 − a12 + a13
a32 a33 a31 a33 a31 a32
a31 a32 a33 
Introduction to Matlab 25

Matrices

Inverse: A must be square


−1 −1
An×n A n× n =A n×n An×n = I
−1
 a11 a12  1  a22 − a12 
a  = − a
 21 a22  a11a22 − a21a12  21 a11 
Introduction to Matlab 26

2D Translation
P’

t
P
Introduction to Matlab 27

2D Translation Equation

P’
t
ty P P = ( x, y )
y t = (t x , t y )
x tx

P ' = ( x + t x , y + t y ) = P+t
Introduction to Matlab 28

2D Translation using Matrices

P’ P = ( x, y )
t
ty P
t = (t x , t y )
y
t P
x tx  x
 x + t x  1 0 t x   
P' →   = ⋅  y
 y + t y  0 1 t y   1 
 
Introduction to Matlab 29

Scaling
P’

P
Introduction to Matlab 30

Scaling Equation

P’
s.y
P = ( x, y )
P P ' = ( sx, sy )
y
P' = s ⋅ P
x s.x
 sx   s 0  x 
P' →   =  ⋅ 
 sy  0 s   y 

P' = S ⋅ P S
Introduction to Matlab 31

Rotation

P’
Introduction to Matlab 32

Rotation Equations

Counter-clockwise rotation by an angle θ

 x' cosθ − sin θ   x 


P’  y ' =  sin θ cosθ   y 
Y’   
θ
P
y P' = R.P
x
X’
Introduction to Matlab 33

Graphics

Visualization of vector data is available


>> x=-pi:0.1:pi; y=sin(x);
>> plot(x,y)
>> plot(x,y,’s-’)
>> xlabel(’x’); ylabel(’y=sin(x)’);

Can change plot properties in Figure menu, or via ”handle”


>> h=plot(x,y); set(h, ’LineWidth’, 4);

Many other plot functions available


>> v=1:4; pie(v)
Introduction to Matlab 34

Graphics
Three-dimensional graphics
>> A = zeros(32);
>> A(14:16,14:16) = ones(3);
>> F=abs(fft2(A));
>> mesh(F)
>> rotate3d on

Several other plot functions available


>> surfl(F)

Can change lightning and material properties


>> cameramenu
>> material metal
Introduction to Matlab 35

Graphics
Bitmap images can also be visualized
>> load mandrill
>> image(X); colormap(map)
>> axis image off

You might also like