Harvard University, fas

Fall 2002

Home
Syllabus
21b Teachers
Assignments
Handouts
Web Links
Midterm 1 Review

Using Mathematica/Maple for least squares problems on larger data sets:

First, enter the two matrices, A and b (to find the least square solution to Ax = b). In Mathematica, matrices are entered row by row, separated by brackets, so for example the following are the matrices for the example we used in class (in the 10 and 11am sections). Just type in the two lines and press Shift+Enter to enter the matrices
A={{1,1},{2,1},{2,1}}
b={{1},{1},{2}}

Next calculate the least squares solution (the (A transpose A) inverse... equation) by typing in the following and pressing Shift+Enter after you finish (note in Mathematica, matrix multiplication is signified by a period) Inverse[Transpose[A].A].Transpose[A].b

That's all there is to it! The output from your last line will be the matrix of coefficients (for instance in this case the output will be {{1/2},{1/2}}, standing for slope 1/2, intercept 1/2.

Now you can do multiple regression as well as finding lines of best fit. You'll just end up with as many columns in matrix A as unknown coefficients, but Mathematica should be able to handle some pretty large matrices for you.