%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PHYS 210 Intro Computational Physics
%
% q3_k.m: Instructor's solution for MATLAB Quiz 3
%
% Note: I have appended _k to all names to facilitate grading of
% student work with a script.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>>
q2_setup_octave
% Q3.1
>>
m7_k = [a1 a2; a3 a4]
m7_k =
32 91 62 15 79 49 81 61
40 56 57 30 31 3 93 57
80 94 21 69 45 36 100 9
76 52 86 25 94 20 30 47
48 36 75 42 74 87 36 54
78 16 47 86 31 47 61 43
95 41 17 61 76 66 74 47
72 11 3 18 35 25 74 69
% Q3.2
>>
m8_k = [zeros(4) a2; a3 a4'; a1 eye(4)]
m8_k =
0 0 0 0 79 49 81 61
0 0 0 0 31 3 93 57
0 0 0 0 45 36 100 9
0 0 0 0 94 20 30 47
48 36 75 42 74 31 76 35
78 16 47 86 87 47 66 25
95 41 17 61 36 61 74 74
72 11 3 18 54 43 47 69
32 91 62 15 1 0 0 0
40 56 57 30 0 1 0 0
80 94 21 69 0 0 1 0
76 52 86 25 0 0 0 1
% Q3.3
>>
v7_k(8) = -1
v7_k =
0 0 0 0 0 0 0 -1
% Q3.4
>>
v8_k(3) = 2
v8_k =
0 0 2
>>
v8_k(10) = 8
v8_k =
0 0 2 0 0 0 0 0 0 8
% Q3.5
>>
m9_k(5,7) = 2
m9_k =
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 2
% Q3.6
>>
m1_md_k = diag(m1);
m1_md_k = m1_md_k(1:10:end)
m1_md_k =
3
60
89
89
39
51
2
6
70
93
100
31
4
8
4
6
66
24
44
16
9
64
35
30
56
% Q3.7
>>
m10_k = diag(v1(2001:11073:end))
m10_k =
Diagonal Matrix
75 0 0 0 0 0 0 0 0
0 10 0 0 0 0 0 0 0
0 0 59 0 0 0 0 0 0
0 0 0 24 0 0 0 0 0
0 0 0 0 89 0 0 0 0
0 0 0 0 0 90 0 0 0
0 0 0 0 0 0 92 0 0
0 0 0 0 0 0 0 77 0
0 0 0 0 0 0 0 0 48
% Q3.8
>>
m11_k = reshape(linspace(0.05,0.74,70),7,10)'
m11_k =
0.050000 0.060000 0.070000 0.080000 0.090000 0.100000 0.110000
0.120000 0.130000 0.140000 0.150000 0.160000 0.170000 0.180000
0.190000 0.200000 0.210000 0.220000 0.230000 0.240000 0.250000
0.260000 0.270000 0.280000 0.290000 0.300000 0.310000 0.320000
0.330000 0.340000 0.350000 0.360000 0.370000 0.380000 0.390000
0.400000 0.410000 0.420000 0.430000 0.440000 0.450000 0.460000
0.470000 0.480000 0.490000 0.500000 0.510000 0.520000 0.530000
0.540000 0.550000 0.560000 0.570000 0.580000 0.590000 0.600000
0.610000 0.620000 0.630000 0.640000 0.650000 0.660000 0.670000
0.680000 0.690000 0.700000 0.710000 0.720000 0.730000 0.740000
>>
m11_k_a = reshape(0.05:0.01:0.74,7,10)'
m11_k_a =
0.050000 0.060000 0.070000 0.080000 0.090000 0.100000 0.110000
0.120000 0.130000 0.140000 0.150000 0.160000 0.170000 0.180000
0.190000 0.200000 0.210000 0.220000 0.230000 0.240000 0.250000
0.260000 0.270000 0.280000 0.290000 0.300000 0.310000 0.320000
0.330000 0.340000 0.350000 0.360000 0.370000 0.380000 0.390000
0.400000 0.410000 0.420000 0.430000 0.440000 0.450000 0.460000
0.470000 0.480000 0.490000 0.500000 0.510000 0.520000 0.530000
0.540000 0.550000 0.560000 0.570000 0.580000 0.590000 0.600000
0.610000 0.620000 0.630000 0.640000 0.650000 0.660000 0.670000
0.680000 0.690000 0.700000 0.710000 0.720000 0.730000 0.740000
% Q3.9
>>
m12_k = m11_k
m12_k =
0.050000 0.060000 0.070000 0.080000 0.090000 0.100000 0.110000
0.120000 0.130000 0.140000 0.150000 0.160000 0.170000 0.180000
0.190000 0.200000 0.210000 0.220000 0.230000 0.240000 0.250000
0.260000 0.270000 0.280000 0.290000 0.300000 0.310000 0.320000
0.330000 0.340000 0.350000 0.360000 0.370000 0.380000 0.390000
0.400000 0.410000 0.420000 0.430000 0.440000 0.450000 0.460000
0.470000 0.480000 0.490000 0.500000 0.510000 0.520000 0.530000
0.540000 0.550000 0.560000 0.570000 0.580000 0.590000 0.600000
0.610000 0.620000 0.630000 0.640000 0.650000 0.660000 0.670000
0.680000 0.690000 0.700000 0.710000 0.720000 0.730000 0.740000
>>
m12_k(1:3, 1:3) = eye(3)
m12_k =
1.00000 0.00000 0.00000 0.08000 0.09000 0.10000 0.11000
0.00000 1.00000 0.00000 0.15000 0.16000 0.17000 0.18000
0.00000 0.00000 1.00000 0.22000 0.23000 0.24000 0.25000
0.26000 0.27000 0.28000 0.29000 0.30000 0.31000 0.32000
0.33000 0.34000 0.35000 0.36000 0.37000 0.38000 0.39000
0.40000 0.41000 0.42000 0.43000 0.44000 0.45000 0.46000
0.47000 0.48000 0.49000 0.50000 0.51000 0.52000 0.53000
0.54000 0.55000 0.56000 0.57000 0.58000 0.59000 0.60000
0.61000 0.62000 0.63000 0.64000 0.65000 0.66000 0.67000
0.68000 0.69000 0.70000 0.71000 0.72000 0.73000 0.74000
>>
m12_k(end-3:end, end-3:end) = reshape(16:-1:1,4,4)'
m12_k =
1.00000 0.00000 0.00000 0.08000 0.09000 0.10000 0.11000
0.00000 1.00000 0.00000 0.15000 0.16000 0.17000 0.18000
0.00000 0.00000 1.00000 0.22000 0.23000 0.24000 0.25000
0.26000 0.27000 0.28000 0.29000 0.30000 0.31000 0.32000
0.33000 0.34000 0.35000 0.36000 0.37000 0.38000 0.39000
0.40000 0.41000 0.42000 0.43000 0.44000 0.45000 0.46000
0.47000 0.48000 0.49000 16.00000 15.00000 14.00000 13.00000
0.54000 0.55000 0.56000 12.00000 11.00000 10.00000 9.00000
0.61000 0.62000 0.63000 8.00000 7.00000 6.00000 5.00000
0.68000 0.69000 0.70000 4.00000 3.00000 2.00000 1.00000
% Q3.10
>>
xx = linspace(-pi/2, pi/3, 101);
fv_k = sin(xx.^3) + cos(sqrt(abs(xx).^4))./(xx + 1) + 6
fv_k =
Columns 1 through 6:
8.03862 7.85350 7.64497 7.41956 7.18285 6.93924
Columns 7 through 12:
6.69188 6.44253 6.19151 5.93757 5.67768 5.40673
Columns 13 through 18:
5.11682 4.79616 4.42688 3.98091 3.41140 2.63341
Columns 19 through 24:
1.47396 -0.48501 -4.58640 -18.85831 111.50253 24.07267
Columns 25 through 30:
16.22275 13.28215 11.74229 10.79460 10.15222 9.68758
Columns 31 through 36:
9.33527 9.05830 8.83417 8.64841 8.49131 8.35612
Columns 37 through 42:
8.23801 8.13345 8.03979 7.95503 7.87762 7.80637
Columns 43 through 48:
7.74032 7.67872 7.62096 7.56657 7.51514 7.46636
Columns 49 through 54:
7.41997 7.37575 7.33352 7.29315 7.25452 7.21753
Columns 55 through 60:
7.18211 7.14820 7.11575 7.08473 7.05511 7.02687
Columns 61 through 66:
7.00000 6.97451 6.95039 6.92765 6.90630 6.88636
Columns 67 through 72:
6.86786 6.85080 6.83522 6.82114 6.80860 6.79762
Columns 73 through 78:
6.78824 6.78049 6.77441 6.77003 6.76739 6.76652
Columns 79 through 84:
6.76746 6.77024 6.77488 6.78141 6.78984 6.80018
Columns 85 through 90:
6.81241 6.82651 6.84244 6.86013 6.87949 6.90038
Columns 91 through 96:
6.92262 6.94599 6.97021 6.99493 7.01974 7.04411
Columns 97 through 101:
7.06747 7.08911 7.10823 7.12391 7.13514
% Q3.11
>>
a_44 = [[78, 17, 4, 88]; [44, 85, 0, 11]; [3, 15, 48, 37]; [12, 28, 80, 98]];
b_44 = [64; 42; 62; 57];
x_44_k = a_44 \ b_44
x_44_k =
2.82794
-0.74215
2.70257
-1.75878
% Q3.12
>>
v1_min_k = min(v1)
v1_min_k = 1
>>
v1_max_k = max(v1)
v1_max_k = 100
% Q3.13
>>
cos_v1_k = sort(cos(v1));
cos_v1_max_k = cos_v1_k(end)
cos_v1_max_k = 0.99984
% Q3.14
>>
m1_mean_k = mean(mean(m1))
m1_mean_k = 50.519
% NOTE: median(median(m1)) does not return the correct answer (why?)
% so use reshape to convert the elements of the matrix into a row
% vector with same number, n, of elements as m1 -- n = prod(size(m1))
>>
m1_median_k = median(reshape(m1, 1, prod(size(m1))));
% Q3.15
>>
fact_17_k = prod(1:17)
fact_17_k = 3.5569e+14
% Q3.16
% This one should make sense once we've studied Matlab's Boolean (logical)
% operations.
>>
n23_k = sum(sum(m1 == 23))
n23_k = 1049