Matlab Lecture 1
Matlab Lecture 1
Lecture 1
Introduction to MATLAB
1
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Introduction
• A software package for high Integer
A
performance numerical computation
Double
and visualization. R
• Matrix Laboratory
R String
• Interactive Environment
•
A Cell
Fundamental data type is array
Y Matrices
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Matlab Interface
• Command Window
• Current Directory Pane
• Workspace Pane
• Figure Window
• Editor Window
• Command History Pane
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Command prompt
Current
directory
pane
Command window
Workspace
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Defining Variables
• Variable_name = A numerical value, or a computable expression
Keywords
• There are 20 words, called keywords, that are reserved by MATLAB for various
purposes and cannot be used as variable names. These words are:
Command Description
Command Description
Using Help
• MATLAB provides on-line help for all its built-in functions and programming language
constructs.
• To access help, type help, give space and after that type the name of the function at the
command prompt. It will open the documentation on that function.
Using Format
• The format command helps us set the displaying format for the command
window.
• Check out command “help format”
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Function Description
sqrt(x) Square root.
nthroot(x,n) Real nth root of a real number x, (if x is negative n must be an odd integer)
exp(x) Exponential ()
abs(x) Absolute Value
log(x) Natural logarithm (Base e logarithm)
log10(x) Base 10 logarithm
factorial(x) The factorial function x!. (x > 0)
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Trigonometric Functions
Function Description
sin(x) Sine of angle x (x in radians).
sind(x) Sine of angle x (x in degrees).
cos(x) Cosine of angle x (x in radians).
cosd(x) Cosine of angle x (x in degrees).
tan(x) Tangent of angle x (x in radians).
tand(x) Tangent of angle x (x in degrees).
cot(x) Cotangent of angle x (x in radians).
cotd(x) Cotangent of angle x (x in degrees).
NOTE : The inverse of trigonometric functions are asin(x), acos(x), atan(x), acot(x) for
angles in radians & asind(x), acosd(x), atan(x), acotd(x) for angles in degrees.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Rounding Figures
Function Description
ARRAYS
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Creating Arrays
• Array Addressing
• Deleting elements
• The column vector can be converted into row vector with the help of transpose symbol
(‘).
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
• Creating a vector with linear (equal) spacing by specifying the first and last terms, and the
number of terms.
Variable_name = linspace(1st term, last term, no. of elements)
Example: v = linspace(0,8,6)
v = 0 1.6000 3.2000 4.8000 6.4000 8.0000
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
A=
• To enter the given matrix in the command window, enter the following expression on the
command prompt.
A = [6 1 1; 4 -2 5; 2 8 7]
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Practice Problems
1. Create the following row vector: [3 6 34 7 3]
2. Create a column vector that has the following elements: 52.2, , 4!, , 0.406,
and .
3. Define variables x = 0.85 and y = 12.5, and then use them to create column
vector that has the following elements: y, , ln(y/x), y*x, and x+y.
4. Create the following matrix by using vector notation for creating vectors with
constant spacing and linspace command. Do not type individual element
explicitly.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
• For a Matrix
A(:,n) = Refer to all the elements in all the rows of column n of matrix A.
A(n,:) = Refer to all the elements in all the column of row n of matrix A.
A(:,m:n) = Refer to all the elements in all the rows between column m and n.
A(m:n,:) = Refer to all the elements in all the columns between row m and n.
A(m:n,p:q) = Refer to the elements in rows m through n and columns p through q.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
E = [1 2 3 ; 5 6 7] AW = [3 6 9; 8 5 11]
E= AW =
AW(4,5) = 17
E(3,:) = [10:4:22]
AW =
E=
Deleting Elements
• To delete element/elements from a matrix/vector, address the elements to
be deleted and define its value as an empty matrix.
Kt = [2 8 40 65 3 55 23 13 75 80] Mt = [5 78 4 24 9; 4 0 36 60 12; 56 13 5
89 3]
Kt = 2 8 40 65 3 55 23 13 75 80
Mt =
Kt(6) = [ ]
Kt = 2 8 40 65 3 23 13 75 80 Mt(:,2:4) = [ ]
Mt =
Kt(3:6) = [ ]
Kt = 2 8 13 75 80
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Generating Matrices
• zeros(m,n): Create a matrix with m rows and n columns in which all the elements
are 0.
• ones(m,n): Create a matrix with m rows and n columns in which all the elements
are 1.
• eye(n): Creates a square matrix with n rows and n columns in which the diagonal
elements are equal to 1 and remaining elements are 0.
• rand(n): Creates a nXn matrix of random real numbers between 0 and 1
• rand(m,n): Creates a mXn matrix of random real numbers between 0 and 1.
• randi(m,n): Creates a random integer matrix of dimensions nXn and the maximum
value being m.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
v = [7 4 2]
When v is a vector, creates a square matrix with
diag(v) A = diag(v)
elements of v in the diagonal.
A = [7 0 0; 0 4 0; 0 0 2]
A = [1 2 3; 4 5 6; 7 8 9]
When A is a matrix, creates a vector from the
diag(A) Vec = diag(A)
diagonal elements of A.
Vec = [1; 5; 9]
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Practice Problems
1. Create a column vector in which the first element is 38.5, the elements decrease with
increment of -3.5, and the last element is 0. what is the number of elements in the created
vector?
2. Create a vector A that has 14 elements of which the first is 49, the increment is -3, and
the last term is 10. Then, using the colon symbol, create a new vector B that has 8
elements. The first four elements are the first four element of vector A and last four
element are the last four element of vector A.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
OPERATION
ON
ARRAYS
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
• Array operations are element to element arithmetic operations for matrices of compatible
size. Array operations are performed by using a dot before the arithmetic symbol between
two arrays.
for ex. For arrays A and B, the array product is calculated by A.*B
• Matrix operations are different from array operations they follow the rules of linear
algebra. Matrix operations are performed by using only the arithmetic symbol between two
matrices.
for ex. For matrices A and B, the matrix product is calculated by A*B
• The addition and subtraction in both array and matrix operations are same.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Matrix Multiplication:
• The multiplication operator ‘*’ is executed by MATLAB according to the rules of
linear algebra. This means that if A and B are two matrices, the operation A*B can be
carried out if the number of columns in matrix A is equal to the number of rows in
matrix B.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Array Multiplication:
• The multiplication operator ‘.*’ is used for array multiplication. The only
condition for array multiplication is that the arrays to be multiplied must be
of same size.
• for ex. Given matrices
A= ,B=
Matrix Division:
• Left Division (\) = It is used to solve AX = B.
in the equation X and B are column vectors.
.^ Exponentiation
./ Right Division
.\ Left Division
median(A) If A is a vector, returns the median value of the elements of the vector.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj
Calculate the scalar (dot) product of two vectors a and b. The vectors
dot(a, b)
can each be row or column vectors.
Calculate the cross product of two vectors a and b (a x b). The two
cross(a, b)
vectors must have each three elements.
Problems
1. Given Matrices: A = , B = , C =
S is the array multiplication of A and D, where D is matrix multiplication of C and B.
Determine the value of S.
2. Given Matrix H =
Find the transpose, inverse and determinant of matrix H.