Homework 1

Submit homework as a ".m" script file. Commands should be in order and the file should be executable. Open a new .m file and type a command like a=5. Execute the command (on a PC) by going to the debug menu and selecting Run.

Answers to verbal questions in text should require no more than one or two sentences, and should be written as matlab comments, like:

% This is a comment

m=4*5; % this is a matlab command with a comment after it.

Please ask questions if you don't understand any part of the assignment, by email or in class.

    1) Matlab coding concepts
    2) Linear Algebra concepts
      % Generate a random x vector.
      rx = ceil(10*rand(1,100));
      % Generate a random y vector.
      ry = ceil(10*rand(1,100));
      % do a scatter plot
      scatter(rx,ry); axis([0 20 0 20]) % Enter the following matrix
      Q = [ 1.2500 0.7500; 0.7500 1.2500]
      % Q will transform our x and y vectors. Let's transform all the
      % points. We could do it as a for loop:
      for j=1:100,
        temp = Q*[rx(j); ry(j)]
        rxtransform(j) = temp(1);
        rytransform(j) = temp(2);
      end
      scatter(rxtransform,rytransform);
      % However, let's do it as a matrix multiply.
      % First create a new matrix X by stacking rx and ry to form a 2 by 100 matrix.
      % Then compute Y = Q*X. Separate it back into rxtransform and rytransform by
      % setting rxtransform and rytransform equal to the 1st and 2nd rows of Y
      % respectively
      % What kind of transformation does Q perform? (It has a name)
      % Every transformation can be decomposed into a rotation U, a scaling S, followed
      % by another rotation. Let's use the Singular Value Decomposition to compute
      % U and S. [U,S,V]=svd(Q);
      % verify that U*S*V' is equal to Q.
      % Now U is a rotation matrix, so
      % U = [cos(theta) sin(theta); -sin(theta) cos(theta)];
      % Use this fact to compute theta (note theta in radians, so multiply result by 180/pi)
    3) Probability concepts
      % Let's sample from a discrete probability distribution
      % Plot the distribution
      p = binopdf(1:6,7,0.5);
      plot(1:6,p)
      % The theoretical expectation is given by: theory_mean = sum(p.*(1:6)); % generate 100 samples from the distribution
      r = binornd(7,0.5,100,1);
      % compute the empirical expected value of r two ways
      % 1) use the mean() function
      % 2) generate a uniform weight vector
      weight = ones(100,1)/100;
      % we can also compute the mean as
      emp_mean2 = sum(weight.*r);
      % compute the empirical average as weight*r (Why does this work?)
      % Let's bin the samples
      N = hist(r,1:6);
      p_empirical = N/sum(N);
      plot(1:6,p_empirical)
      % Finally we can compute the empirical mean as
      emp_mean3 = sum(p_empirical.*(1:6))

      Extra Credit: Simulate the following problems

      Birthday problem: What is the probability that at least two people share a birthday in a room with 45 people?

      Game Show problem: consider a game show. The host tells you there is a prize behind one of three curtains, A, B, C. You guess A and tell the host. The host then opens curtain B and shows you there is no prize behind that curtain. The host then offers you the opportunity to switch from A to C. Should you switch or stay put or does it matter at all?