Exam 2 – CS 151, Fall 2006 Name: __________________________________ TA Name: _______________________________ I. Answer questions 1-9 given these matrices: a = [ 1 4 ], b = [ 2 0 ], c = 5, d = 7, e = 5, x = [ 2 4 5 8 10 ], y = [ 100 120 140 160 180 ], and z = [ -40 30 35 80 ], 1. Sketch the resulting plot if you enter the MATLAB command, plot( a, b ). 2. Why do you receive an error after entering the command plot(y, z)? 3. What is output when you enter the MATLAB command, w = find( y <= 140 )? 4. After executing the command from problem 3, what is the result of then entering, y( w )? 5. If you execute the following commands from an m-file, if( x > 2 ) disp( ‘true’ ) else disp( ‘false’ ) end What will be displayed? ____________ 6. If you execute the following commands from a m-file, if( c > 4 & d <= 7 ) disp( ‘true’ ) else disp( ‘false’ ) end What will be displayed? ____________ 7. If you execute the following commands from a m-file, if( e ~= d ) disp( ‘true’ ) else disp( ‘false’ ) end What will be displayed? _____________ 8. How many times will the following loop repeat? ______________ for i = 1:length(z) fprintf( ‘x(%.0f) is %5.2f\n’, i, x ) end Exam 2 – CS 151, Fall 2006 Name: __________________________________ TA Name: _______________________________ 9. How many times will the following loop repeat? ______________ for i = 2:x(4) disp( i ) end II. Plotting Questions, 10-13 10. What command allows you to open a new figure window? 11. What command allows you add a plot to a currently graphed plot? 12. Using valid MATLAB commands, plot the sine function sin(t) against t between t = 0 and t = 2 (using 50 points). 13. Plot the same sine function using the function plot command. III. Executing Functions For problems 14-22, use the following function: function [a,b]=prod(c,d) % This function multiplies the value of the first argument by all of % the values in the second argument. [e,f] = size(c); % var represents the multiplication result and % var4 represents the size of the result if e ~= 1 | f ~= 1 disp('First Input argument must be a scalar'); a = []; b = 0; return end a = c * d; b = size(b); return 14. What is the name of the function? 15. How many input variables are there and give their names? 16. How many output variables are there and give their names? 17. If you enter this command in MATLAB, [x,y] = prod( 4, 8 ), what will be the result? Exam 2 – CS 151, Fall 2006 Name: __________________________________ TA Name: _______________________________ 18. If you enter this command in MATLAB, [x,y] = prod( 4, [-1 2 3] ), what will be the result? 19. If you enter this command in MATLAB, [x,y] = prod( 2, [6; 5; -1; 4] ), what will be the result? 20. If you enter this command in MATLAB, [x,y] = prod( [4 3], [8 5] ), what will be the result? 21. If you enter this command in MATLAB, x = prod( 4, [-2 7; 1 8] ), what will be the result? 22. If you enter this command in MATLAB, y = prod( 4, 8 ), what will be the result? For problems 23-26, use the following function: function print2 % This function prints out a standard error x = input('Enter an integer value'); disp(['There was an error in the program' num2str(x)]); % then, the function returns return 23. What is the function name? 24. How many input variables are there and give their names? 25. How many input variables are there and give their names? 26. What is output if you enter the MATLAB command, help print2? IV. Complete Functions 27. A rocket is launched vertically. The equation for the height of the rocket is: h(t) = -½(9.8)t2 + vot + ho. Using this equation, create a height function based on the following 4 steps: a) Define the signature of the function called height that accepts both ho and vo as input to the function and returns the maximum of the height values. b) After defining the signature, this function will need to create a vector of times from 0 to 100 seconds in increments of 0.2 seconds. c) Next, find the heights in this 0 to 100 second interval. Exam 2 – CS 151, Fall 2006 Name: __________________________________ TA Name: _______________________________ d) Finally, plot the results (y-axis is the height and x-axis is the time) and return the maximum height value in this 0 to 100 second interval. 28. Create a function that calculates the volume of a right circular cylinder. This function returns the volume and has no input arguments. The equation for the volume is V = r2h. This function should prompt the user to enter the values for r and h and use them in the calculation. 29. Create a function to perform the following 4 steps: a) Define the signature of the function to not return any values and to have one input variable. The input variable will be a vector of values in units of feet. b) Correctly convert the feet values to inches. c) Using the disp() command, print the values of these two vectors in a column format where feet is in the first column and inches is in the second column. d) Using the fprintf() command, print the values as such: 3 feet is 36 inches 5 feet is 60 inches 4 feet is 48 inches 30. Write a function that calculates the value of the equation: (3x-4) (x2+x-2) given a x input value. Make sure you process special values correctly. function value = fun2( x )