MEU 07254 Technical Computing With Matlab
MEU 07254 Technical Computing With Matlab
Overview
• Engineering/Scientific calculations are: Involving,
Repetitive and Complex.
– So we need a powerful tool to deal with it.
• Tools include:
– Mathcad: (https://www.mathcad.com/)
– Maple: (https://www.maplesoft.com/)
– Mathematica: (https://www.wolfram.com/)
– MATLAB: (https://www.mathworks.com/)
– Etc
• Matlab performs complex calculations (advanced
calculator), has a programming environment,
graphical data plotting and other useful features.
• MATLAB is developed by MathWorks and stands for
MATrix LABoratory.
1
2022-05-12
Features of MATLAB …
• Matlab features a family of applications
specific calculations called Toolboxes.
• Toolboxes are comprehensive collection of
MATLAB functions that extend MATLAB
environment to solve particular problems.
• MATLAB software comes in versions.
Particularly there are version 5 (R12), 6 (R13),
7 (R14), R2006a, R2006b, etc.
2
2022-05-12
MATLAB Toolboxes
• Toolboxes include:
• Control System Toolbox
• Communication Toolbox
• Filter Design Toolbox
• Financial Toolbox
• Fuzzy Logic Toolbox
• Image Processing Toolbox
• Simulink
• LMI Control Toolbox
• Statics Toolbox
MATLAB Toolboxes …
• Neural Network Toolbox
• Signal Processing Toolbox
• Wavelet Toolbox
• Optimization Toolbox
• Robust Control Toolbox
• Mapping Toolbox
• Etc
3
2022-05-12
4
2022-05-12
5
2022-05-12
6
2022-05-12
7
2022-05-12
8
2022-05-12
9
2022-05-12
0 0
2.0000 0.9971
4.0000 0.1525
6.0000 -0.9737
8.0000 -0.3015
10.0000 0.9276
12.0000 0.4434
14.0000 -0.8598
16.0000 -0.5750
18.0000 0.7718
20.0000 0.6931
Adding Comments
• The percent symbol (%) is used to indicate a
comment line. For example,
• x=9 % assign the value 9 to x
• ver: displays version of MATLAB installed
other toolboxes and their versions.
• version: displays version of MATLAB only.
• help: to get help topics. It gives list of help
topics
• helpwin: gives details of each help topic.
10
2022-05-12
Numbers
• Can be integer or real numbers.
• Power of 10 is represented by using e or E (eg.
1.45x103 use 1.45e3 or 1.45E3). For negative
power use negative sign (eg. 1.45e-3).
• Numbers are represented to approximately
16 significant figures.
• Use realmax and realmin to determine the
max and min numbers.
11
2022-05-12
\ Left-division operator.
/ Right-division operator.
12
2022-05-12
13
2022-05-12
Naming Variables
• Variable names consist of a letter followed by
any number of letters, digits or underscore.
• MATLAB is case-sensitive.
• Variable names can be of any length, however,
MATLAB uses only first N characters, where N
is given by the function namelengthmax.
14
2022-05-12
MATLAB - Variables
• In MATLAB environment, every variable is an array or
matrix.
• You can assign variables in a simple way. For example,
x = 3 % defining x and initializing it with a value
• MATLAB will execute the above statement and return
x=3
• It creates a 1-by-1 matrix named x and stores the value
3 in its element.
x = sqrt(16) % defining x and initializing it with
an expression
15
2022-05-12
Multiple Assignments
• You can have multiple assignments on the same
line. For example,
a = 2; b = 7; c = a * b
c = 14
• The clear command deletes all (or the specified)
variable(s) from the memory.
clear x % it will delete x, won't display
anything
clear % it will delete all variables in the
workspace
MATLAB Commands
• Now we will provide lists of commonly used
general MATLAB commands.
Command Purpose
clc Clears command window.
clear Removes variables from memory.
exist Checks for existence of file or variable.
global Declares variables to be global.
help Searches for a help topic.
16
2022-05-12
MATLAB Commands …
Command Purpose
17
2022-05-12
%d Format as an integer.
18
2022-05-12
19
2022-05-12
20
2022-05-12
Plotting Commands
• MATLAB provides several commands for
plotting graphs.
• The following table shows some of the
commonly used commands for plotting −
Command Purpose
axis Sets axis limits.
fplot Intelligent plotting of functions.
grid Displays gridlines.
21
2022-05-12
Plotting Commands …
Command Purpose
plot Generates xy plot.
print Prints plot or saves plot to a
file.
title Puts text at top of plot.
xlabel Adds text label to x-axis.
ylabel Adds text label to y-axis.
axes Creates axes objects.
close Closes the current plot.
Plotting Commands …
Command Purpose
close all Closes all plots.
figure Opens a new figure window.
gtext Enables label placement by
mouse.
22
2022-05-12
Plotting Commands …
Command Purpose
subplot Creates plots in subwindows.
text Places string in figure.
bar Creates bar chart.
loglog Creates log-log plot.
polar Creates polar plot.
MATLAB - Operators
• An operator is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations.
• Operators in MATLAB work both on scalar and
non-scalar data.
• MATLAB allows the following types of elementary
operations −
– Arithmetic Operators
– Relational Operators
– Logical Operators
– Bitwise Operations
– Set Operations
23
2022-05-12
Arithmetic Operators
• MATLAB allows two different types of
arithmetic operations −
– Matrix arithmetic operations
– Array arithmetic operations
• Matrix arithmetic operations are same as
defined in linear algebra.
• Array operations are executed element by
element, both on one-dimensional and
multidimensional array.
Arithmetic Operators …
Sr. No. Operator & Description
1 +
Addition or unary plus.
A+B adds the values stored in variables A and B.
A and B must have the same size, unless one is a scalar.
A scalar can be added to a matrix of any size.
2 -
Subtraction or unary minus.
A-B subtracts the value of B from A.
A and B must have the same size, unless one is a scalar.
A scalar can be subtracted from a matrix of any size.
24
2022-05-12
Arithmetic Operators …
Sr. Operator & Description
No.
3 *
Matrix multiplication.
C = A*B is the linear algebraic product of the matrices A
and B. For non-scalar A and B, the number of columns of
A must be equal to the number of rows of B.
A scalar can multiply a matrix of any size.
4 .*
Array multiplication.
A.*B is the element-by-element product of the arrays A
and B. A and B must have the same size, unless one of
them is a scalar.
Arithmetic Operators …
Sr. No. Operator & Description
5 /
Slash or matrix right division.
B/A is roughly the same as B*inv(A).
More precisely, B/A = (A'\B')'.
6 ./
Array right division.
A./B is the matrix with elements A(i,j)/B(i,j). A
and B must have the same size, unless one of
them is a scalar.
25
2022-05-12
Arithmetic Operators …
Sr. No. Operator & Description
7 \
Backslash or matrix left division.
If A is a square matrix, A\B is roughly the same as inv(A)*B, except
it is computed in a different way.
If A is an n-by-n matrix and B is a column vector with n
components, or a matrix with several such columns, then X = A\B
is the solution to the equation AX = B. A warning message is
displayed if A is badly scaled or nearly singular.
8 .\
Array left division.
A.\B is the matrix with elements B(i,j)/A(i,j).
A and B must have the same size, unless one of them is a scalar.
Arithmetic Operators …
Sr. No. Operator & Description
9 ^
Matrix power.
X^p is X to the power p, if p is a scalar.
If p is an integer, the power is computed by repeated squaring.
If the integer is negative, X is inverted first.
10 .^
Array power.
A.^B is the matrix with elements A(i,j) to the B(i,j) power.
A and B must have the same size, unless one of them is a scalar.
26
2022-05-12
Arithmetic Operators …
Sr. No. Operator & Description
11 '
Matrix transpose.
A' is the linear algebraic transpose of A.
For complex matrices, this is the complex conjugate
transpose.
12 .'
Array transpose.
A.' is the array transpose of A.
For complex matrices, this does not involve
conjugation.
Complex Numbers
• sqrt(-1) = i or j.
• Use x = 2+2i or x = 2+2*i or x=2+2*sqrt(-1)
• x = 2+sin(0.5)*j
• Termination with i and j works with numbers and
not expression.
• For expression use *i or *j such as x = 2+sin(0.5)*j
• To find magnitude and angle (in radian) use abs
and angle functions.
• To extract real and imaginary part use real and
imag functions.
• To find conjugate of x use conj function.
27
2022-05-12
Complex Numbers …
• Example:
– Z=3+4j;
– Find magz = abs(z),
– anglez_radians = angle(z),
– anglez_degrees=angle(z)*180/pi,
– real_partz=real(z)
– imaginary_partz=imag(z).
• Results:
– z = 3.0000 + 4.0000i
– magz = 5
– anglez_radians = 0.9273
– anglez_degrees = 53.1301
– real_partz = 3
– imaginary_partz = 4
Creating Vectors
• A vector is a one-dimensional array of
numbers.
• MATLAB allows creating two types of vectors:
– Row vectors
– Column vectors
• Row vectors are created by enclosing the set
of elements in square brackets, using space or
comma to delimit the elements.
28
2022-05-12
Creating Vectors …
• For example,
r = [7 8 9 10 11]
• MATLAB will execute the above statement and
return the following result:
r
= 7 8 9 10 11
• Another example,
r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
Creating Vectors …
• MATLAB will execute the above statement and
return the following result:-
res =
9 11 13 15 17
• Column vectors are created by enclosing the
set of elements in square brackets, using
semicolon(;) to delimit the elements.
c = [7; 8; 9; 10; 11]
29
2022-05-12
Creating Vectors …
• MATLAB will execute the above statement and
return the following result: −
c=
7
8
9
10
11
Creating Matrices
• A matrix is a two-dimensional array of
numbers.
• In MATLAB, a matrix is created by entering
each row as a sequence of space or comma
separated elements, and end of a row is
demarcated by a semicolon.
• For example, let us create a 3-by-3 matrix as −
m = [1 2 3; 4 5 6; 7 8 9]
30
2022-05-12
Creating Matrices …
• MATLAB will execute the above statement and
return the following result: −
m=
1 2 3
4 5 6
7 8 9
Vector Operations
• To carryout vector operations:−
– Addition and Subtraction of Vectors
– Scalar Multiplication of Vectors
– Transpose of a Vector
– Appending Vectors
– Magnitude of a Vector
– Vector Dot Product
– Vectors with Uniformly Spaced Elements
31
2022-05-12
32
2022-05-12
33
2022-05-12
1 2 3 4
34
2022-05-12
35
2022-05-12
36
2022-05-12
37
2022-05-12
MATLAB - Matrix
• A matrix is a two-dimensional array of
numbers.
• In MATLAB, you create a matrix by entering
elements in each row as comma or space
delimited numbers and using semicolons to
mark the end of each row.
• For example, let us create a 4-by-5 matrix a −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
MATLAB - Matrix
• MATLAB will execute the above statement and
return the following result −
a=
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
38
2022-05-12
39
2022-05-12
40
2022-05-12
41
2022-05-12
42
2022-05-12
43
2022-05-12
44
2022-05-12
45
2022-05-12
46
2022-05-12
Relational operators
• < less than
• <= less than or equal
• > greater than
• >= greater than or equal
• == equal to
• ῀= not equal
Logical operations
• & AND
• | OR
47
2022-05-12
Find function
• find function finds indices and values of
elements which satisfies the logical condition.
• Syntax:
• k=find(x); returns the indices of array x that
points to nonzero elements.
• It is equivalent to k=find(x ῀=)
• Example:
– x=[1 0 4 -3 0 0 0 8 6]
Find function …
• indices=find(x);
• Returns indices for the nonzero entries of x:
• Indices=1 3 4 8 9
• To get elements use: x(indices) it will return 1 4 -3
8 6.
• [r, c]=find(x); returns the row and column indices
of nonzero entries in the matrix x.
• Example:
– z=[8 0 6; 3 5 7; 4 9 0]
48
2022-05-12
Find function …
• [r, c]=find(z)
• [r, c] %this shows actual row and column
combination.
49