MATLAB Array and Matrix Operations
Cheng-Liang Chen
PSE
LABORATORY
Department of Chemical Engineering National TAIWAN University
Chen CL
1
Matrix Subscript and Index
Chen CL
1
Matrix Subscript and Index
Chen CL
2
Subscript and Index 1st col 1st row 2nd row
A =
3rd row 4th row 5th row
A(i, j ) = A A((i + ( j ( j A(2 (2,, 3) = A A(12) (12)
4
1
8
2
7
3
0
4
2
5
− 1) 1)m m)
2nd col
5
6
2
7
5
8
3
9
3
10
3rd col
1
11
9
12
7
13
4
14
1
15
4th col
6
16
4
17
1
18
5
19
0
20
(stored (sto red as 1D vecto vector, r, A : : m m
A(4 : 5, 5, 2 : 3) = A = A([ ([99 14; 10 15]) 15]) A(1 : 5, 5, 5) = A A(: (:,, 5) = A A(21 (21 : 25) = A(1 : end end,, end end)) = A A(: (:,, end end)) = A A(21 (21 : end : end))
5th col
2
21
7
22
5
23
4
24
3
25
× n matrix)
Chen CL
3
Subscript and Index 1st col 1st row 2nd row
A =
3rd row 4th row 5th row
A(i, j) = A(i + ( j A(2, 3) = A(12)
4
1
8
2
7
3
0
4
2
5
− 1)m)
2nd col
5
6
2
7
5
8
3
9
3
10
3rd col
1
11
9
12
7
13
4
14
1
15
4th col
6
16
4
17
1
18
5
19
0
20
(stored as 1D vector, A : m
A(4 : 5, 2 : 3) = A([9 14; 10 15]) A(1 : 5, 5) = A(:, 5) = A(21 : 25) = A(1 : end, end) = A(:, end) = A(21 : end)
5th col
2
21
7
22
5
23
4
24
3
25
× n matrix)
Chen CL
4
Subscript and Index 1st col 1st row 2nd row
A =
3rd row 4th row 5th row
A(i, j) = A(i + ( j A(2, 3) = A(12)
4
1
8
2
7
3
0
4
2
5
− 1)m)
2nd col
5
6
2
7
5
8
3
9
3
10
3rd col
1
11
9
12
7
13
4
14
1
15
4th col
6
16
4
17
1
18
5
19
0
20
(stored as 1D vector, A : m
A(4 : 5, 2 : 3) = A([9 14; 10 15]) A(1 : 5, 5) = A(:, 5) = A(21 : 25) = A(1 : end, end) = A(:, end) = A(21 : end)
5th col
2
21
7
22
5
23
4
24
3
25
× n matrix)
Chen CL
5
Subscript and Index 1st col 1st row 2nd row
A =
3rd row 4th row 5th row
A(i, j) = A(i + ( j A(2, 3) = A(12)
4
1
8
2
7
3
0
4
2
5
− 1)m)
2nd col
5
6
2
7
5
8
3
9
3
10
3rd col
1
11
9
12
7
13
4
14
1
15
4th col
6
16
4
17
1
18
5
19
0
20
(stored as 1D vector, A : m
A(4 : 5, 2 : 3) = A([9 14; 10 15]) A(1 : 5, 5) = A(:, 5) = A(21 : 25) = A(1 : end, end) = A(:, end) = A(21 : end)
5th col
2
21
7
22
5
23
4
24
3
25
× n matrix)
Chen CL
6
Subscript and Index 1st col 1st row 2nd row
A =
3rd row 4th row 5th row
A(i, j) = A(i + ( j A(2, 3) = A(12)
4
1
8
2
7
3
0
4
2
5
− 1)m)
2nd col
5
6
2
7
5
8
3
9
3
10
3rd col
1
11
9
12
7
13
4
14
1
15
4th col
6
16
4
17
1
18
5
19
0
20
(stored as 1D vector, A : m
A(4 : 5, 2 : 3) = A([9 14; 10 15]) A(1 : 5, 5) = A(:, 5) = A(21 : 25) = A(1 : end, end) = A(:, end) = A(21 : end)
5th col
2
21
7
22
5
23
4
24
3
25
× n matrix)
Chen CL
7
Subscript and Index 1st col 1st row 2nd row
A =
3rd row 4th row 5th row
A(i, j) = A(i + ( j A(2, 3) = A(12)
4
1
8
2
7
3
0
4
2
5
− 1)m)
2nd col
5
6
2
7
5
8
3
9
3
10
3rd col
1
11
9
12
7
13
4
14
1
15
4th col
6
16
4
17
1
18
5
19
0
20
(stored as 1D vector, A : m
A(4 : 5, 2 : 3) = A([9 14; 10 15]) A(1 : 5, 5) = A(:, 5) = A(21 : 25) = A(1 : end, end) = A(:, end) = A(21 : end)
5th col
2
21
7
22
5
23
4
24
3
25
× n matrix)
Chen CL
8
One-Dimensional Array: Vector
>> g = [3, 7, 9]’ g = 3 7 9 >> g = [3; 7; 9] g = 3 7 9
>> g = [3 7 9] g = 3 7 9 >> r = [2, 4, 20]; >> w = [9, -6, 3]; >> u = [r, w] u = 2
4
20
9
-6
3
Chen CL
9
>> x = [0: 2: 8]
>> x = [8: -2: 0]
x = 0
x = 8
2
4
6
8
6
4
2
>> x = [0: 2: 9]
>> x = [0: 4]
x = 0
x = 0
2
4
6
8
1
2
>> x = linspace(0,8,5) x = 0
2
4
6
8
>> x = logspace(-1, 1, 4) x = 0.1000 0.4642 2.1544 10.000 % 4 equally-spaced pts 10^{-1}-10^{1}
3
0
4
Chen CL
10
Two-Dimensional Arrays: Matrix A = [2, 4, 10; 16, 3, 7] c = [ a b ] A = 2 16
4 3
10 7
a = [1, 3, 5] a = 1
3
5
c = 1
3
5
7
9
11
d = [a; b] d = 1 7
3 9
5 11
b = [7, 9, 11]
d = [[1, 3, 5]; [7, 9, 11]]
b = 7
d = 1 7
9
11
3 9
5 11
Chen CL
11
Array Addressing >> B = [ 2, 4,10,13; 16, 3, 7,18; 8, 4, 9,25; 3,12,15,17] B = 2 4 10 13 16 3 7 18 8 4 9 25 3 12 15 17 >> C = B(2:3, 1:3) C = 16 3 7 8 4 9 >> B(3,:) = [] B = 2 4 10 13 16 3 7 18 3 12 15 17
>> B([1 4],2:3) = 5 B = 2 5 5 13 16 3 7 18 3 12 15 17 0 5 5 0 >> B(2,6) = 6 B = 2 5 5 13 0 16 3 7 18 0 3 12 15 17 0 0 5 5 0 0
>> A = B(:,3:-1:1) A = 5 5 2 7 3 16 15 12 3 5 5 0 >> D = A([2,2,2],:) D = 7 3 16 7 3 16 7 3 16 0 6 0 0
Chen CL
12
Using clear to Avoid Errors >> A = [2, 5; 6, 9]; >> x = [1:5]’; >> A(:, 1) = x ----> error (row numbers in A and x must be the same !)
>> A = [2, 5; 6, 9]; >> x = [1:5]’; >> clear A >> A(:, 1) = x A = 1 2 3 4 5
Chen CL
13
Some Useful Array Functions A x z A
x y z k
= [6,2; -10,-5; 3,0] = max(A), y = min(A), ... = size(A),k = length(A) = 6 2 -10 -5 3 0 = % max value of 6 2 % each column = % min value of -10 -5 % each column = 3 2 = 3
A x z A
= [6,2; -10,-5; 3+4i,0] = max(A), y = min(A), ... = size(A),k = length(A) = 6.0000 2.0000 -10.0000 -5.0000 3.0000 + 4.0000i 0 x = % max magnitude -10 -5 y = % min magnitude 3.0000+4.0000i 0 z = 3 2 k = 3
Chen CL
14
Command
Description
cat(n,A,B,C,...)
Creates a new array by concatenating A,B,... along dimension n
find(x) [u,v,w]=find(A)
Computes an array containing indices of nonzero elements of array x Computes arrays u,v containing row and column indices of nonzero elements of matrix A, and the array w containing values of nonzero elements. (w may be omitted)
length(A)
Computes either the number of elements of A if A is a vector or the largest value of m or n if A is an
m × n matrix
linspace(a,b,n)
Creates a row vector of n equally spaced values between a and b
logspace(a,b,n)
Creates a row vector of n logarithmically spaced values between a and b Returns a row vector containing largest element in each column of A
max(A)
[x,k] = max(A) Stores max(A) in x and indices in k min(A) Same as max(A) but returns minimum values [x,k] = min(A) Stores min(A) in x and indices in k size(A) sort(A)
Returns a row vector [m n] containing the sizes of the m
× n array
sum(A)
Sums the elements in each column of array A and returns a row vector containing the sums
Sorts each column of array A in ascending order and returns an array the same size as A
Chen CL
15
Test Your Understanding T2.1-1 For the matrix B , find the array that results from the operation [B; B ]. Use MATLAB to determine what number is in row 5, column 3 of the result.
B=
2
4
10 13
16
3
7
18
8
4
9
25
3
12 15 17
T2.1-2 For the same matrix B , use MATLAB to (a) find the largest and smallest element in B and their indices and (b) sort each column in B to create a new matrix C .
Chen CL
16
Multi-dimensional Arrays A = [4, 6, 1; 5, 8, 0; 3, 9, 2]; A(:,:,2) = [6, 2, 9; 0, 3, 1; 4, 7, 5] A(:,:,1) = 4 6 1 5 8 0 3 9 2 A(:,:,2) = 6 2 9 0 3 1 4 7 5
A = [8, 2; 9, 5];% 2x2 B = [4, 6; 7, 3];% 2x2 C = cat(1,A,B); %C=[A;B] D = cat(2,A,B); %D=[A,B] E = cat(3,A,B); %E(:,:,1)=A C, D, E %E(:,:,2)=B C = 8 2 9 5 4 6 7 3 D = 8 2 4 6 9 5 7 3 E(:,:,1) = 8 2 9 5 E(:,:,2) = 4 6
Chen CL
17
Array (Element-by-Element) Operations Symbol
Operation
Form
Example
+
Scalar-array addition Scalar-array subtraction
A+b
[6,3]+ 2
=[8, 5]
A-b
[6,3]- 2
=[4, 1]
Array addition Array subtraction
A+B [6,3]+[3,8] =[9,11]
Array multiplication Array right division Array left division Array exponent
A.*B [3,2].*[2,4]=[6, 8]
+ .* ./ .\ .^
A-B [6,3]-[3,8] =[3,-5] A./B [4,8]./[2,4]=[2, 2] A.\B [2,4].\[4,8]=[2, 2] A.^B [3,5].^2
=[9,25]
2.^[3,5]
=[8,32]
[3,2].^[2,3]=[9, 8]
Chen CL
18
Example: Vectors and Relative Velocity A train is heading east at 60 miles per hour. A car approaches the track crossing at 45 miles per hour on a road that makes a 55o angle with the track. What is the velocity of the train relative to the car ? What is the speed of the train relative to the car ? Solution: The train’s velocity v R relative to the car is the difference between the train’s velocity relative to the ground v T and the car’s velocity relative to the ground v C v R = v T
− v
C
Choosing the x direction to be east, and y north, v T v C
= 60i + 0 j = 45 cos(55o)i + 45 sin(55o) j = 25.8109i + 36.8618 j
v R =
v T
− v
C
= 34.1891i sR =
− 36.8618 j (miles/hour) (south-east) (34.1891)2 + (−36.8618)2 = 50.2761 (miles/hour)
Chen CL
19
v_T = [60, 0]; v_C = [45*cos(55*pi/180), 45*sin(55*pi/180)]; v_R = v_T - v_C; s_R = sqrt(v_R(1)^2+v_R(2)^2); v_T, v_C, v_R, s_R v_T = 60
0
v_C = 25.8109
36.8618
v_R = 34.1891
-36.8618
s_R = 50.2761
Chen CL
20
Example: Vectors and Displacement Suppose two divers start at the surface and establish the following coordinate system: x is to the west, y is to the north, and z is down. Diver 1 swims 55 feet west, 36 feet north, and then dive 25 feet. Diver 2 dives 15 feet, then swims east 20 feet and then north 59 feet. 1. Find the distance between diver 1 and the starting point. 2. How far in each direction must diver 1 swim to reach diver 2 ? 3. How far in a straight line must diver 1 swim to reach diver 2
Solution: v1 = diver 1: diver 2: v2 = dist 10: dist10 = v 12
55i + 36 j + 25k 20i + 59 j + 15k 552 + 362 + 252
−√
= v2 v1 = 75i + 23 j
dist 12: dist12 =
−
−
−
− 10k
= 70.3278
( 75)2 + 232 + ( 10)2 = 79.0822
−
Chen CL
21
v_10 = [ 55, 36, 25]; v_20 = [-20, 59, 15]; v_12 = v_20 - v_10; ss_10 = 0; ss_12 = 0; for k = 1:3 ss_10 = ss_10 + v_10(k)^2; ss_12 = ss_12 + v_12(k)^2; end dist_10 = sqrt(ss_10); dist_12 = sqrt(ss_12); v_12, dist_10, dist_12 v_12 = -75
23
-10
dist_10 = 70.3278 dist_12 = 79.0822
v_10 = [ 55, 36, 25]; v_20 = [-20, 59, 15]; v_12 = v_20 - v_10; dist_10 = sqrt(sum(v_10.*v_10)); dist_12 = sqrt(sum(v_12.*v_12)); v_12, dist_10, dist_12
Chen CL
22
Example: Aortic Pressure Model Biomedical engineers often design instrumentation to measure physiological processes, such as blood pressure. To do this they must develop mathematical models of the process. The following equation is a specific case of one model used to describe the blood pressure in the aorta during systole (the period following the closure of the heart’s aortic valve). The variable t represents time in seconds, and the dimentionless variable y represents the pressure difference across the aortic valve, normalized by a constant reference pressure. Plot this function for t 0.
≥
π y(t) = e −8t sin 9.7t + 2
Solution:
Frequency = 9.7 radians/second = 9.7/(2π) = 1.5 cycles/second Period = 1/1.5 = 2/3 second select a spacing of 0.003 to give approximately 200 points per period
⇒ ⇒
t = 0 e0 = 1 t = 0.5 e8(0.5) = 0.02 select upper limit of t to be 0.5 second
⇒
⇒ ⇒
Chen CL
23
t = [0 : 0.1 : 0.5]; p y_1 = exp(-8*t); y_2 = sin(9.7*t+pi/2); y = y_1.*y_2; p = [t; y_1; y_2; y]
= 0 1.0000 1.0000 1.0000
0.1000 0.2000 0.3000 0.4000 0.4493 0.2019 0.0907 0.0408 0.5653 -0.3609 -0.9733 -0.7395 0.2540 -0.0729 -0.0883 -0.0301
t = [0 : 0.003 : 0.5]; y = exp(-8*t).*sin(9.7*t+pi/2); plot(t,y), xlabel(’t (sec)’),... ylabel(’Normalized Pressure Difference y(t)’) 1.2
1
) t ( y e c n
0.8
e r e f f i D e r u s s e r P d e z i l a m r o N
0.6
0.4
0.2
0
−0.2 0
0.05
0.1
0.15
0.2
0.25 t (sec)
0.3
0.35
0.4
0.45
0.5
0.5000 0.0183 0.1372 0.0025
Chen CL
24
Example: Transportation Route Analysis The following table gives data for the distance traveled along five truck routes and the corresponding time required to traverse each route. Use the data to compute the average speed required to drive each route. Find the route that has the highest average speed.
Distance (miles) Time (hours) >> >> >> >>
1
2
3
4
5
560 10.3
440 8.2
490 9.1
530 10.1
370 7.5
d = [ 560, 440, 490, 530, 370]; t = [10.3, 8.2, 9.1, 10.1, 7.5]; speed = d./t [highest_speed, route] = max(speed)
speed = 54.3689 53.6585 highest_speed = 54.3689 route = 1
53.8462
52.4752
49.3333
Chen CL
25
Example: Current and Power Dissipation The current i passing through an electrical resistor having a voltage v across it is given by Ohm’s law: i = v/R, where R is the resistance. The power dissipated in the resistor is given by v 2/R. The following table gives data for the resistance and voltage for five resistors. Use the data to compute (a) the current in each resistor and (b) the power dissipated in each resistor. 1
2
3
R (ohms)
104
2
v (volts)
120
80
× 104
3.5 110
× 104
4
5
105
2
200
350
>> R = [10000, 20000, 3500, 100000, 200000]; >> v = [ 120, 80, 110, 200, 350]; >> current = v./R current = 0.0120
0.0040
0.0314
0.0020
0.0018
3.4571
0.4000
0.6125
>> power = v.^2./R power = 1.4400
0.3200
× 105
Chen CL
26
Example: A Batch Distillation Process Chemical and environmental engineers must sometimes design batch processes for producing or purifying liquids and gases. Applications of such processes occur in food and medicine production, and in waste processing and water purification. An example of such a process is a system for heating a liquid benzene/toluene solution to distill a pure benzene vapor. A particular batch distillation unit is charged initially with 100 mol of a 60% mol benzene/40% mol toluene mixture. Let L (mol) be the amount of liquid remaining in the still, and let x (mol B/mol) be the benzene mole fraction in the remaining liquid. Conservation of mass for benzene and toluene can be applied to derive the following relation (Felder, 1986).
−
x L = 100 0.6
0.625
1
x 0.4
1 625
− .
Determine what mole fraction of benzene remains when L = 70. Note that it is difficult to solve this equation directly for x. Use a plot of x versus L to solve the problem.
Chen CL
>> >> >> >> >>
27
x = [0 : .001 : 0.6]; L = 100*(x/0.6).^(0.625).*((1-x)/0.4).^(-1.625); plot(L,x), grid, xlabel(’L (mol)’), ylabel(’x (mol B/mol)’),... set(gca, ’xtick’, [0 10 20 30 40 50 60 70 80 90 100]); [L,x] = ginput(1) 0.7
0.6
0.5
L = 69.9616 x = 0.5271
) l o 0.4 m / B l o m ( x 0.3
0.2
0.1
0
0
10
20
30
40
50 L (mol)
60
70
80
90
100
Chen CL
28
Example: Height versus Velocity In introductory physics courses Newton’s laws of motion are used to derive the following formula for the maximum height h achieved by an object thrown with a speed v at an angle θ to the horizon. v 2 sin2 θ h = 2g Create a table showing the maximum height for the following values of v and θ: v = 10, 12, 14, 16, 18, 20 meters/sec θ = 50o, 60o, 70o, 80o
Chen CL
29
g = 9.8; v = [10:2:20]; th = vel = []; for k = 1:length(th) vel = [vel, v’]; end theta = []; for k = 1:length(v) theta = [theta;thr]; end h = vel.^2.*sin(theta).^2/(2*g); H = [v’, h]; table = [0, th; H] table = 0 10.0000 12.0000 14.0000 16.0000 18.0000 20.0000
Comparison:
50.0000 2.9940 4.3114 5.8682 7.6646 9.7006 11.9760
60.0000 3.8265 5.5102 7.5000 9.7959 12.3980 15.3061
[50:10:80];
thr = th*(pi/180);
% [v’ v’ v’ v’]
% thr % ... % thr
% table = [0 70.0000 4.5052 6.4875 8.8302 11.5334 14.5969 18.0209
th; v’
80.0000 4.9482 7.1254 9.6985 12.6674 16.0322 19.7928
h]
Chen CL
30
g = 9.8; v = [10: 2: 20]; g = 9.8; th = [50:10:80]; v = [10: 2: 20]; thr = th*(pi/180); th = [50:10:80]; [theta vel] = meshgrid(thr, v); thr = th*(pi/180); h = vel.^2.*sin(theta).^2/(2*g); vel = []; table = [0 th; v’ h] for k = 1:length(th) g = 9.8; vel = [vel, v’]; v = [10: 2: 20]; end th = [50:10:80]; theta = []; thr = th*(pi/180); for k = 1:length(v) for i = 1:length(v) theta = [theta;thr]; for j = 1:length(thr) end h(i,j) = v(i)^2*sin(thr(j))^2/(2* h = vel.^2.*sin(theta).^2/(2*g); end table = [0 th; v’ h] end table = [0 th; v’ h]
Chen CL
31
Test Your Understanding T2.3-1 Given the matrices A =
− 21
18
27 8
B =
− 21
18
27 8
find their (a) array product, (b) array right division (A divided by B ), and (c) B raised to the third power element by element. (ANS: (a) [−147, −81, −162, 32], (b) [−3, −9, −2, 2], (c) [−343, −27; 729, 64])
Chen CL
32
Matrix Operations = [1 0 0] j = [ 0 1 0 ] k = [ 0 0 1 ] i i = j j = k k = 1 i j = j k = k i = 0 u = u1i + u2 j + u3k w = w1i + w2 j + w3k u w = (u1i + u2 j + u3k) (w1i + w2 j + w3k) i
· ·
· ·
· ·
·
·
= u1w1 + u2w2 + u3w3 =
a11 a21
a11 a21 a12 a22
a12 a22 b11 b21
x1 x2 b12 b22
= =
u 1 u 2 u3
w1 w2 w3
a11x1 + a12x2 a21x1 + a22x2 a11b11 + a12b21 a21b11 + a22b21
a11b12 + a12b22 a21b12 + a22b22
Chen CL
33
Test Your Understanding T2.4-1 Use MATLAB to compute the dot product of the following vectors: u = 6i − 8 j + 3k w = 5i + 3 j
− 4k
Check your answer by hand. (ANS: -6)
T2.4-1 Use MATLAB to show that
−
7
4
3 2
5
9
1 8 7 6
35
= 11 68
80
−12 94
Chen CL
34
Example: Manufacturing Cost Analysis The following table shows the hourly cost of four types of manufacturing processes. It also shows the number of hours required of each process to produce three different products. (a) Determine the cost of each process to produce one unit of product 1. (b) Determine the cost to make one unit of each product. (c) Suppose we produce 10 units of product 1, 5 units of product 2, and 7 units of product 3. Compute the total cost. Hours required to produce one unit Product 1 Product 2 Product 3
Process
Hourly cost ($)
Lathe Grinding
10 12
6 2
5 3
4 1
Milling Welding
14 9
3 4
2 0
5 3
Chen CL
>> >> >> >> >>
35
hourly_cost = [10, 12, 14, 9]; hours_1 = [6, 2, 3, 4]; hours_2 = [5, 3, 2, 0]; hours_3 = [4, 1, 5, 3]; process_cost_1 = hourly_cost.*hours_1
process_cost_1 = 60 24 42
36
>> unit_cost = hourly_cost*[hours_1’, hours_2’, hours_3’] unit_cost = 162 114
149
>> units = [10, 5, 7]; >> total_cost = units*unit_cost’ total_cost = 3233
Chen CL
36
Example: Product Cost Analysis The following tables show the costs associated with a certain product, and the production volume for the four quarters of the business year. Find the quarterly costs for materials, labors, and transportation; the total material, labor, and transportation costs for the year; and the total quarterly costs. Unit Costs ($ 103) Materials Labor Transportation
×
Product 1 2 3 4 Product
6 2 4 9
2 5 3 7
1 4 2 3
Quarter 1 Quarter 2 Quarter 3 Quarter 4
1
10
12
13
15
2 3
8 12
7 10
6 13
4 9
4
6
4
11
5
Chen CL
37
>> U = [6,2,1; 2,5,4; 4,3,2; 9,7,3]; >> P = [10,12,13,15; 8,7,6,4; 12,10,13,9; 6,4,11,5]; >> C = U’*P, ... >> Quarterly_Costs = sum( U’*P), ... >> Category_Costs = sum((U’*P)’) C = 178 138 84
162 117 72
241 172 96
179 112 64
Quarterly_Costs = 400 351 509
355
Category_Costs = 760 539 316
%178: material cost in Q1 %138: labor cost in Q1 % 84: transportation cost in Q1
Chen CL
38
Special Matrices Command eye(n) eye(size(A)) ones(n) ones(m,n) ones(size(n)) zeros(n) zeros(m,n) zeros(size(n))
Description Creates an n × n identity matrix Creates an identity matrix with the same size of A Creates an n × n matrix of ones Creates an m × n array of ones Creates an array of ones with the same size of A Creates an n × n matrix of zeros Creates an m × n array of zeros Creates an array of zeros with the same size of A
Chen CL
39
Polynomials f (x) = 9x3 5x2 + 3x + 7 f (x) + g(x) = 9x3 + x2 + 2x + 9
−
f (x)g(x) = 54x5 f (x) = 1.5x g(x) >> f = [9, -5, 3, 7]; >> summation = f + g0 summation = 9 1
2
g(x) = 0x3+6x2
− x + 2
− 39x4 + 41x3 + 29x2 − x + 14 − 0.5833
g = [6, -1, 2]; g0 = [0, 6, -1, 2];
9
Chen CL
40
>> product = conv(f, g) product = 54 -39
41
29
-1
14
>> [quotient, remainder] = deconv(f, g) quotient = 1.5000 remainder = 0 >> >> >> >>
a x f g
= = = =
-0.5833 0
-0.5833
8.1667
[9, -5, 3, 7]; [0: 2: 10]; polyval(a, x), ... polyval([9, -5, 3, 7], [0: 2: 10])
f = 7
65
515
1789
4319
8537
g = 7
65
515
1789
4319
8537
Chen CL
41
Polynomial Functions Command conv(a,b) [q, r]=deconv(num,den) poly(r) polyval(a,x) roots(a)
Description Computes product of two polynomials (need not be the same degree) Computes dividing of two polynomials Computes coefficients of the polynomial whose roots are specified by vector r Evaluates a polynomial at specified values of x Computes the roots of a polynomial (result is a column vector)
Chen CL
42
Example: Earthquake Resistant Building Buildings designed to withstand earthquakes must have natural frequencies of vibration that are not close to the oscillation frequency of the ground motion. A building’s natural frequencies are determined primarily by the masses of its floors and by the lateral stiffness of its surrounding columns (which act like horizontal springs). We can find these frequencies by solving for the roots of a polynomial called the structure’s characteristic polynomial. For a three-story building, if each floor has a mass m and the columns have stiffness k, the polynomial is (α
− f 2)[(2α − f 2)2 − α2] + α2f 2 − 2α3
where α = k/4mπ 2. The building’s natural frequencies in cycles per second are the positive roots of this equation. Find the building’s natural frequencies (cycles/second) for the case where m = 1000 kg and k = 5 106 newtons per meter.
×
Chen CL
43
k = 5e+6; m = 1000; alpha = k/(4*m*pi^2); p1 = [-1, 0, alpha]; p2 = [-1, 0, 2*alpha]; p3 = [alpha^2, 0, -2*alpha^3]; p4 = conv(p2, p2) - [0, 0, 0, 0, alpha^2]; p5 = conv(p1, p4); p6 = p5 + [0, 0, 0, 0, p3]; r = roots(p6) pos = r(r>0) r = 20.2789 -20.2789 14.0335 -14.0335 5.0085 -5.0085
pos = 20.2789 14.0335 5.0085
Chen CL
44
Test Your Understanding T2.5-1 Use MATLAB to confirm that (20x3 − 7x2 + 5x + 10)(4x2 + 12x − 3)
= 80x5 + 212x4 − 124x3 + 121x2 + 105x − 30
T2.5-2 Use MATLAB to confirm that 12x3 + 5x2 − 2x + 3 = 4x + 11 2 3x − 7x + 4
T2.5-3 Use MATLAB to confirm that 6x3 + 4x2 − 5 = 0.7108 3 2 12x − 7x + 3x + 9 when x
2
Chen CL
45
Cell Array Functions Function
Description
c = cell(n) c = cell(n,m) celldisp(c)
Creates an n * n cell array c of empty matrices Creates an n * m cell array c of empty matrices Displays the contents of cell array c
cellplot(c)
Displays a graphical representation of the cell array c Converts a numeric array A into a cell array c
c = num2cell(A) [X,Y, ...] = deal(A,B, ...) [X,Y, ...] = deal(A) iscell(C)
Matches up the input and output lists. Equivalent to X = A, Y = B,... Matches up the input and output lists. Equivalent to X = A, Y = A,... Returns a 1 if C is a cell array; otherwise, returns a0
Chen CL
46
Example: An Environmental Database Data collection is important for early detection of change in our environment. In order to detect such changes, we need to be able to analyze the database efficiently, and this effort requires a database that is set up for easy access. As a simple example, suppose you want to create a 2 2 cell array A, whose cells contain the location, that data, the air temperature (measured at 8 A.M., 12 noon, and 5 P.M.), and the water temperatures measured at the same time in three different points in a pond. The cell array looks like the following.
×
Walden Pond [60 72 65]
June 13, 1997
55 57 56 54 56 55 52 55 53
Chen CL
47
Example: An Environmental Database Solution:
Create an array A(1,1) A(1,2) A(2,1) A(2,2)
= = = =
{’Walden Pond’}; {’June 13, 1997’}; {[60, 72, 65]}; {[55, 57, 56; 54, 56, 55; 52, 55, 53]};
Content Indexing: A{1,1} A{1,2} A{2,1} A{2,2}
= = = =
’Walden Pond’; ’June 13, 1997’; [60, 72, 65]; [55, 57, 56; 54, 56, 55; 52, 55, 53];
Chen CL
48
Example: An Environmental Database
Type A at the command line: A = ’Walden Pond’ [1*3 double]
’June 13, 1997’ [3*3 double]
Typing celldisp(A) displays A{1,1} = Walden Pond A{2,1} = 60 72 65 . . etc.
Chen CL
49
Exampl Exa mple: e: An Env Enviro ironme nmenta ntall Data Databas base e
Typing cellplot(A) Typing cellplot(A) to to see this display for cell array A
Use commas or spaces with braces to indicate columns of cells and use semicolons to indicate rows of cells B = {[ {[2, 2,4] 4], , [6 [6,,-9; 9;3, 3,5] 5]; ; [7 [7;2 ;2], ], 10 10}; };
Create the following 2
× 2 cell array: [2 4] [7 2]
6 3
−9 5
10
Chen CL
50
Accessing Cell Array
One can access the contents of a cell array by using either cell indexing or content indexing
C = cell(3,5) C(2 C( 2,4 ,4) ) = {[ {[6 6, -3 -3,7 ,7]} ]}; ; C(1,5) = {1:10}; C(3,4) = {’30 mph’} Speed = C(3,4) D = C(1:3, 2: 2:5)
Speed = C{3,4}
% % % % % % % % % %
prelocate empty cell array with specified siz put 1x3 arr rray ay in cel ell l (2 (2,4 ,4) ) put numbers 1-10 in cell (1,5) put one string in cell (3,4) cell ce ll in inde dexi xing ng place contents of cell(3,4) in new variable place co pl cont.s in in ro rows 11-3, co col.s 22-5 in in ce cell D a new cell array D conten con tent t ind indexi exing ng assign contents ’30 mph’ to variable Speed
deal function deal function accesses elements of a range of cells in a cell array [x,y] [x, y] = dea deal(B l(B{2, {2,:}) :}) x = 7
2
y = 10
Chen CL
51
Using Cell Array
Use cell arrays in comma-separated lists H = {[ {[2, 2,4, 4,8] 8], , [6 [6,,-8, 8,3] 3], , [2 [2:6 :6], ], [9 [9,2 ,2,5 ,5]} ]}; ;
The expression H{2:4} is equivalent to a comma-separated list of the second through fourth cells in H
To create a numeric array J from the first, second, and fourth cells in the cell array H J = [H [H{1 {1}; }; H{ H{2} 2}; ; H{ H{4 4}]
The result is: J =
2
4
8
6 9
−8
3 5
2
Chen CL
52
Using Cell Array
Typing H{2:3} displays the arrays in second and third cells >>H{2:3}
ans = 6 ans = 2
-8 3
3 4
5
6
Chen CL
53
Using Cell Array
Use cell arrays in function input and output lists >>[K{1:2}] = max(J) K = [1x3 double]
[1x3 double]
>> K{1} % to see the maximum value ans = 9 4
8
>> K{2} ans = 3 1
1
Chen CL
54
Using Cell Array
Apply functions and operators to cell contents L = {[2,4,8], [6,-8,3]; [2:6], [9.2.5]; [1,4,5], [7,5,2]}; max(L{3,2})
ans = 7
Nested cell arrays have cells that contain cell arrays N(1,1) = {2,7,5}; N(1,2) = {{[5,9,1]; 4,8,0], ’Case 1’; {5,8}, [7,3]}}; N
N = [1x3 double]
{2x2 cell}
Chen CL
55
Using Cell Array
Following steps create the same array N using the cell function % FIrst create an empty 1x2 cell array N = cell(1,2) % Then create a 2x2 cell array inside N(1,2) N(1,2) = {cell(2,2)} % Then fill N using assignment statement N(1,1) = {[2,7,5]}; N{1,2}(1,1) = {[5,9,1; 4,8,0]} N{1,2}(1,2) = {’Case 1’} N{1,2}{2,1}(1) = {5} N{1,2}{2,1}(2) = {8} N{1,2}(2,2) = {[7,3]}
Chen CL
56
Using Cell Array
Final example: H = {[2,4,8], [6,-8.3]; [2,6], [9.2.5]; [1,4,5], [7,5,2]}; J = [H{1,1}; H{1,2}; H{2,2}];
2 J = 6 9
4 8 8 3
−
2 5
H{2:3, :} ans = 2 ans = 9
3
4
2
5
5
6
ans = 1 ans = 7
4
5
5
2
Chen CL
57
Test Your Understanding T2.6-1 Create the following cell array: A = {[1:4], [0, 9, 2], [2:5], [6:8]} What is B = A{1:2} ? What is C = [A{2}; A{4}] ? What is D = min(A{2:3}) ?
Chen CL
58
Structure Arrays Example: A Student Database Assignment of data in the structure array student
Create a structure array to contain the following types of student data: Student name Social Security number Email address Test scores
Enter the data into the database.
Chen CL
Create the structure and start with data for the first student student.name student.SSN student.email student.tests
59
= = = =
’John Smith’; ’392-77-1786’; ’
[email protected]’; [67, 75, 84];
Type student >> student
name: ’John Smith’ SSN: = ’392-77-1786’ email: = ’
[email protected]’ tests: = [67 75 84]
Chen CL
To add a second student student(2).name student(2).SSN student(2).email student(2).tests
60
= = = =
’Mary Jones’; ’431-56-9832’; ’
[email protected]’; [84, 78, 93];
Type student again >> student =
1x2 struct array with fields: name SSN email tests
Chen CL
Obtain information about the fields by using fieldnames >> fieldnames(student) ans = ’name’ ’SSN’ ’email’ ’tests’
To build a structure array named sa_1 sa_1 = struct(’field1’,’values1’,’field2’,’values2’,...) student = struct(’name’,’John Smith’, ’SSN’,’392-77-1786’,... ’email’,’
[email protected]’, ’tests’,[67,75,84])
Modifying structures student(1).phone = ’555-1653’
61
Chen CL
62
Structure Functions Function
Description
names = fieldnames(S)
Returns the field names associated with the structure array S as names, a cell array of strings
F = getfield(S,’field’)
Returns the contents of the field ’field’ in the structure array S. Equivalent to F = S.field
isfield(S,’field’)
Returns 1 if ’field’ is the name of a field in the structure array S, and 0 otherwise
isstruct(S)
Returns 1 if the array S is a structure array, and 0 otherwise
S = rmfield(S,’field’)
Removes the field ’field’ from the structure array S
S = setfield(S,’field’,V)
Sets the contents of the field ’field’ to the value V in the structure array S
S = struct(’f1’,’v1’,’f2’,’v2’,...)
Creates a structure array with the fields ’f1’, ’f2’, . . ., having the values ’v1’, ’v2’,
...
Chen CL
63
Test Your Understanding T2.7-1 Created the structure array student shown in Figure 2.7-1 and add the following information about a third student: name: SSN: e-mail: tests:
Alfred E. Newman; 555-12-3456;
[email protected]; 55,45,58.
T2.7-2 Edit your structure array to change Mr.Newman’s second test score from 45 to 53. T2.7-1 Edit your structure array to remove the SSN field.