computer programming eciv 2303 chapter 3 mathematical...
TRANSCRIPT
Computer Programming
ECIV 2303
Instructor: Dr. Talal Skaik
Islamic University of Gaza
Faculty of Engineering
Chapter 3
Mathematical Operations with Arrays
1Dr. Talal Skaik 2018
3.1 ADDITION AND SUBTRACTION
In general, if A and B are two arrays (for example, 2 x 3 matrices),
2Dr. Talal Skaik 2018
3.1 ADDITION AND SUBTRACTION
3Dr. Talal Skaik 2018
3.1 ADDITION AND SUBTRACTION
4Dr. Talal Skaik 2018
3.1 ADDITION AND SUBTRACTION
5Dr. Talal Skaik 2018
When a scalar (number) is added to (or subtracted from) an array, the scalar is added to (or subtracted from) all the elements of the array.
3.2 ARRAY MULTIPLICATION
6Dr. Talal Skaik 2018
The operation A* B can be carried out only if the number of columns in matrix A is equal to
the number of rows in matrix B.
The result is a matrix that has the same number of rows as A and the same number of
columns as B.
For example, if A is a 4 x 3 matrix and B is a 3 x 2 matrix:
then the matrix that is obtained with the operation A*B has dimensions 4 x 2.
3.2 ARRAY MULTIPLICATION
7Dr. Talal Skaik 2018
A*B
3.2 ARRAY MULTIPLICATION
8Dr. Talal Skaik 2018
Numerical Example
3.2 ARRAY MULTIPLICATION
9Dr. Talal Skaik 2018
3.2 ARRAY MULTIPLICATION
10Dr. Talal Skaik 2018
3.2 ARRAY MULTIPLICATION
11Dr. Talal Skaik 2018
3.2 ARRAY MULTIPLICATION
12Dr. Talal Skaik 2018
3.2 ARRAY MULTIPLICATION
13Dr. Talal Skaik 2018
Linear algebra rules of array multiplication provide a convenient way for writing a system of
linear equations. For example, the system of three equations with three unknowns
can be written in a matrix form as
and in matrix notation as
Identity Matrix
14Dr. Talal Skaik 2018
The identity matrix is a square matrix in which the diagonal elements are 1s and the rest of
the elements are 0s.
An identity matrix can be created in MATLAB with the eye command.
When the identity matrix multiplies another matrix (or vector), that matrix (or vector) is
unchanged.
Inverse of a Matrix
15Dr. Talal Skaik 2018
The matrix B is the inverse of the matrix A if, when the two matrices are multiplied, the
product is the identity matrix.
Both matrices must be square, and the multiplication order can be BA or AB.
BA = AB = I
The inverse of a matrix A is typically written as A-1 .
In MATLAB the inverse of a matrix can be obtained either by raising A to the power of -1,
A-1, or with the inv (A) function.
Inverse of a Matrix
16Dr. Talal Skaik 2018
Inverse of a Matrix
17Dr. Talal Skaik 2018
Not every matrix has an inverse. A matrix has an inverse only if it is square and its determinant is not equal to zero.
Determinants
18Dr. Talal Skaik 2018
A determinant is a function associated with square matrices.
The determinant is typically denoted by det(A) or |A|.
The determinant is calculated according to specific rules. For a second-order 2 x 2 matrix,
the rule is:
ARRAY DIVISION
19Dr. Talal Skaik 2018
MATLAB has two types of array division, right division and left division.
Left division, \ :Left division is used to solve the matrix equation AX = B.In this equation X and B are column vectors.
ARRAY DIVISION
20Dr. Talal Skaik 2018
Right division, / :The right division is used to solve the matrix equation XC = D.
In this equation X and D are row vectors.
Solving three linear equations (array division)
21Dr. Talal Skaik 2018
Use matrix operations to solve the following system of linear equations.
4x- 2y+ 6z = 8
2x+ 8y+ 2z = 4
6x+10y+3z = 0
SolutionThe above system of equations can be written in the matrix form AX = B or in the form
XC= D:
Solving three linear equations (array division)
22Dr. Talal Skaik 2018
Solving three linear equations (array division)
23Dr. Talal Skaik 2018
3.4 ELEMENT-BY-ELEMENT OPERATIONS
24Dr. Talal Skaik 2018
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.
If two vectors a and b are a = [a1 a2 a3 a4] and b = [b1 b2 b3 b4], then element-by-element
multiplication, division, and exponentiation of the two vectors gives:
3.4 ELEMENT-BY-ELEMENT OPERATIONS
25Dr. Talal Skaik 2018
If two matrices A and B are
then element-by-element multiplication and division of the two matrices give:
3.4 ELEMENT-BY-ELEMENT OPERATIONS
26Dr. Talal Skaik 2018
Element-by-element exponentiation of matrix A gives:
3.4 ELEMENT-BY-ELEMENT OPERATIONS
27Dr. Talal Skaik 2018
3.4 ELEMENT-BY-ELEMENT OPERATIONS
28Dr. Talal Skaik 2018
3.4 ELEMENT-BY-ELEMENT OPERATIONS
29Dr. Talal Skaik 2018
Element-by-element calculations are very useful for calculating the value of a function at many values of its argument
3.4 ELEMENT-BY-ELEMENT OPERATIONS
30Dr. Talal Skaik 2018
3.5 USING ARRAYS IN MATLAB BUILT-IN MATH FUNCTIONS
31Dr. Talal Skaik 2018
if a vector with seven elements is substituted in the function cos (x), the result is a vector with seven elements in which each element is the cosine of the corresponding element in x.
3.5 USING ARRAYS IN MATLAB BUILT-IN MATH FUNCTIONS
32Dr. Talal Skaik 2018
An example in which the argument variable is a matrix is:
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
33Dr. Talal Skaik 2018
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
34Dr. Talal Skaik 2018
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
35Dr. Talal Skaik 2018
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
36Dr. Talal Skaik 2018
Inverse of a 2x2 Matrix
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
37Dr. Talal Skaik 2018
Example:
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
38Dr. Talal Skaik 2018
Inverse of a 3x3 Matrix:
3. 7 GENERATION OF RANDOM NUMBERS
39Dr. Talal Skaik 2018
The rand command: The rand command generates uniformly distributed random numbers with values between 0 and 1.
3. 7 GENERATION OF RANDOM NUMBERS
40Dr. Talal Skaik 2018
3. 7 GENERATION OF RANDOM NUMBERS
41Dr. Talal Skaik 2018
Random numbers that are distributed in a range (a,b) can be obtained by multiplying rand
by (b- a) and adding the product to a:
(b- a)*rand +a
For example, a vector of 10 elements with random values between -5 and 10 can be created
by (a= -5, b = 10):
3. 7 GENERATION OF RANDOM NUMBERS
42Dr. Talal Skaik 2018
The randi command: generates uniformly distributed random integer.
3. 7 GENERATION OF RANDOM NUMBERS
43Dr. Talal Skaik 2018
The range of the random integers can be set to be between any two integers by typing [imin imax] instead of imax. For example, a 3 x 4 matrix with random integers between 50 and 90 is created by:
3. 7 GENERATION OF RANDOM NUMBERS
44Dr. Talal Skaik 2018
The randn command: generates normally distributed numbers with mean 0 and standard deviation of 1.