computer programming eciv 2303 chapter 3 mathematical...

44
Computer Programming ECIV 2303 Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering Chapter 3 Mathematical Operations with Arrays 1 Dr. Talal Skaik 2018

Upload: others

Post on 24-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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

Page 2: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.1 ADDITION AND SUBTRACTION

In general, if A and B are two arrays (for example, 2 x 3 matrices),

2Dr. Talal Skaik 2018

Page 3: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.1 ADDITION AND SUBTRACTION

3Dr. Talal Skaik 2018

Page 4: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.1 ADDITION AND SUBTRACTION

4Dr. Talal Skaik 2018

Page 5: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 6: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 7: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.2 ARRAY MULTIPLICATION

7Dr. Talal Skaik 2018

A*B

Page 8: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.2 ARRAY MULTIPLICATION

8Dr. Talal Skaik 2018

Numerical Example

Page 9: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.2 ARRAY MULTIPLICATION

9Dr. Talal Skaik 2018

Page 10: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.2 ARRAY MULTIPLICATION

10Dr. Talal Skaik 2018

Page 11: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.2 ARRAY MULTIPLICATION

11Dr. Talal Skaik 2018

Page 12: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.2 ARRAY MULTIPLICATION

12Dr. Talal Skaik 2018

Page 13: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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

Page 14: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 15: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 16: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

Inverse of a Matrix

16Dr. Talal Skaik 2018

Page 17: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 18: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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:

Page 19: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 20: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 21: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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:

Page 22: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

Solving three linear equations (array division)

22Dr. Talal Skaik 2018

Page 23: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

Solving three linear equations (array division)

23Dr. Talal Skaik 2018

Page 24: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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:

Page 25: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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:

Page 26: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.4 ELEMENT-BY-ELEMENT OPERATIONS

26Dr. Talal Skaik 2018

Element-by-element exponentiation of matrix A gives:

Page 27: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.4 ELEMENT-BY-ELEMENT OPERATIONS

27Dr. Talal Skaik 2018

Page 28: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.4 ELEMENT-BY-ELEMENT OPERATIONS

28Dr. Talal Skaik 2018

Page 29: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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

Page 30: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.4 ELEMENT-BY-ELEMENT OPERATIONS

30Dr. Talal Skaik 2018

Page 31: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 32: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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:

Page 33: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS

33Dr. Talal Skaik 2018

Page 34: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS

34Dr. Talal Skaik 2018

Page 35: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS

35Dr. Talal Skaik 2018

Page 36: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS

36Dr. Talal Skaik 2018

Page 37: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

Inverse of a 2x2 Matrix

3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS

37Dr. Talal Skaik 2018

Example:

Page 38: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS

38Dr. Talal Skaik 2018

Inverse of a 3x3 Matrix:

Page 39: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.

Page 40: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3. 7 GENERATION OF RANDOM NUMBERS

40Dr. Talal Skaik 2018

Page 41: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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):

Page 42: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

3. 7 GENERATION OF RANDOM NUMBERS

42Dr. Talal Skaik 2018

The randi command: generates uniformly distributed random integer.

Page 43: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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:

Page 44: Computer Programming ECIV 2303 Chapter 3 Mathematical ...site.iugaza.edu.ps/tskaik/files/Chapter-3-MATLAB.pdf · 3.2 ARRAY MULTIPLICATION 6 Dr. Talal Skaik 2018 The operation A* B

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.