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

A quick tutorial on using matlab, a software package for numerical computation. Topics covered include matlab's origins, variable names and special variables, relational and logical operators, matrices and matrix manipulation functions, and matlab's built-in math functions. This tutorial is suitable for both beginners and those looking to refresh their knowledge.

Typology: Lab Reports

Pre 2010

Uploaded on 09/02/2009

koofers-user-bpc
koofers-user-bpc 🇺🇸

10 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A Quick Tutorial on MATLAB
Gowtham Bellala
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

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