Introductio_to_MATLAB_
Introductio_to_MATLAB_
LECTURE 1
Outline of the Course
• Matlab software installation, environment, getting
started with Matlab, Matlab commands, syntax
and windows.
• MATLAB Operators, rational and logical operators.
• Finding solution of various mathematical
equations, statistics and functions using Matlab.
• MATLAB script files, graphing and plotting of
various engineering quantities.
• Creating simulation blocks using Simulink
• Developing computer program by using Matlab
programing language.
Mode of Teaching:
• Lectures
• Tutorials
• Practicals
Books
• Sigmon, K. and Davis, T. (2002). MATLAB Primer.
Sixth Edition, CHAPMAN & HALL/CRC, USA.
• Knight, A. (2000). Basics of Matlab and Beyond.
CHAPMAN & HALL/CRC, Boca Raton, London, UK.
• Griffiths, D. (1999). An Introduction to Matlab.
Version 2.2 Department of Mathematics, the
University Dundee, Stockholm, Sweden.
• Gander, W. (2015). Learning MATLAB: A Problem
Solving Approach. 1st Edition, Springer
International Publishing.
• Xue, D. and Chen, Y. Q. (2009). Solving Applied
Mathematical Problems With Matlab. Chapman &
Hall/CRC, London, UK.
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.
MATLAB's Power of Computational
Mathematics
• MATLAB is very flexible and usable
for solving problems in various
fields including:
– Applied Mathematics
– Physics
– Chemistry
– Engineering
– Medicine
– Finance
– Complicated numerical calculations
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.
MATLAB Toolboxes
• Toolboxes include:
– Control System Toolbox
– Communication Toolbox
– Filter Design Toolbox
– Financial Toolbox
– Fuzzy Logic Toolbox
– Image Processing Toolbox
– Simulink
– LMI Control Toolbox
– Statistics Toolbox
MATLAB Toolboxes …
– Neural Network Toolbox
– Signal Processing Toolbox
– Wavelet Toolbox
– Optimization Toolbox
– Robust Control Toolbox
– Mapping Toolbox
– Etc
MATLAB - Environment Setup
• Local Environment Setup
• Setting up MATLAB environment is a matter of
few clicks.
• MathWorks provides the licensed product, a
trial version and a student version as well.
• You need to log into the site and wait a little
for their approval.
• After downloading the installer the software
can be installed through few clicks.
Local Environment Setup
Local Environment Setup …
Understanding the MATLAB
Environment
• MATLAB can be launched from the icon
created on the desktop during installation.
• The main working window in MATLAB is called
the desktop.
• When MATLAB is started, the desktop
appears in its default layout.
• Possible windows include: Command window,
current directory, launch pad, command
history and workspace.
Understanding the MATLAB
Environment
The desktop has the following
panels:
• Current Folder − This panel allows you to
access the project folders and files.
• Command Window − This is the main area
where commands are entered at the command
line. It is indicated by the command prompt
(>>).
• Workspace − The workspace shows all the
variables created and/or imported from files.
• Command History − This panel shows or
return commands that are entered at the
command line.
MATLAB - Basic Syntax
• You can enter commands at the >> command
prompt.
• MATLAB provides some special expressions for
some mathematical symbols, like pi for π, Inf
for ∞, i (and j) for √-1 etc.
• Nan stands for 'not a number'.
• MATLAB commands are case sensitive, eg CLC
is not the same as clc.
Use of Semicolon (;) in MATLAB
• Semicolon (;) indicates end of statement.
• However, if you want to suppress and hide the
MATLAB output for an expression, add a
semicolon after the expression.
• For example,
>>x = 3;
>>y = x + 5
y= 8
Colon (:) operator
• Colon can be used to create a vector.
• For example: x=[1:1:10]; creates a row vector with
elements 1,2,3,…10.
– Syntax: vectorname =[firstvalue:increament:lastvalue].
• Increment can be +ve or –ve.
• If increment is omitted then the increment by 1 is
assumed.
• You can create a matrix from vectors of the same
length.
• Example: x=[0:2:20]; y=[x’, sin(x*pi/180)’]
Colon (:) operator
y=
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.
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.
• >> realmax
• ans =
• 1.7977e+308
Numbers …
• >> realmin
• ans =
• 2.2251e-308
Commonly used Operators and
Special Characters
• MATLAB supports the following commonly
used operators and special characters −
Operator Purpose
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
Commonly used Operators and
Special Characters …
Operator Purpose
\ Left-division operator.
/ Right-division operator.
.\ Array left-division operator.
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.
MATLAB Commands …
Command Purpose
%d Format as an integer.
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.
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.
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.
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.
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]
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]
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
Addition and Subtraction of Vectors
• Example (you can also write a script file)
A = [7, 11, 15, 23, 9];
B = [2, 5, 13, 16, 20];
C = A + B;
D = A - B;
disp(C);
disp(D);
Addition and Subtraction of Vectors …
• When you run the file, it displays the following
result −
9 16 28 39 29
5 6 2 7 -11
Scalar Multiplication of Vectors
• Example
• Create a script file with the following code −
v = [ 12 34 10 8];
m=5*v
• When you run the file, it displays the following
result −
m=
60 170 50 40
MATLAB - Transpose of a Vector
• Example
• Create a script file with the following code −
r = [ 1 2 3 4 ]; %row vector
t_r = r';
v = [1;2;3;4]; %column vector
t_v = v';
disp(t_r);
disp(t_v);
MATLAB - Transpose of a Vector …
• When you run the file, it displays the following
result −
1
2
3
4
1 2 3 4
MATLAB - Appending Vectors
• If you have two row vectors r1 and r2 with n and
m number of elements, you can create a row
vector r of n plus m elements, by appending
these vectors:
r = [r1,r2]
• You can also create a matrix r by appending
these two vectors, the vector r2, will be the
second row of the matrix:
r = [r1;r2]
both the vectors should have same number of
elements.
MATLAB - Appending Vectors …
• Similarly, you can append two column vectors c1
and c2 with n and m number of elements.
• To create a column vector c of n plus m elements,
by appending these vectors, you write:
c = [c1; c2]
• You can also create a matrix c by appending
these two vectors; the vector c2 will be the
second column of the matrix:
c = [c1, c2]
• However, to do this, both the vectors should
have same number of elements.
MATLAB - Appending Vectors …
• Example
• Create a script file with the following code −
r1 = [ 1 2 3 4];
r2 = [5 6 7 8];
r = [r1,r2]
rMat = [r1;r2]
c1 = [ 1; 2; 3; 4 ];
c2 = [5; 6; 7; 8 ];
c = [c1; c2]
cMat = [c1,c2]
MATLAB - Appending Vectors …
• When you run the file, it displays the following result −
r=
Columns 1 through 7:
1 2 3 4 5 6 7
Column 8:
8
rMat =
1 2 3 4
5 6 7 8
c=
1
2
3
4
5
6
7
8
cMat =
1 5
2 6
3 7
4 8
MATLAB - Magnitude of a Vector
• Magnitude of a vector v with elements v1, v2, v3, …, vn, is
given by the equation:
|v| = √(v12 + v22 + v32 + … + vn2)
• You need to take the following steps to calculate the
magnitude of a vector −
– Take the product of the vector with itself, using array
multiplication (.*). This produces a vector s_v, whose elements
are squares of the elements of vector v.
s_v = v.*v;
– Use the sum function to get the sum of squares of elements of
vector v. This is also called the dot product of vector v.
d_p= sum(s_v);
– Use the sqrt function to get the square root of the sum which is
also the magnitude of the vector v.
mag = sqrt(d_p);
MATLAB - Magnitude of a Vector …
• Example: Create a script file with the following code −
v = [1: 2: 20];
s_v = v.* v; %the vector with elements
% as square of v's elements
d_p = sum(s_v); % sum of squares -- the dot
product
mag = sqrt(d_p); % magnitude
disp('Magnitude:');
disp(mag);
• When you run the file, it displays the following result −
Magnitude:
36.469
MATLAB - Vector Dot Product
• Dot product of two vectors a and b is calculated using
the dot function.
dot(a, b);
• Example
• Create a script file with the following code:
v1 = [2 3 4];
v2 = [1 2 3];
d_p = dot(v1, v2);
disp('Dot Product:');
disp(d_p);
When you run the file, it displays the following result:
Dot Product:
20
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
Referencing the Elements of a Matrix
(matrix indexing)
• To reference an element in the mth row and nth
column, of a matrix m_x, we write −
m_x(m, n);
• For example, to refer to the element in the 2nd row
and 5th column, of the matrix A, we type −
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
A(2,5)
• MATLAB will execute the above statement and
return the following result:
ans = 6
Referencing the Elements of a Matrix
(matrix indexing) …
• Example 2
Referencing the Elements of a Matrix (matrix
indexing) …
• To reference all the elements in the mth column we type
A(:,m).
• Let us create a column vector A, from the elements of the
4th row of the matrix A
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
V = A(:,4)
• MATLAB will execute the above statement and return the
following result:
V=
4
5
6
7
Referencing the Elements of a Matrix
(matrix indexing) …
• You can also select the elements in the mth through nth columns, for
this we write −
A(:,m:n)
• Let us create a smaller matrix taking the elements from the 2nd and
3rd columns:
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; A(:, 2:3)
• MATLAB will execute the above statement and return the following
result:
ans =
2 3
3 4
4 5
5 6
Deleting a Row or a Column in a Matrix
• You can delete an entire row or column of a matrix
by assigning an empty set of square braces [] to
that row or column.
• Principally, [] denotes an empty array.
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
A( 4 , : ) = []
• MATLAB will execute the above statement and
return:
A=
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
Deleting a Row or a Column in a Matrix …
• To delete the fifth column of A:
A = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(: , 5)=[]
• MATLAB will execute the above statement and
return the following result:
A=
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
Addition & Subtraction of Matrices
• You can add or subtract matrices.
• Both the operand matrices must have the
same number of rows and columns.
• Example
A = [ 1 2 3 ; 4 5 6; 7 8 9];
B = [ 7 5 6 ; 2 0 8; 5 7 1];
C=A+B
D=A-B
Addition & Subtraction of Matrices …
• When you run the file, it displays the following
result −
C=
8 7 9
6 5 14
12 15 10
D=
-6 -3 -3
2 5 -2
2 1 8
Division (Left, Right) of Matrices
• (/) Slash or matrix right division.
• B/A is roughly the same as B*inv(A).
• More precisely, B/A = (A'\B')'.