ELEC 1018Y: MATLAB For Electrical Engineers
ELEC 1018Y: MATLAB For Electrical Engineers
ELEC 1018Y: MATLAB For Electrical Engineers
>>p = [3,7,9]'
p =
3
7
9
Dr S Z Sayed Hassen Dr S Z Sayed Hassen
You can also create a column vector by separating the You can create vectors by ''appending'' one vector to another.
elements by semicolons. For example,
For example, to create the row vector u whose first three
>>g = [3;7;9] columns contain the values of r = [2,4,20] and whose
g = fourth, fifth, and sixth columns contain the values of w =
3 [9,-6,3], you type u = [r,w]. The result is the vector u =
7 [2,4,20,9,-6,3].
9
The colon operator (:) easily generates a large vector of For example, typing x = 0:2:8 creates the vector x
regularly spaced elements. Parentheses are not needed but = [0,2,4,6,8], whereas typing x = 0:2:7
can be used for clarity. Do not use square brackets. creates the vector x = [0,2,4,6].
Dr S Z Sayed Hassen 1
ELEC 1018Y
The linspace command also creates a linearly spaced row The logspace command creates an array of
vector, but instead you specify the number of values rather logarithmically spaced elements.
than the increment.
Its syntax is logspace(a,b,n), where n is the
The syntax is linspace(x1,x2,n), where x1 and x2 are number of points between 10a and 10b.
the lower and upper limits and n is the number of points.
For example, x = logspace(-1,1,4)
For example, linspace(5,8,31) is equivalent to produces the vector x = [0.1000, 0.4642,
5:0.1:8. 2.1544, 10.000].
Dr S Z Sayed Hassen
The length command gives the number of elements in the its magnitude is [22 + (–4)2 + 52] = 6.7082;
(computed from sqrt(x*x’))
vector.
The magnitude of a vector x having elements x1, x2, …, xn is its absolute value is [2,4,5] (computed
from abs(x)).
a scalar, given by x12 + x22 + … + xn2), and is the same as
the vector's geometric length.
Creating Matrices
Matrices If the matrix is small you can type it row by row, separating the
A matrix has multiple rows and columns. For elements in a given row with spaces or commas and separating
example, the matrix the rows with semicolons. For example, typing
2 4 10 >>A = [2,4,10;16,3,7];
M = 16 3 7
8 4 9 creates the following matrix:
3 12 15
2 4 10
A=
has four rows and three columns. 16 3 7
Vectors are special cases of matrices having one Remember, spaces or commas separate elements in different
row or one column. columns, whereas semicolons separate elements in different
rows.
Dr S Z Sayed Hassen
Dr S Z Sayed Hassen 2
ELEC 1018Y
Creating Matrices from Vectors You need not use symbols to create a new array. For
example, you can type
Suppose a = [1,3,5] and b = [7,9,11] (row vectors).
Note the difference between the results given by [a b] and >> D = [[1,3,5];[7,9,11]];
[a;b] in the following session:
Array Addressing
>>c = [a b];
The colon operator selects individual elements, rows,
c = columns, or ''subarrays'' of arrays. Here are some
1 3 5 7 9 11 examples:
>>D = [a;b]
D = ■ v(:) represents all the row or column elements of
the vector v.
1 3 5
7 9 11 ■ v(2:5) represents the second through fifth elements;
that is v(2), v(3), v(4), v(5).
Dr S Z Sayed Hassen
You can use array indices to extract a smaller array from another
Array Addressing, continued array. For example, if you first create the array B
[u,v,w] = find(A) Computes the arrays u and v, max(A) Returns the algebraically
containing the row and column largest element in A if A is a
indices of the nonzero elements of vector.
the matrix A, and the array w,
containing the values of the nonzero Returns a row vector
elements. The array w may be containing the largest
omitted. elements in each column if
A is a matrix.
length(A) Computes either the number of If any of the elements are
elements of A if A is a vector or the complex, max(A) returns
largest value of m or n if A is an m × the elements that have the
n matrix. largest magnitudes.
Dr S Z Sayed Hassen 3
ELEC 1018Y
Multidimensional Arrays
The function size(A) returns a row vector [m n] containing
the sizes of the m × n array A. The length(A) function Consist of two-dimensional matrices “layered” to produce a third
computes either the number of elements of A if A is a vector dimension. Each “layer” is called a page.
or the largest value of m or n if A is an m × n matrix.
Dr S Z Sayed Hassen 4
ELEC 1018Y
Multiplication of an array by a scalar is easily defined and Division and exponentiation must also be
easily carried out. carefully defined when you are dealing with
operations between two arrays.
However, multiplication of two arrays is not so
straightforward. MATLAB has two forms of arithmetic operations
on arrays. Next we introduce one form, called
MATLAB uses two definitions of multiplication: array operations, which are also called
element-by-element operations. Then we will
(1) array multiplication (also called element-by-element introduce matrix operations. Each form has its
multiplication), and own applications.
Division and exponentiation must also be
(2) matrix multiplication. carefully defined when you are dealing with
operations between two arrays.
Dr S Z Sayed Hassen
Element-by-element operations
Array or Element-by-element multiplication is defined only for
Symbol Operation Form Examples
arrays having the same size. The definition of the product x.*y,
+ Scalar-array addition A + b [6,3]+2=[8,5] where x and y each have n elements, is
- Scalar-array subtraction A – b [8,3]-5=[3,-2]
x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]
+ Array addition A + B [6,5]+[4,8]=[10,13]
[3,5].^[2,4]=[3^2,5^4]
If x and y are column vectors, the result of x.*y is a column The array operations are performed between the elements in
vector. For example z = (x’).*(y’) gives 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
2(–7) –14
z = 4(3) = 12 example, if
–5(–8) 40
A= 11 5 B= –7 8
Note that x’ is a column vector with size 3 × 1 and thus –9 4 6 2
does not have the same size as y, whose size is 1 × 3.
then C = A.*B gives this result:
Thus for the vectors x and y the operations x’.*y and
y.*x’ are not defined in MATLAB and will generate an error
11(–7) 5(8) –77 40
message. C=
–9(6) 4(2)
=
–54 8
Dr S Z Sayed Hassen 5
ELEC 1018Y
The built-in MATLAB functions such as sqrt(x) and However, when multiplying or dividing these
exp(x) automatically operate on array arguments to functions, or when raising them to a power, you
produce an array result the same size as the array argument must use element-by-element operations if the
x. arguments are arrays.
Thus these functions are said to be vectorized functions. For example, to compute z = (ey sin x) cos2x, you
must type
For example, in the following session the result y has the
same size as the argument x. z = exp(y).*sin(x).*(cos(x)).^2.
>>x = [4, 16, 25]; You will get an error message if the size of x is not
>>y = sqrt(x) the same as the size of y. The result z will have
y = the same size as x and y.
2 4 5
Dr S Z Sayed Hassen
Array Division
Also, if
The definition of array division is similar to the definition of
24 20 –4 5
A= B=
array multiplication except that the elements of one array –9 4 3 2
are divided by the elements of the other array. Both arrays
must have the same size. The symbol for array right division then C = A./B gives
is ./. For example, if
C= 24/(–4) 20/5 = –6 4
–9/3 4/2 –3 2
x = [8, 12, 15] y = [–2, 6, 5]
Dr S Z Sayed Hassen
Dr S Z Sayed Hassen 6
ELEC 1018Y
0A = A0 = 0
Reversing the order of matrix multiplication is a common
and easily made mistake. IA = AI = A
Dr S Z Sayed Hassen
Dr S Z Sayed Hassen 7
ELEC 1018Y
6x + 12y + 4z = 70
7x – 2y + 3z = 5 The function poly(r)computes the coefficients of the
2x + 8y – 9z = 64 polynomial whose roots are specified by the vector r. The
result is a row vector that contains the polynomial’s
>>A = [6,12,4;7,-2,3;2,8,-9]; coefficients arranged in descending order of power.
>>B = [70;5;64];
>>Solution = A\B
Solution = For example,
3
5 >>c = poly([-2, -5])
-2 c =
The solution is x = 3, y = 5, and z = –2. 1 7 10
>>a = [9,-5,3,7];
>>b = [6,-1,2]; The function polyval(a,x)evaluates a polynomial at specified
>>product = conv(a,b) values of its independent variable x, which can be a matrix or
product =
a vector. The polynomial’s coefficients of descending powers
54 -39 41 29 -1 14 are stored in the array a. The result is the same size as x.
>>[quotient, remainder] = deconv(a,b)
quotient =
1.5 -0.5833
remainder =
0 0 -0.5833 8.1667
Dr S Z Sayed Hassen 8
ELEC 1018Y
>>a = [9,-5,3,7];
>>x = -2:0.01:5;
>>f = polyval(a,x);
>>plot(x,f),xlabel(’x’),ylabel(’f(x)’)
Dr S Z Sayed Hassen 9