Chapter 2
Chapter 2
1
Vectors 1
2
Vectors 2
3
Vectors 3
4
Vectors 4
5
Vectors 5
6
Vectors 6
7
Vectors 7
8
Magnitude, Length, and Absolute Value
of a Vector 1
9
Magnitude, Length, and Absolute Value
of a Vector 2
sqrt(x’*x)).
• its absolute value is [2,4,5] (computed from abs(x)).
10
Matrices
A matrix has multiple rows and columns. For example, the
matrix
2 4 10
16 3 7
M =
8 4 9
3 12 15
11
Creating Matrices
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, typing
>>A = [2,4,10;16,3,7];
12
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
>>D = [a;b]
D =
1 3 5
7 9 11
13
Array Addressing 1
14
Array Addressing 2
15
Array Addressing 3
16
Additional Array Functions 1
17
Additional Array Functions 2
18
Additional Array Functions 3
19
Additional Array Functions 4
20
Additional Array Functions 5
21
The Workspace Browser and Variable
Editor
Source: MATLAB 22
Multidimensional Arrays
They consist of two-dimensional matrices “layered”
to produce a third dimension. Each “layer” is called
a page.
23
Array Addition and Subtraction
For example:
6 −2 9 8 15 6
10 3 = −12 14 = −2 17 (1)
Array subtraction is performed in a similar way.
The addition shown in Equation 1 is performed in MATLAB
as follows:
>>A = [6,-2;10,3];
>>B = [9,8;-12,14]
>>A+B
ans =
15 6
-2 17
24
Multiplication 1
25
Multiplication 2
26
Division and exponentiation
Division and exponentiation must also be carefully
defined when you are dealing with operations
between two arrays.
MATLAB has two forms of arithmetic operations on
arrays. Next we introduce one form, called array
operations, which are also called element-by-element
operations. Then we will introduce matrix
operations. Each form has its own applications.
28
Element-by-element operations 2
29
Element-by-element operations 3
30
Element-by-element operations 4
31
Element-by-element operations 5
32
Element-by-element operations 6
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.
33
Array Division 1
34
Array Division 2
Also, if
24 20 −4 5
A= B=
− 9 4 3 2
24/(−4) 20/5 −6 4
C= =
− 9 /3 4/2 −3 2
35
Array Exponentiation 1
36
Matrix-Matrix Multiplication 1
>>A = [6,-2;10,3;4,7];
>>B = [9,8;-5,12];
>>A*B
ans =
64 24
75 116
1 116
38
Matrix-Matrix Multiplication 3
39
Special Matrices 1
The null matrix contains all zeros and is not the same as the
empty matrix [ ], which has no elements.
0A = A0 = 0
IA = AI = A
40
Special Matrices 2
1 0
I=
0 1
41
Special Matrices 3
42
Matrix Left Division and Linear Equations
6x + 12y + 4z = 70
7x − 2y + 3z = 5
2x + 8y − 9z = 64
>>A = [6,12,4;7,-2,3;2,8,-9];
>>B = [70;5;64];
>>Solution = A\B
Solution =
3
5
-2
The solution is x = 3, y = 5, and z = −2.
43
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,
44
Polynomial Roots
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,
45
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
two polynomials need not be the same degree. The result is the
coefficient array of the product polynomial.
46
Polynomial Multiplication and Division:
Examples
>>a = [9,-5,3,7];
>>b = [6,-1,2];
>>product = conv(a,b)
product =
54 -39 41 29 -1 14
>>[quotient, remainder] = deconv(a,b)
quotient =
1.5 -0.5833
remainder =
0 0 -0.5833 8.1667
47
Plotting Polynomials
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.
48
Example of Plotting a Polynomial
To plot the polynomial f (x) = 9 x3 − 5 x 2 + 3x + 7 for − 2 ≤ x
≤ 5, you type
>>a = [9,-5,3,7];
>>x = -2:0.01:5;
>>f = polyval(a,x);
>>plot(x,f),xlabel(’x’),ylabel(’f(x)’)
49
Cell Array Functions
Function Description
C = cell(n) Creates an n × n cell array C of
empty matrices.
C = cell(n,m) Creates an n × m cell array C of
empty matrices.
celldisp(C) Displays the contents of cell array C.
cellplot(C) Displays a graphical representation
of the cell array C.
50
© McGraw Hill LLC. All rights reserved. No reproduction or distribution without the prior written consent of McGraw Hill LLC.