0% found this document useful (0 votes)
16 views

Matlab Lecture 1

This document provides an overview of an introduction to MATLAB lecture. It discusses MATLAB as a software package for numerical computation and visualization. The lecture covers MATLAB's interface and basic commands. It also introduces fundamental MATLAB concepts like variables, arrays, matrices, and built-in math and trigonometric functions. The document aims to familiarize students with MATLAB's programming environment and basic functionality.

Uploaded by

Aaditya Tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Matlab Lecture 1

This document provides an overview of an introduction to MATLAB lecture. It discusses MATLAB as a software package for numerical computation and visualization. The lecture covers MATLAB's interface and basic commands. It also introduces fundamental MATLAB concepts like variables, arrays, matrices, and built-in math and trigonometric functions. The document aims to familiarize students with MATLAB's programming environment and basic functionality.

Uploaded by

Aaditya Tyagi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Process Simulation Lab

(An Undergraduate Lab Course)

Lecture 1
Introduction to MATLAB

Course Instructor: Dr. Shabih Ul Hasan


Motilal Nehru National Institute of Technology Allahabad
Prayagraj - 211004

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

• Rules about Variable Name


 Must begin with a letter.
 Can be up to 63 character long.
 Can contain letters, digits, and underscore character.
 Matlab is case sensitive: it distinguishes between uppercase and lowercase letters.
 Avoid using the name of a built-in-function for a variable.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

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:

break case catch classdef countinue else elseif

end for function global otherwise if parfor

persistent return spmd switch try while


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

SOME BASIC MATLAB


COMMANDS
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Commands for Matlab Workspace


• The Matlab workspace consists of the sets of variables that are defined and stored during
a MATLAB session.

Command Description

who displays a list of variables currently in workspace

displays a list of the variables currently in workspace and information


whos
about their size, bytes, and class

clear Clears the variables in the workspace

clear A B Clears the variables A and B in the workspace


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Some General Matlab Commands

Command Description

clc Clears command window

Datetime(‘now’) Gives the current date and time

Semicolon(;) It is used after a command to suppress the result

Percentange(%) Used to give human instructions or help, matlab skips this.

Up arrow(↑) Used to recall last used commands.


Used to continue writing the command to the next line without
Three dots(…)
executing the first line.
quit To end the matlab session
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

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

Some Important Formatting Commands


Command Description

format short Fixed-point with 4-decimal digits.

format long Fixed-point with 15-decimal digits.

format short e Scientific notation with 4 decimal digits.

format long e Scientific notation with 15 decimal digits.

format bank Two decimal digits.

Eliminates empty lines to allow more lines with information displayed


format compact
on screen.

format loose Adds empty lines (opposite of compact).


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Elementary Math Built-in Functions

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

round(x) Round to the nearest integer.

fix(x) Round toward zero.

ceil(x) Round toward infinity.

floor(x) Round toward minus infinity.

rem(x,y) Returns the remainder after x is divided by y.


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

ARRAYS
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Creating Arrays

• Creating one-dimensional array (Vector).

• Creating two-dimensional array (Matrix).

• Array Addressing

• Using a colon : In Addressing Array

• Adding element to existing variables

• Deleting elements

• Built-in-functions for handling array


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Creating One-Dimensional Array


• Variable_name : [type vector elements]
Example :
Year = [1984 1986 1988 1990 1992 1994 1996 1998 2000] (row vector)
Population = [120; 122; 125; 127; 128; 131; 132; 135; 137] (column vector)

• 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 One-Dimensional Array


• Creating a vector with constant spacing by specifying the first term, the spacing, and the
last term.
Variable_name = [first term : spacing : last term]
Example: x = [1:2:13]
x = 1 3 5 7 9 11 13

• 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

Creating Two-Dimensional Array


• Creating a 3X3 matrix

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

Array Addressing or indexing


• Vector: A(element position)
A = [35 46 77 15 16 84 94 56]
A(4) = 15

• Matrix: A(row, column)


A = [1 2 3; 4 5 6; 7 8 9]
A(2,3) = 6
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Using a colon : In Addressing Array


• For a Vector
V(:) = Refer to all elements of vector V.
V(m:n) = Refer to elements m through n of the vector V.

• 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

Adding Elements in a Matrix


• To add element/elements to a matrix/vector, address the elements in the
matrix and provide them some value.

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=

NOTE: If we add some elements of matrix/vector without defining other elements


required to complete the matrix. By default the value of all other elements is set to
zero.
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

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

Built-in-functions for handling array


Function Description Example
A = [5 9 4 2]
length(A) Returns number of elements in the vector A.
length(A) = 4

Returns a row vector [m, n], where m and n are A = [6 1 4 0 12; 5 19 6 8 2]


size(A)
the size of m x n of the array A. size(A) = 2 5

Creates a m by n matrix from the elements of A = [5 1 6; 8 0 2]


reshape(A) matrix A. The elements are taken column after B = reshape(A,3,2)
column. Matrix A must have m times n element. B = [5 0; 8 6; 1 2]

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 and Matrix Operations

• 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

• Note: dot is not used for addition and substraction.


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Addition and Subtraction


• Operator ‘+’ and ‘-’ can be used to add and subtract arrays of identical
size.

• 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=

the array multiplication is executed by the command C = A.*B


the result obtained is C =
SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Matrix Division:
• Left Division (\) = It is used to solve AX = B.
in the equation X and B are column vectors.

• Right Division(/) = It is used to solve XA = B.


in the equation X and B are row vectors.
Some Other Array Operations
• Assume,

Symbol Description Example

.^ Exponentiation

./ Right Division

.\ Left Division

Log(a) Natural logarithm log(a) =


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Built-in-function for Analyzing Arrays


Function Description

mean(A) If A is a vector, it returns mean value of the elements of the vector

max(A) If A is a vector, C is the largest element in A.


If A is a matrix, C is a row vector containing the largest element of each
column of A.

min(A) Same as max(A) but for smallest number.


If A is a matrix, it gives a row vector containing the sum of elements of
each column of A.

sort(A) If A is a vector, arranges all the elements in ascending order.

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

Built-in-function for Analyzing Arrays


Function Description

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.

sum(A) If A is a vector, returns the sum of the elements of the vector.

Transpose(A) or A’ Transpose of matrix A

det(A) Determinant of matrix A.

inv(A) Returns the inverse of a square matrix.


SHABIH UL HASAN (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

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.

You might also like