Transcript ENG 1181

•
ENG 1181
Array Operations in MATLAB
Mathematical Operations with Arrays : Chapter 3
1. Types of Matrix arithmetic
2. Dot operators
College of Engineering
Engineering Education Innovation Center
1
•
ENG 1181
Types of Matrix Arithmetic
• There are two types of matrix arithmetic
• Matrix math is used in Linear Algebra to
solve simultaneous equations (MATLAB
text Ch. 3.2 and 3.3 - beyond the scope of
this course)
• We will only introduce element-by-element
arithmetic, e.g. If A = [a b c] and B = [x y z],
then
A + B = [a+x b+y c+z]
2
•
ENG 1181
Types of array arithmetic
If A and B are matrices and s is a scalar,
• There are only six cases where matrix math
and element-by-element arithmetic differ
A*B A/B A^B A^s s^A s/A
• For these cases, the standard operator has
been chosen to represent matrix arithmetic,
so we need a new symbol for element-by
element arithmetic (dot operators)
A.*B A./B A.^B A.^s s.^A s./A
3
•
ENG 118160-
Examples of Dot Operators
63
The vectors must be the same size.
Element-by-element operations for row vectors:
If:
a = [a1 a2 a3] and b = [b1 b2 b3]
Then: a .* b = [a1 * b1
a2* b2
a3* b3]
a ./ b = [a1 / b1
a2 / b2
a3 / b3]
a .^ b = [a1^b1
a2^b2
a.^2 = [a1^2
2./a = [2/a1
a2^2
2/a2
a3^b3]
a3^2]
2/a3]
4
•
ENG 118160-
Examples of Dot Operators
63
Element-by-element operations for matrices:
 A11

Given: A   A21
 A31
Then:
 A11B11
A . * B   A21B21
 A31B31
  A11 2

2
A .^ 2   A21 
 A 2
 31
A12
A22
A32
A12 B12
A22 B22
A32 B32
A13 
A23 
A33 
and
 B11
B   B21
 B31
A13 B13 
 A11/B11
A23 B23  A ./ B   A21/B21
 A31/B31
A33 B33 
 A12 2  A13 2 
2
2
 A22   A23  
 A32 2  A33 2 
B12
B22
B32
B13 
B23 
B33 
A12 /B12
A22 /B22
A32 /B32
A13/B13 
A23/B23 
A33/B33 
 6/A11 6/A12 6/A13 
6 ./ A  6/A21 6/A22 6/A23 
6/A31 6/A32 6/A33 
5
•
ENG 118160-
63
MATRIX ELEMENT-BY-ELEMENT EXAMPLES
>> A = [1, 2, 3; 4, 5, 6]
A=
1 2 3
4 5 6
>> A .* B
ans =
1
4
8 -10
>> B = [1, 2, -1; 2, -2, 5]
B=
1 2 -1
2 -2 5
>> A./ B
ans =
1.0000 1.0000 -3.0000
2.0000 -2.5000 1.2000
>> B .^ 3
ans =
1
8
-3
30
8
-8
-1
125
6
•
ENG 1181
Dot Operators
If you forget a dot, when one is required, you
will either get a cryptic error message
(involving matrix dimensions) or you will
get the wrong answer
For the following operations:
s+A, s-A, A-s, A+B, A-B, s*A, A/s
matrix math is the same as element-byelement arithmetic, so dot operators are
optional.
7
•
ENG 118152-
53
SCALAR-ARRAY
ADDITION AND SUBTRACTION
The scalar operates on each element in the array.
For example, given the following matrix M and the
scalar s = 2 we would get the following results for
addition and subtraction
>> M =
>> a = s + M
>> b = s - M
>> c = M - s
M=
a=
b=
c=
1
2
3
3
4
5
1
0
-1
-1
0
1
4
5
6
6
7
8
-2
-3
-4
2
3
4
7
8
9
9
10
11
-5
-6
-7
5
6
7
8
•
ENG 118152-
53
SCALAR-ARRAY
MULTIPLICATION AND DIVISION
the scalar operates on each element in the array
For example, given the same matrix M and scalar s
= 2 we would get the following results for
multiplication and division
>> M =
>> d = s * M
>> d = M / s
M=
d=
d=
1
2
3
2
4
6
0.5000
1.0000
1.5000
4
5
6
8
10
12
2.0000
2.5000
3.0000
7
8
9
14
16
18
3.5000
4.0000
4.5000
9
•
ARRAY-ARRAY
ADDITION AND SUBTRACTION
ENG 118152-
53
The arrays being used in the operation must have the
same size.
The sum (difference) of two arrays is obtained by
adding (subtracting) corresponding array elements,
resulting in a new array of the same size.
>> A = [1 2; 3 4]
A=
1 2
3 4
>> C = A + B
C=
2 1
5 4
>> B = [1 -1; 2 0]
B=
1 -1
2
0
>> A - B
ans =
0 3
1 4
10
•
ENG 118162-
CALCULATING THE VALUE
OF A FUNCTION
For the function:
63
z 3  5z
y 2
4 z  10
calculate y for z = 1, 3, 5, 7, 9, 11, 13
SOLUTION USING MATLAB:
>> z = [1: 2: 15]
Create a vector z with seven elements.
z=
Where do you have
operations between vectors
>> y = (z .^ 3 + 5 * z) ./ (4 * z .^ 2 - 10) that need a dot operator?
y=
-1.0000 1.6154 1.6667 2.0323 2.4650 2.9241 3.3964
1
3
5
7
9
11
13
11
•
ENG 1181
Element-by-Element
Calculations
• NOTE: Even though the calculation is
written only one time in the previous
example, MATLAB iterates through all
elements of z (Element-by-Element)
and calculates a corresponding number
of elements of solution array y.
12
•
ENG 118164-
65
SOME USEFUL BUILT-IN ARRAY FUNCTIONS
MATLAB has many built-in functions that can be used
with arrays. Some of them are (A is a vector):
max(A)
Returns the largest element in A
min(A)
Returns the smallest element in A
mean(A)
Returns the average value of the elements in A
sum(A)
Returns the sum of the elements of A
length(A)
Returns the number of elements in A
sort(A)
Sorts the elements of A
The Help window gives information
about many other functions.
13
•
ENG 1181
6465
EXAMPLES OF BUILT IN FUNCTIONS
>> A = [8 2 9 5 14 10]
>> sum(A)
A=
ans =
8
2
9
5
14
10
48
>> max(A)
>> length(A)
ans =
ans =
14
6
>> min(A)
>> sort(A)
ans =
ans =
2
2
5
8
9
10
14
>> mean(A)
ans =
8
14
•
ENG 118169
APPLICATION
Element-by-element calculations are useful in
processing data and in calculating the value of a
mathematical function at many points.
friction
F
m
EXAMPLE:
•The coefficient of friction μ is determined by measuring the force
F required to move a mass μ:
μ = F / (mg)
g = 9.81 m/s2
•The results from measuring F in five tests are given in the table.
•Determine the coefficient of friction in each test, and the average
from all tests.
Mass: m (kg)
2
4
5
10
20
50
Force: F (N)
12.5
23.2
30
61
116
294
15
•
ENG 1181
69
COEFFICIENT OF FRICTION USING MATLAB
Create the mass vector.
>> mass = [2 4 5 10 20 50];
>> force = [12.5 23.2 30 61 116 294];
Create the force vector
>> mu = force ./ (9.81 * mass) Calculate mu for each mass-force pair,
using element-by-element calculations.
mu =
0.6371
0.5912
0.6116
0.6218
0.5912
0.5994
>> mu_ave = mean(mu) Determine the average of the elements in
the vector mu by using the function mean().
meu_ave =
0.6087
16