Docsity

MATLAB Quick Tutorial: Variables, Operators, Matrices, and Functions, Lab Reports of Signals and Systems

Partial preview of the text

Download MATLAB Quick Tutorial: Variables, Operators, Matrices, and Functions and more Lab Reports Signals and Systems in PDF only on Docsity!

A Quick Tutorial on MATLAB

Gowtham Bellala

MATLAB

 MATLAB is a software package for doing numerical

computation. It was originally designed for solving linear

algebra type problems using matrices. It’s name is derived

from MATrix LABoratory.

 MATLAB has since been expanded and now has built-in

functions for solving problems requiring data analysis, signal

processing, optimization, and several other types of scientific

computations. It also contains functions for 2-D and 3-D

graphics and animation.

MATLAB Special Variables

 pi Value of π

 eps Smallest incremental number

 inf Infinity

 NaN Not a number e.g. 0/

 i and j i = j = square root of -

 realmin The smallest usable positive real number

 realmax The largest usable positive real number

MATLAB Relational operators

 MATLAB supports six relational operators.

Less Than <

Less Than or Equal <=

Greater Than >

Greater Than or Equal >=

Equal To ==

Not Equal To ~= (NOT != like in C)

Matrices and MATLAB

MATLAB Matrices

 MATLAB treats all variables as matrices. For our purposes a

matrix can be thought of as an array, in fact, that is how it is

stored.

 Vectors are special forms of matrices and contain only one

row OR one column.

 Scalars are matrices with only one row AND one column

Generating Matrices

 MATLAB treats row vector and column vector very differently

 A matrix can be created in MATLAB as follows (note the

commas and semicolons)

>> X = [1 , 2 , 3 ; 4 , 5 , 6 ; 7 , 8 , 9]

X =

Matrices must be rectangular!

The Matrix in MATLAB

A(2,4)

A(17)

Note: Unlike C, MATLAB’s indices start from 1

Extracting a Sub-matrix

 Example :

X = [1 , 2 , 3 ; 4 , 5 , 6 ; 7 , 8 , 9] X = 1 2 3 4 5 6 7 8 9

X22 = X(1:2 , 2:3)

X22 = 2 3 5 6

>> X13 = X(3,1:3)

X13 =

>> X21 = X(1:2,1)

X21 =

Matrix Extension

 >> a = [1,2i,0.56] a = 1 0+2i 0.

a(2,4) = 0. a = 1 0+2i 0.56 0 0 0 0 0.

 repmat – replicates and tiles a matrix

b = [1,2;3,4] b = 1 2 3 4 b_rep = repmat(b,1,2) b_rep = 1 2 1 2 3 4 3 4

 Concatenation

a = [1,2;3,4] a = 1 2 3 4 a_cat =[a,2a;3a,2*a] a_cat = 1 2 2 4 3 4 6 8 3 6 2 4 9 12 6 8

NOTE: The resulting matrix must be rectangular

Matrix Multiplication

 Matrix multiplication

a = [1,2;3,4]; (2x2) b = [1,1]; (1x2) c = b*a c = 4 6

c = a*b ??? Error using ==> mtimes Inner matrix dimensions

must agree.

 Element wise multiplication

a = [1,2;3,4]; b = [1,½;1/3,¼]; c = a.*b c = 1 1

1 1

Matrix Element wise operations

 >> a = [1,2;1,3];

b = [2,2;2,1];

 Element wise division

c = a./b c = 0.5 1 0.5 3

 Element wise multiplication

c = a.*b c = 2 4 2 3

 Element wise power operation

c = a.^ c = 1 4 1 9

c = a.^b c = 1 4 1 3

MATLAB inbuilt math functions

Elementary Math functions

 abs - finds absolute value of all elements in the matrix

 sign - signum function

 sin,cos,… - Trignometric functions

 asin,acos… - Inverse trignometric functions

 exp - Exponential

 log,log10 - natural logarithm, logarithm (base 10)

 ceil,floor - round towards +infinity, -infinity respectively

 round - round towards nearest integer

 real,imag - real and imaginary part of a complex matrix

 sort - sort elements in ascending order