GEOS 597e: Spatiotemporal Data Analysis Workshop

Homework 2: Basic matrix manipulations

Last updated 9/10/06.
To be completed prior to class session Weds., Sept. 13th.  


Introduction: Today we'll review some fundamental matrix operations that we'll be using routinely as we code the analysis tools we're developing over the semester.  So, a little matrix algebra practice to get started.  You'll need paper and pencil for Problem 1 and parts of Problems 3 and 4.

  1. Paper and pencil. Do the following exercises from Strang, Ch. 1.  We'll discuss the implications of these examples next week.
    1. Ex. 1.3.5.
    2. Ex. 1.4.1.
    3. Ex. 1.4.3. 
    4. Ex. 1.4.5.
    5. Ex. 1.6.1.
    6. Ex. 1.6.13.
    7. Ex. 1.6.14.  (Start by simplifying the transpose of the two matrix products.)  
  2. Tell Matlab to do it.  Now use Matlab to make some of these matrix manipulations on larger, arbitrary matrices.  Open a matlab script; call it  hw2.m .   Copy the following matlab datafile into your working directory (use "Save as..." in your favorite browser, or wget from the command line in *nix.).  Start matlab and load the data file:
    load hw2data.mat

For help, review our list of useful matlab functions, the Navy Matlab Primer, the error messages you may get, and use the help command.  At the end of your script, add the command: save hw2soln.matCheck your script by verifying your calculations by hand
    1. ATB
    2. BTA
    3. AB
    4. BBT
    5. A-1 
    6. (BBT)-1  
  1. Let Matlab do a little heavy lifting.  Now use your script to calculate the following matrices.  How do you know your calculations are correct?
    1. CTD
    2. DTC
    3. CD
    4. DDT
    5. C-1
    6. (DDT)-1
  1. Least-squares linear regression.  Now code the fundamentals of univariate least-squares linear regression in matrix form. 
    1. Start by writing down the algebraic equation for the line with y as dependent variable, x as independent variable, m2 as slope and m1 as the y-intercept, a constant. 
    2. Now assume we have a overdetermined system of equations empirically giving the relationship of y to x: Xm = y.   Here,
Write down the algrebraic system of equations represented by this formulation by multiplying through the matrix operation Xm = y for x and y vectors of length n.  You can write down just the first three and nth  Use subscript numbers to denote the first, second, third ... nth equations, first, second, third, ... nth elements of x and y, and the first and second elements of m.  Put an ellipsis (...) in between the third and nth equations to indicate you're skipping writing down all the equations in between.  How is this system of equations analogous to your answer to part a?
  1. Find the matrix solution for the unknown m of the system Xm = y.  (Like problem 1a but unlike problem 1d, there is  a unique solution which minimizes the sum of the residuals Xm - y.)  If this is unfamiliar, consult your favorite linear algebra book for the solution to an overdetermined linear system of equations; you'll need matrix matrix multiplications, the inverse, and the transpose to do so.
  2. Code your solution in matlab.  Now that you've solved for m, calculate the best-fit line yhat = Xm.  Make a labeled plot of both y and yhat vs. x on the same x-y plot (use plot(x,y,'o',x,yhat,'-') or similar to do this.) 
  3. Calculate the RMS error in your regression as sqrt of the mean of the sum of the squares of the differences between y and yhat.  Add the value of the RMS error to your plot labeling (perhaps in the title).
  1. Products.  Please hand in the following as hard copy:
    1. A copy of your matrix algebra rule list from Strang (PW2).
    2. Your answers to questions 1 and 2 above (don't forget to include your hand-checked 2a-f) . 
    3. A printout of the hw2 script you wrote to answer questions 3a-f and 4d, and the answer to the question posed in 3.
    4. Your answers to questions 4a-c,e, and the plot from problem 4d.
Back to Schedule/Syllabus.