Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Lecture 2

Uploaded by

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

Lecture 2

Uploaded by

Nawaz Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Computational Methods and Modeling for

Engineering Applications
(GENG-8030)

Department of Electrical and Computer Engineering,


University of Windsor, ON, Canada,

By:
Dr. Mohammad Sedigh Toulabi

Lecture number:
(2)

Fall 2023
Vectors:

To create a row vector, separate the elements by commas. Use square brackets.
For example,
>>p = [3,7,9]
p=
3 7 9

You can create a column vector by using the transpose notation (') or by
separating the elements by semicolons.
>>p = [3,7,9]'
p=
3
7
9

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Vectors:

You can create vectors by ''appending'' one vector to another.

For example, to create the row vector u whose first three columns contain the
values of r = [2,4,20] and whose fourth, fifth, and sixth columns contain the
values of w = [9,-6,3], you type u = [r,w]. The result is the vector u =
[2,4,20,9,-6,3].

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Vectors:

The colon operator (:) easily generates a large vector of regularly spaced
elements. Parentheses are not needed but can be used for clarity. Do not use
square brackets.
Typing
>>
creates a vector values with a spacing . The first value is . The last value is n
if is an integer multiple of . If not, the last value is less than .
For example, typing x = 0:2:8 creates the vector x = [0,2,4,6,8], whereas
typing x = 0:2:7 creates the vector x = [0,2,4,6].

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Vectors:

To create a row vector z consisting of the values from 7 to 8 in steps of 0.1,


type z = 7:0.1:8.

If the increment q is omitted, it is presumed to be 1. Thus typing y = -3:2


produces the vector y = [-3,-2,-1,0,1,2].

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Vectors:

The linspace command also creates a linearly spaced row vector, but instead
you specify the number of values rather than the increment.

The syntax is linspace(x1,x2,n), where x1 and x2 are the lower and upper
limits and n is the number of points.

For example, linspace(7,8,11) is equivalent to 7:0.1:8.

If n is omitted, the spacing is 100.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Vectors:

The logspace command creates an array of logarithmically spaced elements.

Its syntax is logspace(a,b,n), where n is the number of points between 10 a and


10b.

For example, x = logspace(-2,2,4) produces the vector x = [0.1000, 0.2154,


4.6416, 100.0000].

If n is omitted, the number of points defaults to 50.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Magnitude, length and absolute value of a vector:

Keep in mind the precise meaning of these terms when using MATLAB.

The length command gives the number of elements in the vector.

The magnitude of a vector x having elements x1, x2, …, xn is a scalar, given by


and is the same as the vector’s geometric length.

The absolute value of a vector x is a vector whose elements are the absolute
values of the elements of x.
For example, if
• its length is 3; (computed from )
• its magnitude is (computed by norm(x))
• its absolute value is (computed from ).

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Matrices:

A matrix has multiple rows and columns. For example, the matrix

M
has four rows and three columns.
Vectors are special cases of matrices having one row or one column.

If the matrix is small you can type it row by row, separating the elements in a
given row with spaces or commas and separating the rows with semicolons.
For example,

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Creating matrices from vectors:

Suppose a = [1,3,5] and b = [7,9,11] (row vectors). Note the difference


between the results given by [a,b] and [a;b] in the following session:
>>c = [a,b]
c=
1 3 5 7 9 11
>>c = [a;b]
c=
1 3 5
7 9 11

Remember, spaces or commas separate elements in different columns,


whereas semicolons separate elements in different rows.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Array addressing:

You need not use symbols to create a new array. For example, you can type
>> D = [[1,3,5];[7,9,11]];
The colon operator selects individual elements, rows, columns, or “subarrays”
of arrays. Here are some examples:
• D(:) represents all the row or column elements of the vector D.

• D(2:5) represents the second through fifth elements; that is D(2), D(3),
D(4), D(5).

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Array addressing:

D(:,3) denotes all the elements in the third column of the matrix D.
D(:,2:3) denotes all the elements in the second through fifth columns of D.
D(2:3,1:3) denotes all the elements in the second and third rows that are also
in the first through third columns.
D(end,:) denotes the last row in D, and D(:,end) denotes the last column.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Array addressing:

You can use array indices to extract a smaller array from another array. For
example, if you first create the array B

B=[2,4,10,13;16,3,7,1;8,4,9,25;3,12,15,17]

then type C = B(2:3,1:3), you can produce the following array:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Additional array functions:

Computes either the number of elements of A if A is a vector or


length(A) the largest value of m or n if A is an m n matrix.

Returns the algebraically largest element in A if A is a vector.


Returns a row vector containing the largest elements in each
max(A) column if A is a matrix.
If any of the elements are complex, max(A) returns the elements
that have the largest magnitudes.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Additional array functions:

size(A) Returns a row vector [m n] containing the sizes of the m n array A.


Sorts each column of the array A in ascending order and returns an
sort(A) array the same size as A.

Sums the elements in each column of the array A and returns a row
sum(A) vector containing the sums.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Question:
Q1-For the matrix A given below, the values retuned by max(A) functions is

a) Max=4+3i 4 (correct)

[ ]
24
b) Max= 1 4 𝐴= 13
c) Max=4i+3 4 3 𝑖+ 4 0
d) Max= 1 0

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Question:
Q2- For the matrix B, use MATLAB to find

a) (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Question:
Q3-For the matrix B, find the array that results from the operation

a) 4

b) 16

c) 2

d) 9 (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Workspace browser and variable editor:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Array addition and subtraction:

(1)
Array subtraction is performed in a similar way.
The addition shown in Equation 1 is performed in MATLAB as follows:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Multiplication:

Multiplying a matrix A by a scalar w produces a matrix whose elements are


the elements of A multiplied by w. For example:

This multiplication is performed in MATLAB as:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Multiplication:

Multiplication of an array by a scalar is easily defined and easily carried out.

However, multiplication of two arrays is not so straightforward.

MATLAB uses two definitions of multiplication:

(1) array multiplication (also called element-by-element multiplication), and

(2) matrix multiplication.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Element by element operations:

Symbol Operation Form Examples

Scalar-array addition A+B [6,3]+2=[8,5]

Scalar-array subtraction A-B [8,3]-5=[3,-2]

Array addition A+B [6,5]+[4,8]=[10,13]

Array subtraction A-B [6,5]-[4,8]=[2,-3]

Array multiplication A.*B [3,5].*[4,8]=[12,40]

Array right division A./B [2,5]./[4,8]=[2/4,5/8]

Array left division A.\B [2,5].\[4,8]=[2\4,5\8]

[3,5].^2=[3^2,5^2]
Array exponentiation A.^B 2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Question:
Q4- Given the vectors x = [ 6 5 10 12 ] and y = [ 3 9 8 1]

The product w = x.*y is:

a) 44 45 17 10

b) (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Question:
Q5- Given the vectors x = [ 6 5 10 12 ] and y = [ 3 9 8 1]

z = sqrt(y)+ exp(x) :

a) ]

b) ] (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Question:
Q6- Given the matrices

The array product w = A.*B and the array product z = B.*A are

a) (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
Question:
Q7- Given the vectors x = [ 6 15 10 ] and y = [ 3 19 8 ]
The array quotient w = x./y and z = y./x. are

a) 4

b) (correct)
c) 3

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Question:
Q8- Given the matrices and B
Use MATLAB to find C = A./B and E =
A.\B.

a)C = [1 2.1 1.3; 1.8 0.7 0.3] E=


[1.5 1.2 2.8; 1.3 0.6 0.8]

b)C = [1.3 2 1.1; 0.4 1.1 1.1] E=


[1.3 0.6 0.8 ; 0.5 1.8 0.8]

c)C = [-0.2 1.6 0.3; 1.3 1 1.4] E=


[0.3 1.1 0.3; 1.1 1.6 1.8]

d)C = [2 0.6 1.3; 0.8 1.8 1.3] E=


[0.5 1.8 0.8; 1.3 0.6 0.8] (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Element by element operations:

Array or Element-by-element multiplication is defined only for arrays having


the same size. The definition of the product x.*y, where x and y each have n
elements, is

x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]

if x and y are row vectors. For example, if

x = [2, 4, – 5] and y = [– 7, 3, – 8]

then z = x.*y gives

z = [2(– 7), 4 (3), –5(–8)] = [–14, 12, 40]

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Element by element operations:

If x and y are column vectors, the result of x.*y is a column vector. For
example z = (x’).*(y’) gives

Note that x’ is a column vector with size 3 1 and thus does not have the same
size as y, whose size is 1 3.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Element by element operations:

The array operations are performed between the elements in corresponding


locations in the arrays. For example, the array multiplication operation A.*B
results in a matrix C that has the same size as A and B and has the elements ci j
= ai j bi j . For example, if

A B

then C = A.*B gives this result:

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Element by element operations:

The built-in MATLAB functions such as sqrt(x) and exp(x) automatically


operate on array arguments to produce an array result the same size as the
array argument x.

Thus these functions are said to be vectorized functions.

For example, in the following session the result y has the same size as the
argument x.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Element by element operations:

However, when multiplying or dividing these functions, or when raising them to


a power, you must use element-by-element operations if the arguments are
arrays.

For example, to compute z = (ey sin x) cos2x, you must type

z = exp(y).*sin(x).*(cos(x)).^2.

You will get an error message if the size of x is not the same as the size of y. The
result z will have the same size as x and y.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
Array division:

The definition of array division is similar to the definition of array


multiplication except that the elements of one array are divided by the
elements of the other array. Both arrays must have the same size. The symbol
for array right division is ./. For example, if

x = [8, 12, 15] y = [–2, 6, 5]

then z = x./y gives


z = [8/(–2), 12/6, 15/5] = [–4, 2, 3]
or,

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 34
Array exponentiation:

MATLAB enables us not only to raise arrays to powers but also to raise
scalars and arrays to array powers.

To perform exponentiation on an element-by-element basis, we must use


the .^ symbol.

For example, if x = [3, 5, 8], then typing x.^3 produces the array [3 3, 53, 83] =
[27, 125, 512].

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 35
Question:
Q9- Given the matrices and
Their array product is

a) (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 36
Question:
Q10- Given the matrices
Use MATLAB to calculate B raised to the third power element by element. The
answer is

a) (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 37
Matrix-to-matrix multiplication:

In the product of two matrices AB, the number of columns in A must equal the
number of rows in B. The row-column multiplications form column vectors,
and these column vectors form the matrix result. The product AB has the same
number of rows as A and the same number of columns as B. For example,

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 38
Question:
Q11- Given the vectors
Use MATLAB to find the matrix product w = x*y and z = y*x.

a) , z=78

b) , z=

c) , z=61 (correct)

d) W= 78 , z

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 39
Question:
Q12- Use MATLAB to compute the dot product of the following vectors:

Definition of dot product


⃗ .⃗
𝑢 𝑤=𝑢 𝑖 .𝑤 𝑖 +𝑢 𝑗 . 𝑤 𝑗 +𝑢𝑘 .𝑤 𝑘
a) -6
b) 7
c) -10 (correct)
d) 12

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 40
Matrix-to-matrix multiplication:

Matrix multiplication does not have the commutative property; that is, in
general, AB BA. A simple example will demonstrate this fact:

AB
Whereas
BA

Reversing the order of matrix multiplication is a common and easily made


mistake.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 41
Special matrices:

Two exceptions to the non-commutative property are the null or zero matrix,
denoted by 0 and the identity, or unity, matrix, denoted by I.

The null matrix contains all zeros and is not the same as the empty matrix [ ],
which has no elements.

These matrices have the following properties:


0A A0 0
IA AI A
The identity matrix is a square matrix whose diagonal elements are all equal
to one, with the remaining elements equal to zero.

For example, the 2 2 identity matrix is

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 42
Special matrices:

The functions eye(n) and eye(size(A)) create an n n identity matrix and an


identity matrix the same size as the matrix A.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 43
Special matrices:

Sometimes we want to initialize a matrix to have all zero elements. The zeros
command creates a matrix of all zeros.

Typing zeros(n) creates an n n matrix of zeros, whereas typing zeros (m,n)


creates an m n matrix of zeros.

Typing zeros(size(A)) creates a matrix of all zeros having the same


dimension as the matrix A. This type of matrix can be useful for applications
in which we do not know the required dimension ahead of time.

The syntax of the ones command is the same, except that it creates arrays
filled with ones.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 44
Matrix left division and linear equation:

6x 12y 4z 70
7x 2y 3z 5
2x 8y 9z 64

So, the solution is x 3, y 5, and z 2

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 45
Question:
Q13- Use MATLAB to solve the following set of equations.

a) 0.41, -0.15 (correct)


b) -0.67, 0.71
c) 0.93, 0.17
d) 1.41, -2.43

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 46
Question:
Q14- Use MATLAB to solve the following set of equations.

a) 2.1, 3.1, -2.5


b) 1.4, -2.1, -1.5 (correct)
c) 1.2, -0.1, 0.5
d) 4.4, -8.1, 3.5

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 47
Polynomial coefficients:

The function poly(r) computes the coefficients of the polynomial whose roots
are specified by the vector r. The result is a row vector that contains the
polynomial’s coefficients arranged in descending order of power.

For example,

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 48
Polynomial coefficients:

The function roots(a)computes the roots of a polynomial specified by the


coefficient array a. The result is a column vector that contains the
polynomial’s roots.

For example, 2 x 2 +14 x +20=0

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 49
Question:
Q15- Use MATLAB to obtain the roots of

Use the poly function to confirm your answer.

a) -22.3 , -0.3 + 0.4i, -0.3 - 0.4i (correct)


b) -22.3 , 0.3 - 0.4i, 0.3 - 0.4i
c) -22.3 , -0.3 - 0.4i, 0.3 - 0.4i
d) -22.3 , 0.3 - 0.4i, -0.3 - 0.4i

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 50
Polynomial multiplication and division:

The function conv (a,b) computes the product of the two polynomials
described by the coefficient arrays a and b. The result is the coefficient array
of the product polynomial.

The function [q,r] = deconv (num,den) computes the result of dividing a


numerator polynomial, whose coefficient array is num, by a denominator
polynomial represented by the coefficient array den. The quotient polynomial
is given by the coefficient array q, and the remainder polynomial is given by
the coefficient array r.

3 2
(9 𝑥 − 5 𝑥 +3 𝑥 +7)× ¿

3 2
9 𝑥 −5 𝑥 + 3 𝑥 +7 − 0.5833 𝑥+ 8.1667
2
=1.5 𝑥 −0.5833 + 2
6 𝑥 − 𝑥 +2 6 𝑥 − 𝑥+ 2

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 51
Question:
Q16- Use MATLAB to calculate the quotient and the remainder of

a) (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 52
Plotting polynomial:

The function polyval(a,x)evaluates a polynomial at specified values of its


independent variable x, which can be a matrix or a vector. The polynomial’s
coefficients of descending powers are stored in the array a. The result is the
same size as x.
3 2
18 𝑥 −5 𝑥 − 2 𝑥 +3
2
𝑓 ( 𝑥 ) =6 𝑥 − 𝑥 +2 𝑔 ( 𝑥 )= 2
3 𝑥 +7 𝑥+ 4
𝑓 ( 2 )=?
𝑔 ( 2 )=?

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 53
Example of plotting a polynomial:

To plot the polynomial f (x) 9x3 5x2 3x 7 for 2 x 5, is as follows.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 54
Question:
Q18- Plot the polynomial

over the range −7 ≤ x ≤ 1.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 55

You might also like