You are on page 1of 30

MATLAB

S3 ECE
MATrix LABoratory
● Software package for high-performance numerical computation and
visualization

● Basic building block of MATLAB is the matrix

● Originally designed for solving linear algebra type problems using


matrices

● MATLAB has since been expanded and now has built-in functions for
solving several types of scientific computations.
MATLAB environment

● The Command Window

● The Command History

● The Workspace

● The Current Directory


Points to Note

● Data type : Array

● Dimensioning: automatic

● Case sensitivity: MATLAB is case-sensitive ; ( a


and A are different variables )

● Output display: A semicolon at the end of a


command suppresses the screen output
Getting Started
VARIABLES
● Variable names are case sensitive

● Variable names must start with a letter and can be


followed by letters, digits and underscores.

x = 2;
abc_123 = 0.005;
2ab = 5;

Error ???
SPECIAL VARIABLES

● pi Value of π
● eps Smallest incremental number
● inf Infinity
● NaN Not a number e.g. 0/0
● i and j i = j = square root of -1
● realmin The smallest usable positive real number
● realmax The largest usable positive real number
OPERATORS
not ~
Less Than <
and &
Less Than or Equal <=
or |
Greater Than >
Add +
Greater Than or Equal >=
Subtract -
Equal To ==
Multiply *
Not Equal To ~=
Divide /
Lesson 1
Calculator
»2+2

»x=2+2

» y = 2^2 + log (pi ) * sin (x) ;

» format long
EXERCISE
● Arithmetic operations :
○ 25 / (25 - 1 ) ○ 1 .0323
○ (1 - (1/25)) - 1 ○ 1 .0323
○ [ 3 ( √5 - 1) / (√5 + 1 ) 2 ] - 1 ○ -0.6459
○ Area = 𝛑r2 with r = 𝛑(⅓) - 1. ○ 0.6781

● Exponential and logarithms :


○ e3 ○ 20.0855
○ ln(e3) ○ 3
○ log10 (e3) ○ 1.3029
○ log10 (105) ○ 5
○ Solve for x from 3x = 17 ○ 2.5789
● Trigonometry :
○ 0.5000, -1 , 1.6331e+16
○ sin(π/6) , cos π , tan (π/2)
○ sin2(π/6) + cos2 (π/6) ○ 1
○ sinh2(x) - cosh2 (x) with x = 32π ○ 0

● Complex numbers :

○ (1 + 3i )/ (1 - 3i )
○ e(𝞹i / 4 ) ○ -0.8000 + 0.6000i
○ exp (pi/2*i)exp (pi/2i) ○ 0.7071 + 0.7071i
○ 0.0000 + l .OOOOi
○ 0.0000 - l .OOOOi
Elementary Math functions

● abs - finds absolute value of all elements


● sign - signum function
● sin,cos,… - Trigonometric functions
● asin,acos… - Inverse trigonometric functions
● exp - Exponential
● log,log10 - natural logarithm, logarithm (base 10)
● round - round towards nearest integer
● real,imag - real and imaginary part of a complex matrix
Lesson 2
Matrix
● MATLAB treats all variables as matrices.

● 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 a scalar w = 23
>> w = 23;

Generating a row vector y = 12 10 -3


>> y = [ 12 , 10 ,-3]

Generating a column vector z =


>> z = [ 12 ; 10 ; -3] 12
10
-3
Generating a scalar x =
>> x = [1, 2 ,3 ; 4, 5, 6 ; 7, 8, 9] 1 2 3
4 5 6
7 8 9
Extracting a Sub-matrix

A portion of a matrix can be extracted


sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;

r1 and r2 specify the beginning and ending rows and c1 and c2 specify
the beginning and ending columns to be extracted
EXAMPLE
>> X = [1, 2, 3 ; 4, 5, 6 ; 7, 8, 9] >> X13 = X(3,1:3)

X= 1 2 3 X13 = 7 8 9
4 5 6
7 8 9

>> c X22 = 2 3 >> X21 = X(1:2,1)


5 64 X21 = wnryfg,

1
4
Matrix Extension
>> a = [1, 2i, 0.56]
>> a = [1,2;3,4]
a = 1 0+2i 0.56
a= 1 2
3 4
>> a(2,4) = 0.1
a = 1 0+2i 0.56 0
>> a_cat =[a, 2*a ; 3*a, 2*a]
0 0 0 0.1
a_cat =
1 2 2 4
>> b = [1,2;3,4] 3 4 6 d8
b= 1 2 3 6 2 4
3 4 9 12 6 8
>> b_rep = repmat(b,1,2)
b_rep = 1 2 1 2
3 4 3 4
Matrix Addition

Increment all the elements of a Adding two matrices


matrix by a single value >> xsy = x + y
Xsy = 7 9
>> x = [1,2;3,4] 11 13
x= 1 2
3 4 >> z = [1, 0.3]
z = 1 0.3
>> y = x + 5 >> xsz = x + z ??

y= 6 7
8 9 xsz shows error due to
dimension mismatch of x and z
Matrix Multiplication

>> a = [1,2;3,4]; (2x2)


>> b = [1,1]; (1x2)

>> c = b*a

c= 4 6

>> c = a*b

Error ??
Matrix Element wise operations

>> a = [1,2; 1,3]; Element wise multiplication


>> b = [2,2; 2,1]; >> c = a .* b
c= 2 4
2 3
Element wise division
>> c = a . / b Element wise power operation
c = 0.5 1 >> c = a .^ 2
0.5 3 c= 1 4
1 9
>> c = a .^ b
c= 1 4
1 3
Matrix Manipulation functions

● zeros : creates an array of all zeros, Ex: x = zeros(3,2)


● ones : creates an array of all ones, Ex: x = ones(2)
● size : returns array dimensions
● length : returns length of a vector (row or column)
● det : Matrix determinant
● inv : matrix inverse
Lesson 3
Graphics Fundamentals
2D Plotting

● plot(X,Y) creates a 2-D line plot


● xlabel(txt) annotates the x-axis,
● ylabel(txt) annotates the y-axis,
● title(txt) puts a title on the plot, and
● linspace(a,b,n) generates a row vector y of n points
linearly spaced between and including a and b
EXERCISE

Plot sin(x) and cos(x) over [0,2π], on the same plot with different colours
Plot the following function
y = t, 0 <= t <=1
= 1/t , 1 <= t <=6
Subplots
Syntax: subplot (rows, columns, index)

You might also like