CS 221 Lab 10 Fall 2011: Solving Systems of Linear Equations

Out: 16 November 2011
Due: 8 December 2011

Note Well: The purpose of lab exercises is for you to learn how to solve problems using these tools. It is not for you to type something into MATLAB.

Read the instructions for each step carefully. Do your best to carry them out on your own. If you don't understand something, ask your instructor -- that's what he's there for.


  1. Solve the following system of linear equations in five unknowns using MATLAB's "left division" (\) operator:
    3x1 + 4x2 - x3 + 9x4 + 27x5  =  326
    -10x1 - x2 + 2x3 + 4x4 - 17x5  =  -149
    -3x1 + 20x2 - x3 + 2x4 + 4x5  =  116
    12x1 + 13x3 - 5x4 + 7x5  =  129
    2x1 - 4x2 + 5x3 - 1x4 - 9x5  =  -73
    Store the solution in a column vector called x. Then use the MATLAB "save" command to save the variables A, B, and x from your workspace in a file called "matrices.mat".

  2. Create a plot showing how long matrix inversion takes compared to "left division", for problems involving 1000, 2000, 3000, 4000, and 5000, and 6000 unknowns. Using the MATLAB tic and toc commands to measure the time required to invert matrices of those sizes, and to compute the left-division.

    Hint: Write a script containing a for-loop that does the following steps for values of i equal to 1, 2, 3, 4, 5, and 6:

    1. Create a random square matrix A, of size 1000i, using the rand() function. (Note: you can overwrite A on each iteration.)
    2. Create a random 1x1000i column vector b, using rand(). (Note: you can overwrite b on each iteration.)
    3. Record the time it takes to invert the matrix A as the ith element of a vector called invtimes (i.e., as invtimes(i). (See the help and/or lecture slides usage of tic and toc.)
    4. Record the time it takes to do the "left division" A\b as the ith element of a vector called divtimes.
    5. Plot invtimes and divtimes on the same graph using plot(). Make the plot look nice, with a title, labeled axes (including units), and a legend. Save the plot as a jpeg file.

  3. Solve the system of equations from the Problem 1 above using Excel. Compare the results to those you got with MATLAB. Save the spreadsheet containing the results.
Create a zip file containing the three files (.mat, .jpg, and .xlsx) you created, and submit it via the submission portal as "Lab 10".