pmid1.diary rohan ~/cs205/spr98/exams ======================================================= pmid1 diary of solutions - we'll discuss these in class on monday (3/2/98) i've tried to amplifying some of the solutions to help clarify what the MATLAB syntax is accomplishing. ----------------------------------------------------- (1a) ---- >> a = [1,0,0,0,0,1] a = 1 0 0 0 0 1 >> size(a) ans = 1 6 (1b) ---- >> a = [1,0,0;0,0,1] a = 1 0 0 0 0 1 >> size(a) ans = 2 3 (1c) ---- >> a = [1,0;0,0;0,1] a = 1 0 0 0 0 1 >> size(a) ans = 3 2 (1d) ---- >> a = [1;0;0;0;0;1] a = 1 0 0 0 0 1 >> size(a) ans = 6 1 (1e) ---- >> t = [4 24 9] t = 4 24 9 >> q = [t 0 t] q = 4 24 9 0 4 24 9 >> r = [t; 3:5; 2*t] r = 4 24 9 3 4 5 8 48 18 >> size(r) ans = 3 3 >> size(q) ans = 1 7 >> size(t) ans = 1 3 ===================================================================== (2) given matrix ------------------- >> g = [0.6 1.5 2.3 -0.5; 8.2 0.5 -0.1 -2.0; 5.7 8.2 9.0 1.5;... 0.5 0.5 2.4 0.5; 1.2 -2.3 -4.5 0.5] g = 0.6000 1.5000 2.3000 -0.5000 8.2000 0.5000 -0.1000 -2.0000 5.7000 8.2000 9.0000 1.5000 0.5000 0.5000 2.4000 0.5000 1.2000 -2.3000 -4.5000 0.5000 >> size(g) ans = 5 4 (2a) ---- >> a = g(:,2) a = 1.5000 0.5000 8.2000 0.5000 -2.3000 (2b) ---- >> b = g(4,:) b = 0.5000 0.5000 2.4000 0.5000 (2c) ---- >> c = [10:15] c = 10 11 12 13 14 15 >> size(c) ans = 1 6 (2d) ---- >> d = [4:9; 1:6] d = 4 5 6 7 8 9 1 2 3 4 5 6 >> size(d) ans = 2 6 (2e) ---- >> e = [0.0:0.1:1.0] e = Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 Columns 8 through 11 0.7000 0.8000 0.9000 1.0000 >> size(e) ans = 1 11 (2f) ---- >> f = g(1:2:5,:) f = 0.6000 1.5000 2.3000 -0.5000 5.7000 8.2000 9.0000 1.5000 1.2000 -2.3000 -4.5000 0.5000 >> size(f) ans = 3 4 ================================================================= Note - i'm just arbitrarily initializing some of the variables on the right hand side of the given expressions so that when i construct the correct matlab statement, i won't get "yelled at" you practice exam says you can assume the variables in the equations are scalar and have been assignment values, so you only need to provide the final matlab expression (3a) ---- >> v=rand; s=rand; >> friction = v^2/(30*s) % notice v^2/30*s is v^2/30 * s friction = 0.0340 (3b) ---- >> x1=rand;x2=rand;y1=rand;y2=rand; >> slope = (y2-y1)/(x2-x1) % notice NEED all parentheses slope = -1.2768e+03 (3c) ---- >> r1=rand; r2=rand; r3=rand; >> >> >> resistance = 1/((1/r1)+(1/r2)+(1/r3)) % NEED parentheses resistance = 0.0312 (3d) ---- >> f=rand; p=rand; d=rand; v=rand; >> >> >> loss = f * p / d * v^2 / 2 % convince yourself don't need parentheses loss = 1.2503e-06 ============================================================= (4) --- >> a = [2 -1 5 0] a = 2 -1 5 0 >> b = [3 2 -1 4] b = 3 2 -1 4 (4a) ---- >> c = a-b c = -1 -3 6 -4 (4b) ---- >> c = b+a-3 c = 2 -2 1 1 (4c) ---- >> %explore matrix .^ matrix >> a a = 2 -1 5 0 >> b b = 3 2 -1 4 >> a.^b ans = 8.0000 1.0000 0.2000 0 >> c = 2*a + a.^b c = 12.0000 -1.0000 10.2000 0 (4d) ---- >> >> % explore scalar .^ matrix >> >> b b = 3 2 -1 4 >> 2.^b ans = 8.0000 4.0000 0.5000 16.0000 >> c = 2.^b + a c = 10.0000 3.0000 5.5000 16.0000 (4e) ---- >> >> % explore .\ dot-left division >> % explore matrix .\ matrix - dot left division like multiply by inverse >> % b .\ a is equivalent to reciprocal (component by componet) of b >> % time (component by component) a >> a a = 2 -1 5 0 >> b b = 3 2 -1 4 >> b .\ a ans = 0.6667 -0.5000 -5.0000 0 >> >> % explore ./ dot-right division (usual way) >> % just for the fun of it - wan't on the practice test but fits into >> % the discussion >> >> a a = 2 -1 5 0 >> b b = 3 2 -1 4 >> a ./ b ans = 0.6667 -0.5000 -5.0000 0 (4f) ---- >> >> % explore .* >> >> 2*b/3.0 ans = 2.0000 1.3333 -0.6667 2.6667 >> a a = 2 -1 5 0 >> 2*b/3.0 .* a ans = 4.0000 -1.3333 -3.3333 0 ================================================================ (5) --- >> [0:0.3:2, 1:0.75:4] ans = Columns 1 through 7 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 Columns 8 through 12 1.0000 1.7500 2.5000 3.2500 4.0000 >> round ([0:0.3:2, 1:0.75:4]) ans = 0 0 1 1 1 2 2 1 2 3 3 4 ================================================================== (6) --- >> c = rand; l = rand; >> frequency = 1/sqrt(2*pi*c/l) % convince yourself no more parens are needed frequency = 0.1666 ================================================================= (7) --- >> a = 5.5; b = 1.5; k = -3; (7a) ---- >> k-b>a ans = 0 ----------------------------------------------------------- to clarify the order of operation for logical ops and arithmetic ops put in parentheses >> (k-b) > a ans = 0 >> k- (b>a) ans = -3 --------------------------------------------------------------- (7b) ---- >> a+b>=6.5 ans = 1 >> a+b ans = 7 (7c) ---- >> a<10 & a>5 ans = 1 >> a a = 5.5000 (7d) ---- >> ~(a==3*b) ans = 1 >> >> a==3*b ans = 0 (7e) ---- Note- look at the individual components first >> abs(k) > 3, k> abs(k) > 3 | k> b = [1 0 4;0 0 3; 8 7 0] b = 1 0 4 0 0 3 8 7 0 ---------------------------------------------------- NOTE: MATLAB looks at the matrix b in "column-major" order above. "any" is true (1) is any component in the row is true (nonzero). column1 yields a true (1) since both b(1,1) and b(3,1) are non zero >> any(b) ans = 1 1 1 ------------------------------------------------------------- NOTE: MATLAB looks at the matrix b in "column-major" order above, and "find"s the index of the nonzero (true) components ------------------------------------------------------- >> find(b) ans = 1 3 6 7 8 >> all(any(b)) ans = 1 >> any(all(b)) ans = 0 >> any(b(1:2,1:3)) ans = 1 0 1 >> b(1:2,1:3) ans = 1 0 4 0 0 3 ===================================================================== 9) i've initialize some of the variables used below to keep MATLAB from "yelling" at me when i construct the sample solutions. your practice midterm stated that the variables were all scalar and that you could assume they had values, where appropriate (9a) ---- >> time=rand; >> if time > 50 time = time + 1 end (9b) ---- >> poly=rand; >> if sqrt(poly) < 0.001 poly end >> poly poly = 0.6868 >> sqrt(poly) ans = 0.8287 (9c) ---- >> volt_1 = rand; volt_2 = rand; >> if abs(volt_1-volt_2) > 2 volt_1, volt_2 end (9d) ---- >> den = rand; num = rand; >> if den < 0.003 result = 0 else result = num/den end result = 0.6227 (9e) ---- >> count = rand; >> if log(x) >= 10 time = 0 else count = count+1 end count = 1.6539 >> if log(x) >= 10 time = 0 else count = count+1 end count = 2.6539 >> count count = 2.6539 (9f) ---- >> time=rand; dist=rand; >> if dist >= 100 time = time + 10 elseif dist > 50 % already know dist < 100 at this point time = time + 1 else time = time+0.5 end time = 0.9160 >> %% notice the change if you use else if instead of elseif >> if dist > 100 time = time + 10 else if dist > 50 time = time + 1 else time = time + 0.5 end end time = 1.4160 >> dist dist = 0.7012 >> diary off