CS 1173: MATLAB Program Control Worksheet 1. Trace each of the following loops. Show all of the values that the variables take on in the boxes at the right. Also show any output if any. k x a) A simple loop for k = 1:3 x = k*k; fprintf('x =%g\n', x); end; b) An accumulation using a loop k x x = 5; for k = 1:3 x = x + k*k; end; fprintf('x =%g\n', x); c) An array using a loop k x x = [1, 2, 3, 4]; for k = 1:4 x(k) = 3*k; end; c) An array update equation k x x = [1, 2, 3, 4]; for k = 2:4 x(k) = x(k-1) + 3*k; end;2. Trace each of the following if statements. Show all of the values that the variables take on in the boxes at the right. Also show any output if any. x y y = 2; x = 35; if x <= 40 y = x + 3; else y = y + 10; end; 3. Translate the following into MATLAB code. Assume x, y, z, and h contain a single value. Assume measles is a vector containing the number of cases in each year from 1921 to 1950. a) If x is less than or equal to z, set y to 3. Otherwise, set y to 4. b) If h = 1, output a message that the null hypothesis is rejected. c) If the average number of measles cases is greater than 10, print a message. d) Define total21 to be a MATLAB variable containing the total number of measles cases for the years 1921 to 1930. If total21 is between 400 and 1000 inclusive, set the variable bigDecade to true. Otherwise set the variable bigDecade to
View Full Document