Final Examination of Tongji University (A) b= a(b The First Semester(2013---2014) a(b)=_ a啡 a(b)=).02= length((b])= Paper-making Teacher:CHEN Ming Audit Teacher:Hans WIEDMANN 9.Assume that array'array'is defined as shown,ad determine the neof the following suby Course Number:_ Course Name:Matlab programming for Mechanical Design arrayl= Time:120minutes Maximum Marks:100 1amay12,3= Grade 一Mpr Number Name Marks 2aray12:4,【1330= I.Fill in the blanks.(50X1') 3到aray111l34gr= 1.MATLAB variable namescan contin ay combination of letters 、and and must begin with 10.Suppose that a=[1 ;34]and b=3;2-1]. If you typeab in the command window,then can get: 2.If you type MATLAB will display alist of possible help topics in the Command Window. If you type ab in the command window,then can get 3.You can type to ist all the variables in the current workspae,together with information about thersie 11.Determine the contentsof arrayafter the following statementsare exeuted. bytes,cass,etc. 1a=23:4578外aB1=a13乳 4.Suppose that you were looking for a function to take the inverse of a matrix,You will use the function:_ a= Suppose that you were going toclear the information in command window,You wll use the function: 211=123:456789外a121月=a33乳1 Suppose that you were going to cear the variables in the workspace,You will use the function: a=_ 5.x=zeros(2) y=ones(six) y=_ 3引a=123:456789外a=a11. z=one0,3到 1- t=zeros(length()t=_ a= 4a=123:456789啡a2end,2:cnd=2 a= then use MATLAB linspoce to generate this vector: 7.Write the results of the following MATLAB statements 12.Determine the sbsrip of thea m=1n=mm A=[](Waring:for example,A(1)=4 and you should use only one subscript! n= A()=5; 2a=ee3,3h:b=123a2=b 26,,14101:829:75刃:86:2=6254124:152 a- 6=12. 13. sta 3eros(1,3)ones3,1r13 the loop; ans=_ tatement terminates the current pass through the loop and returns control to the top of the loop. 8.Suppose that a=[123;456;789 and b=a>S. 14.Assume that value has been initialized to 5.6443,and determine what is printed out by eath of the following
1 Final Examination of Tongji University(A) The First Semester (2013 --- 2014) Paper-making Teacher: CHEN Ming Audit Teacher: Hans WIEDMANN Course Number: Course Name: Matlab programming for Mechanical Design Time: 120 minutes Maximum Marks: 100 Grade Major Number Name Marks Ⅰ. Fill in the blanks. (50×1’) 1. MATLAB variable names can contain any combination of letters, and , and must begin with a . 2. If you type , MATLAB will display a list of possible help topics in the Command Window. 3. You can type to list all the variables in the current workspace, together with information about their size, bytes, class, etc. 4. Suppose that you were looking for a function to take the inverse of a matrix, You will use the function: ; Suppose that you were going to clear the information in command window, You will use the function: ; Suppose that you were going to clear the variables in the workspace, You will use the function: . 5. x = zeros(2) x = ; y = ones(size(x)) y = ; z = ones(2,3) z = ; t = zeros( length(z) ) t = . 6. Use MATLAB operator ':' to generate the vector of [ 𝜋 100 , 2𝜋 100 , 3𝜋 100 , …, 𝜋]: ; then use MATLAB linspace to generate this vector: . 7. Write the results of the following MATLAB statements. 1) m = 7:11; n = [m’ m’] n = ___ ; 2) a = eye(3,3); b = [1 2 3]; a(2,:) = b; a = _ _; 3) [zeros(1,3) ones(3,1)’ 1:3’] ans = _ . 8. Suppose that a = [ 1 2 3; 4 5 6; 7 8 9 ] and b = a > 5. b = ; a(b)= ; a(~b) = ; a(~~b)= ; a(~b) = a(~b).^2= ; length(a(b))= . 9. Assume that array 'array1' is defined as shown, and determine the contents of the following sub-arrays. array1 = [ 1.2 −3.5 4.1 −2.7 0.3 8.1 −7.6 1.8 3.1 0.7 0.5 0.4 1.4 5.7 0.2 1.0 ] 1) array1(2, :) = ; 2) array1(2:4, [1 3 3]) = ; 3) array1([1 1], [3 4])’ = . 10. Suppose that a = [1 2; 3 4] and b = [-1 3; 2 -1]. If you type a .* b in the command window, then can get: ; If you type a * b in the command window, then can get: . 11. Determine the contents of array ‘a’ after the following statements are executed. 1) a = [1 2 3; 4 5 6; 7 8 9]; a([3 1], :) = a([1 3], :) a = ; 2) a = [1 2 3; 4 5 6; 7 8 9]; a([1 2], :) = a([3 3], :) a = ; 3) a = [1 2 3; 4 5 6; 7 8 9]; a = a([1 1], :) a = ; 4) a = [1 2 3; 4 5 6; 7 8 9]; a(2:end, 2:end) = -2 a = . 12. Determine the subscript of the array. 1) A = [4 10 1; 8 2 9; 7 5 6] (Warning: for example, A(1) = 4 and you should use only one subscript!) A( ) = 5; 2) B(:, :, 1)=[4 10 1; 8 2 9; 7 5 7] ; B(:, :, 2)=[6 2 5; 4 12 4; 1 5 2] B( ) = 12. 13. statement terminates the execution of a loop and passes the control to the next statement after the end of the loop; statement terminates the current pass through the loop and returns control to the top of the loop. 14. Assume that value has been initialized to 5.6443, and determine what is printed out by each of the following
3 two points ()and (x2,y2)inaCartesian 1)disp(['value='int2str(value); coordinate system. 2)fprintf['value=0.2f,valuel: 5.%Define variables: 6.%x1 xposition of point1 15.After running the following codes,what will show on the MATLA command window? 7%y1 osition of point1 x=0.1,020,3.041,2.3,4 8%2 xposition of point 2 try 9.%2 yposition of point2 a=1,24 10.%distance catch 19.By executing the follewing Mfile,what in the commad window? a=xend,4); a=2b=64 end fprintf'before sample:a=fb=船n,ab此 3=. out=sample(a,b]: 16.X=2-23 fprintf('after sample:a=b str=['disp:x='num2str) fprintf('after sample:out=ffn,out); disp(strh fprintf('fprintf:x=8.4fnx): function out-sample(a,b) The resuls printed out by these statementsare fprintf('In sample:a=b=.a.b: 17.Here is the input of a and b,please fill in the result in the table a=142' b=a.女 b or(a,b) out=a+b(1); 11 fprinti('In sample:a=%b=f fb) 01 18.Here is a function and its comment lines. The lookfor command searches function according to the comment In sample: fine:_ _And the help command displays the comment lines from the line to line after sample: down the number before the comment line) after sample function distance=dist2(x1,v1,x2.y2) Calculate the distance between two points 2.%Function DST2 calculates the distance between
2 statements. 1) disp([‘value = ’ int2str(value)]); ; 2) fprintf(‘value = %0.2f’, value); . 15. After running the following codes, what will show on the MATLAB command window? x = [0, 1, 0, 2; 0, 3, 0, 4; 1, 2, 3, 4]; try a = x([1 1], [2 4]); catch a = x(2:end, 4); end a = . 16. X = 2*(1-2*i)^3; str = [‘disp: x = ’ num2str(X)]; disp(str); fprintf(‘fprintf: x = %8.4f\n’,x); The results printed out by these statements are . 17. Here is the input of a and b, please fill in the result in the table. a b xor(a, b) 1 1 0 1 18. Here is a function and its comment lines. The lookfor command searches function according to the comment line: . And the help command displays the comment lines from the line to line .(write down the number before the comment line) function distance = dist2(x1, y1, x2, y2) 1.%DIST2 Calculate the distance between two points 2.% Function DIST2 calculates the distance between 3.% two points (x1,y1) and (x2,y2) in a Cartesian 4.% coordinate system. 5.% Define variables: 6.% x1 -- x-position of point 1 7.% y1 -- y-position of point 1 8.% x2 -- x-position of point 2 9.% y2 -- y-position of point 2 10.% distance -- Distance between points 19. By executing the following M.file, what will appear in the command window? a = 2; b = [6 4]; fprintf(‘before sample: a = %f, b = %f %f \n’, a, b); out = sample(a, b); fprintf(‘after sample: a = %f, b = %f %f \n’, a, b); fprintf(‘after sample: out = %f %f \n’, out); function out=sample(a, b) fprintf(‘In sample: a = %f, b = %f %f \n’, a, b); a = b(1)+2*a; b = a.*b; out = a + b(1); fprintf(‘In sample: a = %f, b=%f %f \n’, a, b); before sample: ; In sample: ; In sample: ; after sample: ; after sample:
Name】 Number_ Major. ⅡP1ot(20') 2.Please complete the following figure:(4') 1.Please draw the figure which is displayed after the following statements:(4') x=0:0.1:pi x=123:23434 plot(x sinx) hold on y1=ones(3): Y2=13524357刀 plot(x cos(x)- subplot(211k titlesn/kost树n xlabelfx] plot(x,y1): subplot[212 ylabelfy) plot(x.y2:-] legend('sin(x.'cosx) tex(5,0.7,point of intersection grid on 2 0.8 05 15 2253 35 4 45 02 04 06 233544古
Name Number Major 3 Ⅱ Plot(20’) 1. Please draw the figure which is displayed after the following statements: (4’) x = [1 2 3; 2 3 4; 3 4 5]; y1 = ones(3); y2 = [1 3 5; 2 4 6; 3 5 7]; subplot(211); plot(x, y1); subplot(212); plot(x,y2,’--’); 2. Please complete the following figure: (4’) x = 0: 0.1: pi; plot(x, sin(x)); hold on plot(x, cos(x), 'r--') title('sin(x)/cos(x)') xlabel('x') ylabel('y') legend('sin(x)', 'cos(x)') text(0.75, 0.7, 'point of intersection') grid on
3.The gain Gof a certain microwave dish antenna can be expressed as a function of angle by the equation G(0)=Isinc401 for-<< Where 8 is measured in radians from the boresite of the dish,and sincx-sinx/x.Plot this gain function with the fplot function,with the title"Antenna Gain vs 8"in boldface.(4') ⅢProgramming(30') 1. 4.Please write a program which will ge erate a figure as the following one.(8) Please write a MATLAB program to solve the following system of simultaneous equations for X.(3) 2X13%3+X+2%=8 Plot ofy-2.10+15 X+3X1+元=6 XrX+X+8名=1 7%+Xr2X+2X=5
4 3. The gain G of a certain microwave dish antenna can be expressed as a function of angle by the equation 𝐆(𝛉) = |𝐬𝐢𝐧𝐜𝟒𝛉| 𝐟𝐨𝐫 − 𝛑 𝟐 < 𝛉 < 𝛑 𝟐 Where θ is measured in radians from the boresite of the dish, and sincx=sinx/x. Plot this gain function with the fplot function, with the title “Antenna Gain vs 𝛉”in boldface. (4’) 4. Please write a program which will generate a figure as the following one. (8’) Ⅲ Programming (30’) 1. Please write a MATLAB program to solve the following system of simultaneous equations for X.(3’) 2X1-3X2+X3+2X4 = 8 X1+3 X2+ X4 = 6 X1- X2+ X3+8 X4 = 1 7X1+ X2-2 X3+2 X4 = 5
Name】 Number Major. 2.Assume that a,b,c,and d are as defined,and evaluate the following expressions(6'): a=20:b=-2: c=0;d='Test'; 1)isinf(a/b) 2)isinf(a/c) 3到a>b&ischard 4)isempty(c) 5)(-a)b 6)(a)+b 4.Please analyze the following two statements,which one is better?Why?(3') 1)fort=1:100 s0uare(0间=i^2: square root0(间=M1/2: be_root间=r/3 end 2)日=1:100 s0uare(0间=i.2 square_root(ii)=iL(1/2); cube_root(间)=且.1/3: 3.Please modify the following program without using"for"loop.You can write the program using mask (A mask is an array that selects the elements of another array),but the result shall be the same as this one.(4") A123456789外 [rcl=size[A): for i=1:r for j=1:1:c A(i.jk3 A(i.j)=AGL)2; elseif A(i)>7 A,=-a. end; end;
Name Number Major 5 2. Assume that a, b, c, and d are as defined, and evaluate the following expressions(6’): a = 20; b = -2; c = 0; d = 'Test'; 1) isinf(a/b) 2) isinf(a/c) 3) a > b && ischar(d) 4) isempty(c) 5) (~a) & b 6) (~a) + b 3. Please modify the following program without using “for” loop. You can write the program using mask (A mask is an array that selects the elements of another array), but the result shall be the same as this one. (4’) A=[1 2 3; 4 5 6; 7 8 9]; [r c]=size(A); for i=1:1:r for j=1:1:c if A(i, j) 7 A(i, j) = - A(i, j); end; end; end; 4. Please analyze the following two statements, which one is better? Why ?(3’) 1) for ii = 1:100 square(ii) = ii^2; square_root(ii) = ii^(1/2); cube_root(ii) = ii^(1/3); end 2) ii = 1:100 square(ii) = ii.^2; square_root(ii) = ii.^(1/2); cube_root(ii) = ii.^(1/3);
5.Design a function named plotline with the user defined function varargin.The function accepts an 6.One simple random number generator algorithm is shown below,it will produce number between 0 arbitrary numbers of 1x2 row vectors,with each vector containing the (x,y)position of one point to and134455. values together.Note that this fncon ao 241=m0d(8121m+28411,134456 t specification on the plot function.(7) Now you need to write a function that generates one random number in the range 01each time it is called.(7
6 5. Design a function named plotline with the user defined function varargin. The function accepts an arbitrary numbers of 1x2 row vectors, with each vector containing the (x,y) position of one point to plot. The function plots a line connecting all of the (x, y) values together. Note that this function also accepts an optional line specification string and passes that specification on the plot function.(7’) 6. One simple random number generator algorithm is shown below, it will produce number between 0 and 134455. 𝒏𝒊+𝟏 = 𝒎𝒐𝒅(𝟖𝟏𝟐𝟏𝒏𝒊 + 𝟐𝟖𝟒𝟏𝟏, 𝟏𝟑𝟒𝟒𝟓𝟔) Now you need to write a function that generates one random number in the range 0~1 each time it is called. (7’)