Lecture 2
Lecture 2
Engineering Applications
(GENG-8030)
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:
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:
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.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Vectors:
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 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:
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]
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Additional array functions:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Additional array functions:
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:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Multiplication:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Element by element operations:
[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]
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.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Element by element operations:
x = [2, 4, – 5] and y = [– 7, 3, – 8]
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:
A B
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Element by element operations:
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:
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:
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.
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:
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
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.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 42
Special matrices:
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.
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
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.
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.
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:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 49
Question:
Q15- Use MATLAB to obtain the roots of
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.
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:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 53
Example of plotting a polynomial:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 54
Question:
Q18- Plot the polynomial
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 55