%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PHYS 210 Intro Computational Physics
%
% q2_key.m: Instructor's solution for MATLAB Quiz 2.
%
% Note: I have appended _k to all names to facilitate grading of
% student work with a script.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
q2_setup_octave;
% Q2.1
>>
v1_length_k = length(v1);
% Q2.2
>>
[m1_m_k, m1_n_k] = size(m1)
m1_m_k = 250
m1_n_k = 400
% Q2.3
>>
v2_k = v1([10056 24493 26631 27740 36992 45365 56733 62284 65056 84730])'
v2_k =
23
19
2
76
6
30
11
74
1
68
% Q2.4
>>
m2_k = m1([53 59 73 106], [158 255 340])
m2_k =
34 19 12
43 44 30
95 55 56
20 73 20
% Q2.5
>>
v3_k=v1(53400:53451)'
v3_k =
91
13
66
51
91
80
16
8
15
70
75
39
55
32
40
95
62
82
56
29
82
16
88
28
99
64
21
54
80
65
76
5
24
4
11
39
3
29
90
61
6
70
14
64
70
80
65
95
88
74
40
38
% Q2.6
>>
v4_k = v3_k;
v4_k([15, 27]) = []
v4_k =
91
13
66
51
91
80
16
8
15
70
75
39
55
32
95
62
82
56
29
82
16
88
28
99
64
54
80
65
76
5
24
4
11
39
3
29
90
61
6
70
14
64
70
80
65
95
88
74
40
38
% Q2.7
>>
m3_k = m1(35:39, 102:107)
m3_k =
21 87 67 43 63 55
22 30 77 55 52 10
84 44 27 97 26 35
1 57 33 11 74 42
12 27 88 1 15 43
% Q2.8
>>
m4_k = m3_k;
m4_k(3, :) = zeros(1, 6);
m4_k(:, 6) = ones(1, 5)
m4_k =
21 87 67 43 63 1
22 30 77 55 52 1
0 0 0 0 0 1
1 57 33 11 74 1
12 27 88 1 15 1
% Q2.9
>>
m5_k = m1(25:13:233, 10:42:395)
m5_k =
76 22 66 2 87 18 79 35 33 94
91 14 63 10 77 37 29 90 56 38
90 15 79 10 62 86 41 92 34 97
20 6 9 39 44 82 48 10 3 25
96 36 57 21 20 78 29 64 60 68
70 93 96 98 21 46 30 65 92 83
11 51 85 77 52 99 27 54 77 100
83 31 25 9 58 49 78 92 83 76
78 3 77 45 7 24 24 66 4 24
20 35 72 65 55 52 57 15 28 76
63 54 32 57 65 25 45 28 55 22
12 56 83 69 3 64 35 81 78 91
84 50 53 59 12 20 52 88 95 77
35 46 35 86 79 35 35 51 6 7
3 38 50 73 56 33 34 60 16 63
84 94 46 9 59 41 63 17 70 92
62 51 86 56 26 43 58 46 72 6
% Q2.10
>>
m6a_k = m5_k;
m6a_k(2:3:end, :) = []
m6a_k =
76 22 66 2 87 18 79 35 33 94
90 15 79 10 62 86 41 92 34 97
20 6 9 39 44 82 48 10 3 25
70 93 96 98 21 46 30 65 92 83
11 51 85 77 52 99 27 54 77 100
78 3 77 45 7 24 24 66 4 24
20 35 72 65 55 52 57 15 28 76
12 56 83 69 3 64 35 81 78 91
84 50 53 59 12 20 52 88 95 77
3 38 50 73 56 33 34 60 16 63
84 94 46 9 59 41 63 17 70 92
% Q2.11
>>
m6b_k = m5_k;
m6b_k(:, 8) = m6b_k(end:-1:1, 8)
m6b_k =
76 22 66 2 87 18 79 46 33 94
91 14 63 10 77 37 29 17 56 38
90 15 79 10 62 86 41 60 34 97
20 6 9 39 44 82 48 51 3 25
96 36 57 21 20 78 29 88 60 68
70 93 96 98 21 46 30 81 92 83
11 51 85 77 52 99 27 28 77 100
83 31 25 9 58 49 78 15 83 76
78 3 77 45 7 24 24 66 4 24
20 35 72 65 55 52 57 92 28 76
63 54 32 57 65 25 45 54 55 22
12 56 83 69 3 64 35 65 78 91
84 50 53 59 12 20 52 64 95 77
35 46 35 86 79 35 35 10 6 7
3 38 50 73 56 33 34 92 16 63
84 94 46 9 59 41 63 90 70 92
62 51 86 56 26 43 58 35 72 6
% Q2.12
>>
v5_k = [w1 w2(length(w2):-1:1) w3(6:5:end) w4([end-7, end])]
v5_k =
Columns 1 through 16:
41 55 44 27 60 64 81 78 44 55 79 74 65 22 77 12
Columns 17 through 32:
81 43 11 55 10 98 91 9 15 87 36 35 93 3 79 89
Columns 33 and 34:
96 80
% Q2.13
>>
v5a_k = v5_k';
v5a_k(1:10) = zeros(1,10);
v5a_k(end-9:end) = 20:-1:11
v5a_k =
0
0
0
0
0
0
0
0
0
0
79
74
65
22
77
12
81
43
11
55
10
98
91
9
20
19
18
17
16
15
14
13
12
11