Practice Midterm 2 CS 205 - Spring 98 This exam is designed to ask you to work with given functions and scripts and produce the output that MATLAB would. You should use MATLAB to practice with these. The actual scripts and functions have been placed within your MATLAB path, so you can easily see their output. You'll want to experiment with these, because the exam itself will have modified versions of these ideas. 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 and on the exam you'll be asked to give the values that each of the variables contains. So for problem one, you should be able to draw 1) Given the files: script1.m ---------- % m-file script [script1.m] for practice midterm 2/cs205/spr98 % clear % empty the work space who % x = 0:0.1:5; y = -2:5; z = 1:2:10; % invoke the function, "testit1" [output,dummy] = testit1 (x,y,z); output dummy fprintf(' leaving script1 - did you get the dimensions and shapes correct?\n\n') whos testit1.m ---------- function [out1,out2] = testit1 (in1, in2, in3) % m-file function devised for practice midterm 1 in cs 205/spr98 % the file is available from the class home page % http://www.stewart.cs.sdsu.edu/cs205/ % and from the individual student's rohan account for this course % so that you can practice with it to check your understanding of % communications between MATLAB m-file scripts and m-file functions % n1 = length(in1); n2 = length(in2); n3 = length (in3); % ; suppressed output % out1 = [n1, n2, n3]; % first output parameter is a matrix of size 1 by 3 out2 = [n1; n2; n3]; % second output parameter is a matrix of size 3 by 1 fprintf ('\n leaving m-file function testit1 \n') =============================================================== Produce the output produced from >> script1 MATLAB workspace ------------------------------------- | | | | | indicate contents of x y z | | dummy | | output | | | ------------------------------------- ============================================================== 2) similair to 1) with the following example files script2.m --------- % m-file script [script2.m] for practice midterm 1/cs205/spr98 % clear % emtpy the work space x = -3:0.1:3; y = rect(x); % a function to compute the "rectangle function" plot (x,y), title(' Rectangle Function'), ... xlabel ('x'), ylabel ('y'), grid fprintf('\n\n finished with script 2 \n\n') who rect.m ------ function r = rect (x) % RECT The rectangular function is defined to be 1 % on [-0.5,0.5] and 0 elsewhere % % m-file function [rect.m] devised for practice midterm 1 in cs 205/spr98 % the file is available from the class home page % http://www.stewart.cs.sdsu.edu/cs205/ % and from the individual student's rohan account for this course % so that you can practice with it to check your understanding of % communications between MATLAB m-file scripts and m-file functions % % initialize output matrix to be all zeros, same size as input r = zeros(size(x)); % set1 = find(abs(x) <= 0.5); % identify component of input vector in [-0.5,0.5] r(set1) = ones(size(set1)); % for those component in [-0.5,0.5], set value 1 =================================================================== 3) similair to 1) and 2) with these files script3.m --------- % m-file script [script3.m] for practice midterm 1/cs205/spr98 % % start with an empty workspace clear who % invoke the function, "testit1" with constants as inputs [output,dummy] = testit1 (1,0,[1,2,3,4]); output dummy fprintf(' what were the dimensions of the constants? \n\n') who testit1.m --------- function [out1,out2] = testit1 (in1, in2, in3) % m-file function devised for practice midterm 1 in cs 205/spr98 % the file is available from the class home page % http://www.stewart.cs.sdsu.edu/cs205/ % and from the individual student's rohan account for this course % so that you can practice with it to check your understanding of % communications between MATLAB m-file scripts and m-file functions % n1 = length(in1); n2 = length(in2); n3 = length (in3); % ; suppressed output % out1 = [n1, n2, n3]; % first output parameter is a matrix of size 1 by 3 out2 = [n1; n2; n3]; % second output parameter is a matrix of size 3 by 1 fprintf ('\n leaving m-file function testit1 \n') ======================================================================== 4) as above with the following script script4.m --------- % m-file script [script4.m] for practice midterm 1/cs205/spr98 % clear % empty the workspace s = 'this is a string'; % invoke the function, "testit1" [output,dummy] = testit1 (s,' what is the length',0); output dummy s disp(s) fprintf(' \n\nfinished script 4 \n') whos