CPA All Chapters
CPA All Chapters
Textbook: A. Gilat, Matlab: An Introduction with Applications 4th Edition. John Wiley
& Sons, Incorporated, 2017. (Chapter 1: Introduction)
INTRODUCTION TO MATLAB 3 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
• Mathematics
• An algorithm
CURRENT WORKSPACE
FOLDER WINDOW
WINDOW
COMMAND WINDOW
COMMAND HISTORY
WINDOW
INTRODUCTION TO MATLAB 6 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
• Editing:
Command-line editing:
• Left-arrow, Right-arrow
Comment
Comment line is not executed when ENTER is
pressed
• It is not interactive.
• whos Lists all the current variables in more detail than who.
• clc clears the command window (but does not clear the saved
variables in the workspace)
• clear all deletes all the saved variables from the workspace (but
does not clear the command window) and brings the cursor at top
Busy…
INTRODUCTION TO MATLAB 21 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
Command History
Addition: 3+2
Subtraction: 3-2
Multiplication: 3*2
Division: 3/2 or 2\3
Exponent: 3^2
INTRODUCTION TO MATLAB 23 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
Order of Precedence
INTRODUCTION TO MATLAB 24 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
Display formats
Command Description Example
>> 290/7
format short
upto 4 decimal digits ans =
(default)
41.4286
>> 290/7
ans =
format long upto 15 decimal digits
41.4285714285
71431
>> 290/7
format short e scientific notation with 4 decimal digits ans =
4.1429e+001
>> 290/7
ans =
format long e scientific notation with 15 decimal digits
4.14285714285
7143e+001
INTRODUCTION TO MATLAB 26 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
Display formats
Command Description Example
>> 290/7
format short g 5 s.g.f of floating digits ans =
41.429
>> 290/7
ans =
format long g 15 s.g.f of floating digits
41.4285714285
714
>> 290/7
format bank upto 2 decimal digits ans =
41.43
Eliminates empty lines to allow more lines with information
format compact
displayed on the screen.
format loose
Adds empty lines (opposite of compact).
(default)
INTRODUCTION TO MATLAB 27 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
Infinity:
1/0
inf + 3
Not-a-Number (NaN):
0/0
INTRODUCTION TO MATLAB 29 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
break for
case function end
catch global return
classdef if spmd
continue otherwise switch
else parfor try
elseif persistent while
INTRODUCTION TO MATLAB 32 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
ans
pi
…
INTRODUCTION TO MATLAB 33 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
a= 4
a = sqrt(4)
INTRODUCTION TO MATLAB 34 of 38
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS
Practice …..
feet = fix(40/12)
inches = rem(40, 12)
INTRODUCTION TO MATLAB
Textbook: A. Gilat, Matlab: An Introduction with Applications 6th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 1: Introduction)
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 8 of 18
Practice …..
4 inches
feet = fix(40/12)
inches = rem(40,12)
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 9 of 18
Script File
• Can be typed and edited in any text editor and then pasted
into the MATLAB editor.
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 10 of 18
Or
Ctrl + N
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 11 of 18
Script File
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 12 of 18
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 13 of 18
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 14 of 18
*Appears next to
filename.m* when changes
are made to the saved M file
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 15 of 18
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 16 of 18
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 17 of 18
• Press F5
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
FUNCTIONS | SCRIPT FILE 18 of 18
• Press ENTER
METU NCC
CREATING ARRAYS
• Transpose operator
• Array addressing
Textbook: A. Gilat, Matlab: An Introduction with Applications 6th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 2: Creating Arrays)
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 3 of 47
Arrays
• Arrays: List of numbers arranged in rows and/or columns
• One-dimensional array:
a row or a column of numbers
Vectors
• Two-dimensional array:
Collection of numbers arranged in rows and columns
Matrices.
Arrays
Element: A (1,2)
Position of a point
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 6 of 47
or
Column vector:
Position of a point
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 7 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 8 of 47
Elements in a row
separated by:
• comma
• space
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 9 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 10 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 11 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 12 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 13 of 47
• linspace(xi, xf, n)
where
xi first element
xf last element
n number of elements
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 14 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 15 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 17 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 18 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 19 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 20 of 47
eye command
• Square matrix
• Identity matrix
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 21 of 47
eye command
First 4 rows=
4×4 identity
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 22 of 47
eye command
First 4 rows=
4×4 identity
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 23 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 24 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 25 of 47
Transposing Matrices
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 26 of 47
.
Array addressing
Element: A (1,2)
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 27 of 47
.
Array addressing
Change to 8
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 28 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 29 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 30 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 31 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 32 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 33 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 34 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 35 of 47
.
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 36 of 47
.
Element numbering:
column by column
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 37 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 38 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 40 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 41 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 42 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 43 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 44 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 45 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 46 of 47
METU NCC
CREATING ARRAYS BUSHRA FATIMA
ARRAYS | VECTORS | MATRICES | TRANSPOSE | ADDRESSING AN ARRAY | ADD/DELETE 47 of 47
METU NCC
MATHEMATICAL OPERATIONS WITH
ARRAYS
Bushra Fatima
NED University of Engineering & Technology
May 13 2020
Outline
• Addition and Subtraction
• Multiplication
• Division
Using arrays
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 4 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 5 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 6 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 7 of 37
Adding/Subtracting a Scalar
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 8 of 37
Adding/Subtracting a Scalar
Adding/Subtracting a Scalar
Precedence Operation
Array Multiplication
Multiplication operator (*)
Condition A*B:
A=(𝑟𝐴 , 𝑐𝐴 ) 𝐵=(𝑟𝐵 , 𝑐𝐵 )
• 𝑐𝐴 = 𝑟𝐵
Array Multiplication
4x3 3x2
𝑐𝐴 = 𝑟𝐵
(𝑟𝐴 , 𝑐𝐵 ) = (4 , 2)
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 12 of 37
Array Multiplication
3x3 3x2 3 x2
1 4 3 5 4 1.5 + 4.1 + 3.2 (1.4 + 4.3 + 3.6)
2 6 1 1 3 = 2.5 + 6.1 + 1.2 (2.4 + 6.3 + 1.6)
5 2 8 2 6 5.5 + 2.1 + 8.2 (5.4 + 2.3 + 8.6)
15 34
= 18 32
43 74
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 13 of 37
Array Multiplication
• If A & B are square matrices
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 14 of 37
Array Multiplication
3x3
3x3
3x3
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 15 of 37
Array Multiplication
Multiplication of matrices is not commutative
A*B ≠ B*A
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 16 of 37
Array Multiplication
2x3 * 2x3
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 17 of 37
Scalar product
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 18 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 19 of 37
3 x3 matrix
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 20 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 21 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 22 of 37
Identity matrix
• square matrix
• 𝐴∗𝐼 =𝐴
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 23 of 37
Identity matrix
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 24 of 37
Identity matrix
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 25 of 37
Inverse of a matrix
• The matrix B is the inverse of the matrix A if, when the
two matrices are multiplied, the product is the identity
matrix.
It is a square matrix
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 27 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 28 of 37
Determinant of a matrix
• a number associated with each square matrix.
det(A) or
|A|
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 29 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 30 of 37
Array Division
MATLAB has two types of array division:
Left division \
Right division /
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 31 of 37
Left Division \
The left division is used to solve the matrix equation
𝐴𝑋 = 𝐵 x =B/A
where X and B are column vectors.
−1 −1 −1
𝐴 𝐴𝑋 = 𝐴 𝐵 𝐼. 𝑋 = 𝐴 𝐵
𝑋= −1
𝐴 𝐵
In MATLAB the last equation can be written using the left
division character \
𝑋 = A\B METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 32 of 37
Right Division /
The right division is used to solve the matrix equation
𝑋𝐶 = 𝐷
Where X and D are row vectors.
−1 −1 −1
𝑋. 𝐶𝐶 = 𝐷. 𝐶 𝑋 = 𝐷. 𝐶
In MATLAB the last equation can be written using the right
division character /:
𝑋 = 𝐷/C
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 33 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 35 of 37
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ADD & SUBTRACT | MULTIPLY | DIVIDE 37 of 37
METU NCC
CREATING ARRAYS
Bushra Fatima
NED University of Engineering & Technology
May 12, 2020
Outline
• Built-in MATLAB functions for arrays
• Strings
Textbook: A. Gilat, Matlab: An Introduction with Applications 6th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 2: Creating Arrays)
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 3 of 18
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 4 of 18
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 6 of 18
Strings
• an array of characters.
• ‘ strings ’
• Can include:
Letters Other symbols
Digits Spaces
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 9 of 18
Strings
maroon with the first single quote purple with the last single quote
Output
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 10 of 18
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 11 of 18
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 12 of 18
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 13 of 18
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 14 of 18
No. of characters 14
• a one-line string:
a row vector
No.of elements = no. of characters
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 15 of 18
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 16 of 18
String in a matrix
METU NCC
CREATING ARRAYS BUSHRA FATIMA
FUNCTIONS | STRINGS 17 of 18
METU NCC
MATHEMATICAL OPERATIONS WITH
ARRAYS
Bushra Fatima
NED University of Engineering & Technology
June 17, 2020
Outline
• Element by Element operations
Using arrays
Textbook: A. Gilat, Matlab: An Introduction with Applications 4th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 3: Mathematical operations with arrays)
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 3 of 21
•Element-by-element
. multiplication, division, or
exponentiation of two vectors or matrices is
entered in MATLAB by typing a period . in front
of the arithmetic operator.
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 4 of 21
Symbol Description
.* Element by element Multiplication
.^ Element by element Exponentiation
./ Element by element Right Divison
. .\ Element by element Left Divison
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 5 of 21
𝑎 = [𝑎1 𝑎2 𝑎3 𝑎4 ] b= [𝑏1 𝑏2 𝑏3 𝑏4 ]
𝑎.∗ 𝑏 = [𝑎1 𝑏1 𝑎2 𝑏2 𝑎3 𝑏3 𝑎4 𝑏4 ]
𝑎./𝑏 = [𝑎1 /𝑏1 𝑎2 /𝑏2 𝑎3 /𝑏3 𝑎4 /𝑏4 ]
.
Array product
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 7 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 8 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 9 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 11 of 21
𝐵 =𝑃 1+𝑟 𝑡
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 12 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 13 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 15 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 16 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 17 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 18 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 19 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 20 of 21
METU NCC
MATHEMATICAL OPERATIONS WITH ARRAYS BUSHRA FATIMA
ELEMENT BY ELEMENT OPERATIONS | FUNCTIONS 21 of 21
METU NCC
CHAPTER # 4
USING SCRIPT FILES & MANAGING DATA
Bushra Fatima
NED University of Engineering & Technology
June 18, 2020
Outline
MATLAB workspace and window
Output Commands:
Textbook: A. Gilat, Matlab: An Introduction with Applications 4th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 4: Using Script Files & Managing Data)
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 3 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 5 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 6 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 7 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 8 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 9 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 10 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 11 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 12 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 13 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 14 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 15 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 16 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 18 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 19 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 20 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 21 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 22 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 24 of 30
Next command
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 25 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 27 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 28 of 30
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 29 of 30
i or d
S string
METU NCC
USING SCRIPT FILES & MANAGING DATA BUSHRA FATIMA
WORKSPACE | INPUT | OUTPUT 30 of 30
METU NCC
PROGRAMMING WITH MATLAB
RELATIONAL & LOGICAL OPERATORS
Bushra Fatima
NED University of Engineering & Technology
June 23, 2020
Outline
• Introduction
• Relational Operators
• Logical Operators
Textbook: A. Gilat, Matlab: An Introduction with Applications 4th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 6: Programming with MATLAB)
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 3 of 29
Introduction
• A computer program is a sequence of computer
commands.
• Simple execution:
Order of execution order they are typed.
• Not so simple:
Example: cost of a mailing package
Relational Operators
• A relational operator compares two numbers by
determining whether a comparison statement is true
(1) or false (0).
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 6 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 7 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 8 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 9 of 29
Array comparison
element-by-element comparison
Logical
Array
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 10 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 11 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 12 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 13 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 14 of 29
Array addressing
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 15 of 29
Order of precedence
Precedence Operation
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO | RELATIONAL OPERATORS | LOGICAL OPERATORS 17 of 29
Order of precedence
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 18 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 20 of 29
Order of Precedence
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 21 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 22 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 23 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 24 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 25 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 26 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 27 of 29
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 28 of 29
-5<x=1
1<-1=0
-5<x=1
-2<-1=11
1&1=1
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
INTRO| RELATIONAL OPERATORS | LOGICAL OPERATORS 29 of 29
y<7=1 ~y=0
~1=0 0<7=1
y>=8=0
x<-1=1
0|1=1
~1=0 METU NCC
PROGRAMMING WITH MATLAB
LOGICAL FUNCTIONS
CONDITIONAL STATEMENTS
Bushra Fatima
NED University of Engineering & Technology June 29, 2020
OUTLINE
1. LOGICAL FUNCTIONS
2. CONDITIONAL STATEMENTS
1. if – end STRUCTURE
2. if – else - end STRUCTURE
3. if – elseif – else - end STRUCTURE
Textbook: A. Gilat, Matlab: An Introduction with Applications 4th Edition. John Wiley &
Sons, Incorporated, 2016. (Chapter 6: Programming with MATLAB)
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 3 of 16
1. and(A,B) = A&B
2. or(A,B)=A|B
3. not(A)=~A
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 4 of 16
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 5 of 16
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 6 of 16
Truth Table
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 7 of 16
CONDITIONAL STATEMENT
• Decision-making command:
• execute the command
• or to skip the command
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 8 of 16
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 9 of 16
If-end statement
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 10 of 16
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 11 of 16
If-else-end statement
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 12 of 16
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 13 of 16
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 14 of 16
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 15 of 16
Exercise
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOGICAL FUNCTIONS | IF-END | IF-ELSE-END | IF-ELSEIF-ELSE-END 16 of 16
Exercise
METU NCC
PROGRAMMING WITH MATLAB
SWITCH STATEMENTS
Bushra Fatima
NED University of Engineering & Technology
July 4, 2020
Outline
• Switch statement
Textbook: A. Gilat, Matlab: An Introduction with Applications 6th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 6: Programming with MATLAB)
switch statement
For women:
THR = [ ( 206 - 0.88 x AGE )-RHR ] x INTEN + RHR
where AGE is the person’s age, RHR the resting heart rate, and INTEN the fitness
level (0.55 for low, 0.65 for medium, and 0.8 for high fitness). Write a
program in a script file that determines the THR. The program asks users to
enter their gender (male or female), age (number), resting heart rate (number),
and fitness level (low, medium, or high). The program then displays the training
heart rate. Use the program for determining the training heart rate for the
following two individuals:
(a) A 21-years-old male, resting heart rate of 62, and low fitness level.
(b) A 19-years-old female, resting heart rate of 67, and high fitness level.
Method 1: Using input function with switch
Method 1: Using input function with switch
Method 2: Using menu function with switch
Method 2: Using menu function with switch
PROGRAMMING WITH MATLAB
FOR LOOP
Bushra Fatima
NED University of Engineering & Technology
July 07, 2020
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOPS | FOR LOOP 2 of 14
• Both kinds of loops can be terminated at any time with the break
command METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP 3 of 14
for-end loops
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP 4 of 14
• k = 3:7 k = 3, 4, 5, 6, 7
• k = [7 9 –1 3 3 5]
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP 8 of 14
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP 9 of 14
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP 10 of 14
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP 11 of 14
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP 14 of 14
METU NCC
PROGRAMMING WITH MATLAB
FOR LOOPS
Bushra Fatima
NED University of Engineering & Technology
July 13, 2020
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 2 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 3 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 4 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 5 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 6 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 7 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 8 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 9 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 10 of 11
METU NCC
PROGRAMMING WITH MATLAB BUSHRA FATIMA
LOOP | FOR LOOP | WHILE LOOP | NESTED LOOP 11 of 11
METU NCC
PROGRAMMING WITH MATLAB
WHILE LOOPS
Bushra Fatima
NED University of Engineering & Technology
July 14, 2020
while-end loops
∞
𝑥
𝑥𝑛 𝑥2 𝑥3
𝑒 = =1+𝑥+ + +⋯
𝑛! 2! 3!
𝑛=0
while-end loop: sample problem 6-7
Use the program to calculate: 𝑒 2 :
Program flow:
• The program calculates 𝑒 2 by adding terms of the series
and stopping when the absolute value of the term that
was added last is smaller than 0.0001.
• Use a while-end loop, but limit the number of passes to
30.
• If in the 30th pass the value of the term that is added is
not smaller than 0.0001, the program stops and displays a
message that more than 30 terms are needed.
while-end loop: sample problem 6-7
while-end loop: testing prime numbers
PROGRAMMING WITH MATLAB
NESTED LOOPS
Bushra Fatima
NED University of Engineering & Technology
July 19, 2020
Nested loops
• This means that a loop and/or a conditional statement can start (and
end) within another loop or conditional statement.
• It must be remembered, however, that each if, case, for, and while
statement must have a corresponding end statement.
Nested loops
Nested loops: Review Problem 8
Column (j )
1 2 3
2 2 2
1 (1+1)/1 (1+2)/2 (1+3)/3
2 2 2
2 (2+1)/1 (2+2)/2 (2+3)/3
Row (i ) 2 2 2
3 (3+1)/1 (3+2)/2 (3+3)/3
2 2 2
4 (4+1)/1 (4+2)/2 (4+3)/3
Nested loops: Review Problem 8
break command
• break command terminates the execution of the loop
1 19 27 13 5 3
2 19 13 27 5 3
1
3 19 13 5 27 3
4 19 13 5 3 27
Outside nested
Loop Loop 19 13 5 3 27
1 13 19 5 3 27
2 2 13 5 19 3 27
3 13 5 3 19 27
Outside nested
Loop Loop 13 5 3 19 27
1 5 13 3 19 27
3
2 5 3 13 19 27
Outside nested
Loop Loop 5 3 13 19 27
4 1 3 5 13 19 27
Solution
Script file:
clear, clc
BOS=[2.67 1.00 1.21 3.09 3.43 4.71 3.88 3.08 4.10 2.62 1.01 5.93];
SEA=[6.83 3.63 7.20 2.68 2.05 2.96 1.04 0.00 0.03 6.71 8.28 6.85];
disp('Part (a)')
B_T=sum(BOS);
B_A=mean(BOS);
S_T=sum(SEA);
S_A=mean(SEA);
fprintf('The total precipitation in Boston in 2012 was %.2f in',B_T)
fprintf(' and average %.2f in\n',B_A)
fprintf('The total precipitation in Seattle in 2012 was %.2f in',S_T)
fprintf(' and average %.2f in\n\n',S_A)
disp('Part (b)')
B_D=sum(BOS>B_A);
S_D=sum(SEA>S_A);
fprintf('Boston had %i months above average and Seattle %i
months\n\n',B_D,S_D)
disp('Part (c)')
BltS=sum(BOS<SEA);
m=1:12;
fprintf('The precipitation was lower in Boston in the following %i
months:',BltS)
fprintf(' %i',m(BOS<SEA))
fprintf('\n')
Command Window:
Part (a)
The total precipitation in Boston in 2012 was 36.73 in and average 3.06 in
The total precipitation in Seattle in 2012 was 48.26 in and average 4.02 in
Part (b)
Boston had 7 months above average and Seattle 5 months
Part (c)
The precipitation was lower in Boston in the following 6 months: 1 2 3 10 11
12
USER-DEFINED FUNCTIONS AND FUNCTION
FILES
Bushra Fatima
NED University of Engineering & Technology
Sept 02, 2020
Introduction
• simple function that needs to be calculated only
once typed as part of the program.
• Calculation
• Assignments
• any built-in or user-defined functions
• flow control (conditional statements and/or loops)
• Comments
• blank lines
• interactive input and output.
Local Variables
• All the variables in a function file are local (the input and output arguments and any variables
that are assigned values within the function file).
• This means that the variables are defined and recognized only inside the function file.
• When a function file is executed, MATLAB uses an area of memory that is separate from the
workspace (the memory space of the Command Window and the script files).
• In a function file the input variables are assigned values each time the function is called.
• These variables are then used in the calculations within the function file.
• When the function file finishes its execution, the values of the output arguments are
transferred to the variables that were used when the function was called.
• All this means that a function file can have variables with the same names as variables in the
Command Window or in script files.
• The function file does not recognize variables with the same names as have been assigned
values outside the function.
• The assignment of values to these variables in the function file will not change their assignment
elsewhere.
SAVING A FUNCTION FILE
• A function file must be saved before it can be used.
• In this way the function is called (used) by using the function name.
• If a function file is saved with a different name, the name it is saved under must be
used when the function is called.
• The function can be called from the Command Window, from a script file, or from another
function.
• To use the function file, the folder where it is saved must either be in the current folder or
be in the search path
• A function can be used by assigning its output to a variable (or variables), as a part of a
mathematical expression, as an argument in another function, or just by typing its name in
the Command Window or in a script file.
• In all cases the user must know exactly what the input and output arguments are.
• An input argument can be a number, a computable expression, or a variable that has an
assigned value.
• The arguments are assigned according to their position in the input and output argument
lists in the function definition line.
Sample Problem
Sample Problem
Sample Problem’s Solution
COMPARISON
Script Files BETWEEN SCRIPT FILES AND FUNCTION FILES
Function
Both script and function files are saved with the extension .m (that is why they
are sometimes called M-files).
The first executable line in a function file must
be the function definition line.
The variables in a script file are recognized in The variables in a function file are local.
the Command Window.
• Script files contain a sequence of MATLAB Function files can accept data through input
commands (statements). arguments and can return data through
output arguments.
Vertical direction:
𝟏
Velocity 𝒗𝒚 = 𝒗𝟎𝒚 − 𝐠𝒕 Position 𝒚 = 𝒗𝟎𝒚 𝒕 − 𝐠𝒕
𝟐
Highest Point:
𝒗𝟎𝒚 𝒗𝟐 𝟎𝒚
Velocity 𝒗𝒚 = 𝟎 Time 𝒕𝒎𝒂𝒙 = Height 𝒉𝒎𝒂𝒙 =
𝒈 𝟐𝒈
Bushra Fatima
NED University of Engineering & Technology
July 25, 2020
Outline
• Introduction
• Commands
• plot
• fplot
• format
Textbook: A. Gilat, Matlab: An Introduction with Applications 6th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 5: Two dimensional pots)
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 3 of 37
Introduction
o Plots:
• standard plots with linear axes
• plots with logarithmic and semi-logarithmic axes
• bar and stairs plots
• polar plots
• three-dimensional contour surface
• mesh plots
Introduction
o Formatting of plots:.
• line type (-, - -, etc.),
• Color (r, g, b etc)
• -
thickness ( , - , etc.),
• line markers ( * , o, x etc)
• grid lines
• titles
• legends
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 5 of 37
Introduction
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 6 of 37
plot command
• The plot command is used to create 2D plots.
>> plot(x,y)
• The arguments x and y are each a vector (one-
dimensional array).
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 7 of 37
plot command
The plot command has additional, optional arguments that can be used to
specify:
• the color of the line (blue, black, red etc.)
• style of the line ( - , --, : etc. )
• type of markers ( * , + , o etc. )
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 8 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 9 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 10 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 11 of 37
Line specifiers
• The specifiers are typed inside the plot command as
‘strings’.
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 12 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 13 of 37
2 METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 14 of 37
2
1 2 3 4 5 6 7 8 9 10
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 15 of 37
Example plots
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 16 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 17 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 18 of 37
Example plot
plot(x,y,‘-mo’,‘LineWidth’,2,‘markersize’,12,
‘MarkerEdgeColor’,‘g’,‘markerfacecolor’,‘y’)
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 19 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 20 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 21 of 37
line command
• The format of the line command is almost the same as the plot
command
line(x,y,‘PropertyName’,PropertyValue)
• (Optional) Properties with values that can be used to specify the line style,
color, and width, marker type, size, and edge and fill colors
line(x,y,‘linestyle’,‘--’,‘color’,‘r’,‘marker’,‘o’)
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 22 of 37
Solution (method 1)
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 24 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 25 of 37
Solution (Method 2)
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 26 of 37
Solution (Method 3)
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 27 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 28 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 29 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 30 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 31 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 32 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 33 of 37
title command
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 34 of 37
legend command
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 36 of 37
METU NCC
TWO-DIMENSIONAL PLOTS BUSHRA FATIMA
INTRO | PLOT COMMAND | FPLOT COMMAND | FORMAT 37 of 37
METU NCC
Polynomials,
Curve Fitting, and
Maxima Minima
Polynomials are mathematical expressions that are frequently used for
problem solving and modeling in science and engineering.
Curve fitting is a process of finding a function that can be used to model
data
Maximum and Minimum points of function.
Polynomial
# of elements: Degree of the polynormial + 1
Linear: 2 elements
Quadratic: 3 elements
1
Value of a Polynomial
Example
2
Roots of a Polynomial
Example
3
Roots of a Polynomial
Example
4
Addition, Multiplication, and Division of
Polynomials
Two polynomials can be added (or subtracted) by adding (subtracting)
the vectors of the coefficients. If the polynomials are not of the same
order (which means that the vectors of the coefficients are not of the
same length), the shorter vector has to be modified to be of the same
length as the longer vector by adding zeros (called padding) in front
Example
10
5
Multiplication
11
Example
12
6
Division
13
Example
14
7
Derivatives of Polynomials
15
16
8
Curve Fitting
• Curve fitting, also called regression analysis, is a process of fitting a
function to a set of data points. The function can then be used as a
mathematical model of the data. Since there are many types of
functions (linear, polynomial, power, exponential, etc.), curve fitting
can be a complicated process.
17
18
9
polyfit function
polyfit command does not plot the graph, it only i the polynomial's coefficients.
19
20
10
Example
21
22
11
log(y)=log(b) + m*log(x)
ln(y)=ln(b)+mx
log(y)=log(b)+mx
1/y=mx+b
23
24
12
Example
25
Minima
26
13
Example
27
Maxima
28
14
LAB #01
REGRESSION ANALYSIS
OR
CURVE FITTING
Bushra Fatima
NED University of Engineering & Technology August 19, 2020
Regression Analysis or Curve Fitting
•Curve fitting, also called regression analysis, is a
process of fitting a function to a set of data points.
•The function can be used as a mathematical
model of the data.
•Since there are many types of functions (linear,
polynomial, power, exponential, etc.), curve fitting
can be a complicated process.
Curve Fitting with polynomials
Polynomials can be used to fit data points in two ways.
• When n points (xi, yi) are given polynomial of degree (n-1) that passes
through all the points.
!!! polyfit function does NOT plot the graph. It only gives the coefficients
A set of seven points is given by (0.9, 0.9), (1.5, 1.5), (3, 2.5), (4, 5.1),(6, 4.5), (8, 4.9),
and (9.5, 6.3).
ln 𝑦 = 𝑚 ln 𝑥 + ln 𝑏
l𝑜𝑔 𝑦 = 𝑚𝑥 + l𝑜𝑔 𝑏
Curve Fitting with functions other than polynomials
Curve Fitting with functions other than polynomials
Solution: Scatter Plot (linear axes)
1. Linear function
2. Logarithmic function
3. power function
4. Exponential function
5. Reciprocal function
Solution: Scatter Plot (semi log axis)
Checking for Exponential function
Solution: Scatter Plot (linear axes)
Checking for reciprocal function
Solution: selected plot:
Function chosen: Exponential function
t=0:0.5:5;
w=[6 4.83 3.7 3.15 2.41 1.83 1.49 1.21 0.96 0.73 0.64];
p=polyfit(t,log(w),1);
m=p(1)
b=exp(p(2))
tp=0:0.1:5;
wp=b*exp(m*tp);
plot(t,w,'o',tp,wp)
xlabel('t'), ylabel('w')
Solution: selected plot:
Function chosen: Exponential function
The boiling temperature of water TB at various altitudes h is given in the following table.
Determine a linear equation in the form TB = mh + b that best fits the data. Use the equation for
calculating the boiling temperature at 5,000 m. Make a plot of the points and the equation.
1- Write a user-defined function that multiplies two polynomials. Name the function
p=polymult(p1, p2). The two input arguments p1 and p2 are vectors of the coefficients of the
two polynomials. The output argument p is the resulting polynomial.
Use the function to multiply the following polynomials:
Solution
Function script
function p = polymult(p1,p2)
na=length(p1); nb=length(p2);
if nb > na
d=p1; p1=p2;
clear b
p2=d;
nd=na; na=nb; nb=nd;
end
for k=1:nb
p(k)=0;
for i=1:k
p(k)=p(k)+p1(i)*p2(k+1-i);
end
end
for k=nb+1:na
p(k)=0;
for i=k-nb+1:k
p(k)=p(k)+p1(i)*p2(k+1-i);
end
end
for k=na+1:na+nb-1
p(k)=0;
for i=k-nb+1:na
p(k)=p(k)+p1(i)*p2(k+1-i);
end
end
SYMBOLIC MATH
Bushra Fatima
NED University of Engineering & Technology
Aug 10, 2020
Outline
Textbook: A. Gilat, Matlab: An Introduction with Applications 6th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 11: Symbolic Math)
Numerical VS Symbolic Operations
• When a numerical expression is executed by MATLAB, the
outcome is also numerical.
• exact number [e.g. 1/4 gives 0.2500]
• or a floating point–approximated [e.g. 1/3 gives
0.3333—an approximated value]
• Symbolic operations, which are mathematical operations
with expressions that contain symbolic variables (variables
that don’t have specific numerical values when the
operation is executed). The result of such operations is
also a mathematical expression in terms of the symbolic
variables.
symbolic objects
• A symbolic object can be
• a variable (without a preassigned numerical value),
• a number,
• or an expression made of symbolic variables and
numbers.
• A symbolic expression is a mathematical expression
containing one or more symbolic objects.
• When typed, a symbolic expression may look like a
standard numerical expression.
• However, because the expression contains symbolic
objects, it is executed by MATLAB symbolically.
Step one: Create symbolic objects
String as a single letter or combination of letters with no spaces
symbolic object as a number
Creating several symbolic objects in one command
Creating symbolic expressions
Difference b/w exact and approximate solutions
Difference b/w exact and approximate solutions
Expand command
Expand command
factor command
simplify command
simplify command
Solving algebraic equations
Solving algebraic equations
Problem: Intersection of a circle and a line
Differentiation
Differentiation
Integration
Integration
Integration
Ordinary Differential Equations
Ordinary Differential Equations
Ordinary Differential Equations
Ordinary Differential Equations
Sample problem
Plot the function:
3
𝑦 = 3𝑥 − 26𝑥 + 10
and its first and second
derivatives,
for −2 ≤ 𝑥 ≤ 4 all in the
same plot.