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.
- Paper and pencil. Do the
following exercises from Strang,
Ch. 1. We'll discuss the implications of these examples next week.
- Ex. 1.3.5.
- Ex. 1.4.1.
- Ex. 1.4.3.
- Ex. 1.4.5.
- Ex. 1.6.1.
- Ex. 1.6.13.
- Ex. 1.6.14. (Start by simplifying the transpose of the
two matrix products.)
- 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.mat.
Check
your script by verifying your calculations by hand.
- ATB
- BTA
- AB
- BBT
- A-1
- (BBT)-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?
- CTD
- DTC
- CD
- DDT
- C-1
- (DDT)-1
- Least-squares linear regression.
Now code the fundamentals of univariate least-squares linear regression
in matrix form.
- 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.
- Now assume we have a overdetermined system of equations
empirically giving the relationship of y to x: Xm = y. Here,
- y is the column
vector of dependent variable observations;
- m is the 2 element
column vector containing the unknown intercept m1 and slope m2
of the regression on x,
and
- X is an nt x 2
matrix containing in its first column ones, and in its second column
the column vector x of
independent variable observations.
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?
- 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.
- 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.)
- 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).
- Products. Please
hand in the following as hard copy:
- A copy of your matrix algebra rule list from
Strang (PW2).
- Your answers to questions 1 and 2 above (don't forget to
include your hand-checked 2a-f) .
- A printout of the hw2 script
you wrote to answer questions 3a-f and 4d, and the answer to the
question posed in 3.
- Your answers to questions 4a-c,e, and the plot from problem 4d.
Back
to Schedule/Syllabus.