Midterm 1 CS 205 - Intro. Computational Programming & Visualization March 4, 1998 5pm BA 259 Closed Book Exam Name: **** Full Solution from instructor 3/25/98 **** SHOW YOUR WORK TO RECEIVE FULL CREDIT This exam is designed to ask you to work with given functions and scripts and produce the output that MATLAB would. The focus is on the inputs and outputs from user-defined M-file functions and how they interact with M-file scripts. The MATLAB workspace is very important on this exam. You should be clear in indicating the output produced when an m-file script is invoked, as well as the contents and values of those contents for the MATLAB workspace. 1) Given the files: script1.m ---------- % m-file script [script1.m] for midterm 2/cs205/fall 96 % clear % empty the work space % x = -2:2; y = 2*x; z = 0:2:8; w = 2 % invoke the function, "testit" [variable,output,dummy] = testit (x,y,z,w); variable output dummy fprintf(' leaving script1 - did you get the dimensions and shapes correct?\n\n') testit.m ---------- function [out1,out2,out3] = testit (in1, in2, in3, in4) % m-file function devised for midterm 2 in cs 205/fall 96 % n1 = length(in1); n2 = length(in2); n3 = length (in3); n4 = length (in4); % out1 = [n1, n2, n3, n4]; out2 = [n1; n2; n3; n4]; out3 = [n1, n2]; fprintf ('\n leaving m-file function testit \n') >> script1 (indicate the output from executing the script) w = 2 leaving m-file function testit variable = 5 5 5 1 output = 5 5 5 1 dummy = 5 5 leaving script1 - did you get the dimensions and shapes correct? MATLAB workspace - indicate the contents and values of variabkes ------------------------------------------------------------------------------- x = -2 -1 0 1 2 y = -4 -2 0 2 4 z = 0 2 4 6 8 w = 2 variable = 5 5 5 1 output = 5 5 5 1 dummy = 5 5 Name Size Bytes Class dummy 1x2 16 double array output 4x1 32 double array variable 1x4 32 double array w 1x1 8 double array x 1x5 40 double array y 1x5 40 double array z 1x5 40 double array 2) Using the testit.m function above and the following script file script2.m --------- % m-file script [script2.m] for midterm 2/cs205/fall 96 % % start with an empty workspace clear % invoke the function, "testit" with constants as inputs [myvar,yourvar,ourvar] = testit (1,0,[1,2,3,4],1:2:5); myvar yourvar ourvar fprintf(' what were the dimensions of the constants? \n\n') >> script2 (indicate the output from executing the script) leaving m-file function testit myvar = 1 1 4 3 yourvar = 1 1 4 3 ourvar = 1 1 what were the dimensions of the constants? MATLAB workspace - indicate the contents and values of variables ------------------------------------------------------------------------------- myvar = 1 1 4 3 yourvar = 1 1 4 3 ourvar = 1 1 Name Size Bytes Class myvar 1x4 32 double array ourvar 1x2 16 double array yourvar 4x1 32 double array 3) Draw the following matrices and state the size (dimension)? a) A = [1,5,2;9,1,3]; A = 1 5 2 9 1 3 b) T = [ 4 24 3]; T = 4 24 3 Q = 2 * T; Q = 8 48 6 R = [ 3:5 2*Q]; R = 3 4 5 16 96 12 4) Given the following matrix -- -- | 0.6 1.5 2.3 -0.5 | | 8.2 0.5 -0.1 -2.0 | G = | 5.7 8.2 9.0 1.5 | | 0.5 0.5 2.4 0.5 | | 1.2 -2.3 -4.5 0.5 | -- -- Display the following. What is the size? a) A = G(2,:); A = 8.2000 0.5000 -0.1000 -2.0000 ----------------------------------------------- b) B = G(5,1); B = 1.2000 ---------------------------------------------- c) C = [1:5]; C = 1 2 3 4 5 ---------------------------------------------- d) 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 -------------------------------------------- e) F = G(:,1:2:4); F = 0.6000 2.3000 8.2000 -0.1000 5.7000 9.0000 0.5000 2.4000 1.2000 -4.5000 5) Given A = [3 -2 1 2] B = [3 3 -2 1] Give the value of C after executing the following statements a) C = 2*A + A.^B; --------------------------------------------- 2*A = 6 -4 2 4 A.^B = 27 -8 1 2 C = 33 -12 3 6 --------------------------------------------- b) C = 2*B/2.0.*A; 2*B/2.0= 3 3 -2 1 C = 9 -6 -2 2 -------------------------------------------- 6) Determine if the following expressions are true(1) or false(0). (Show intermediate steps). Assume a = 4 b = 1 k = 3 a) b - k <= a 1-4 <= 4 yes, so true 1 b) a + b >= 6.5 4+1 >= 6.5 no, no false 0 c) a < 10 & a > 5 4<10 & 4>5 = True & False = False 0 d) a < 10 | a > 5 4<10 | 4>5 = True | False = True 1 e) ~(a <= 3*b) ~(4 <= 3*1) = ~(4<=3) = ~false = true 1 7) Assume that all values are scalars. Write the MATLAB code to evaluate the correction factor in a pressure calculation which is given mathematically as b c factor = 1 + --- + --- v v^2 factor = 1 + b/v + c/v^2 since exponentiation is done first, then multiply/divide (from left to right), then add/subtract (from left to right) - don't even need parenthesis here