Previous
Next

An M-file to generate easy-to-row-reduce matrices

February 15, 2011, 11:42 am

In my Linear Algebra class we use a lot of MATLAB — including on our timed tests and all throughout our class meetings. I want to stress to students that using professional-grade technological tools is an essential part of learning a subject whose real-life applications closely involve the use of those tools. However, there are a few essential calculations in linear algebra, the understanding of which benefits from doing by hand. One of those calculations is row-reduction. Nobody does this by hand; but doing it by hand is useful for understanding elementary row operations and for getting a feel for the numerical processes that are going on under the hood. And it helps with understanding later concepts, notably that of the LU factorization of a matrix.

I have students take a mastery exam where they have to reduce a 3×5 or 4×6 matrix to reduced echelon form by hand. They are not allowed any technology on that exam. I’ve learned that making up good matrices for this exam is surprisingly tricky. My first attempt at writing the exam resulted in a nice-looking matrix whose reduced echelon form had mind-bendingly big fractions in it. I want the exam to be about row reduction and not fraction arithmetic, so I  sat down this morning and wrote this MATLAB function called easyRR.m which automatically spits out \(m \times n\) random integer matrices whose row-reduction process might involve fractions but which aren’t horrendous:

[sourcecode language="matlab"]
%% Function to create an mxn matrix that is easy to row-reduce by hand.
% Basic idea: Construct this matrix by building an LU factorization for it
% where both L and U have small integer values.
% R. Talbert, Feb 15, 2011

function A = easyRR(m,n)

%% Create the L in the LU factorization. This matrix encodes the elementary
%% row operations needed to get A to echelon form.

% Start with a random integer square matrix:
L = randi([-10, 10], [m,m]);

% Replace diagonal elements with 1′s:
for i=1:m
L(i,i) = 1;
end

% Zero out all entries above the diagonal:
L = tril(L);

%% Now create the U in the LU factorization, using smaller integers so that
%% the back substitution phase isn’t too bad.

% This creates an mxn random integer matrix and zeros out all entries below
% the diagonal.
U = triu(randi([-5,5], [m,n]));

%% The easy-to-reduce matrix is the product of L and U.
A = L*U;

[/sourcecode]

Here’s a screenshot:

The fractions involved here have denominators no larger than 25, which is way more doable for students than what I had been having them work with (sorry, guys).

And, if you happen to have the Symbolic Toolbox for MATLAB, you can add the line latex(sym(A)) to the end and the function will spit out the \(\LaTeX\) code for that matrix, for easy copy/paste into the exam.

Anyway, I thought this was useful and so I’m giving it away!

Enhanced by Zemanta
This entry was posted in LaTeX, Linear algebra, Math, MATLAB, Teaching and tagged , , , , , . Bookmark the permalink.