Tag Archives: M-file

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

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:

%% 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;

% 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;

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


Filed under LaTeX, Linear algebra, Math, MATLAB, Teaching

And so it begins: Lab #1 in the MATLAB course

The MATLAB course began in earnest on Monday this week with our first full-length lab activity session. This was the second overall meeting, the first one being some organizational stuff and a lengthy fly-through of the main features of MATLAB. What follows is a breakdown of what we did and how it went, which also serves as an invitation for critique and suggestions in the comments.

First, some context. I intend for this course to be heavily hands-on with an emphasis on self-teaching within reasonable bounds. I laid a ground rule in the first class meeting that any question of the form “How do you do ____ in MATLAB?” was going to be met with the responses “What have you found in the MATLAB help documentation? What have you found via a Google search? What have you found out from your lab partner?” I’m not above giving hints to students in the class, but I insist that they exhaust all efforts of their own first before I come swooping in to the rescue, thereby creating a dependency upon me that makes their future, independent use of MATLAB problematic. So students have to do a considerable amount of individual work to learn basic commands and concepts before they come in to lab. Maybe I’ll blog more about that idea later, but now I want to get on to the activity itself.

First of all, students did this homework assignment in the week before the lab. The main item is to produce a graph of y = \sin(1/x) from x = -1 to x = 12. The theme of the lab this week was plotting, as you’ll see. This homework item forces them to figure out the basic usage of the PLOT command in MATLAB. It also forces them to figure out that you can’t just type 1/x in MATLAB if x is a vector.

The first 10 minutes of class are spent taking a quiz over the basic elements of the homework. Then, the lab activity itself. Here it is: CMP150-Lab1 (PDF, 201Kb). The summary:

  1. Reproduce a graph of a somewhat complicated function (related to the one from the homework set) with a bunch of extras added in the graph. Create a JPG and submit it.
  2. Create a data set by hand and plot it, and add some extras, including a text annotation. But this time, no pointy-clicky stuff using the Plot Tools window — you have to do it all by hand from the command line and put the commands into an M-file and submit that. (The moral: You need to know how to create a plot both ways, using the pointy-clicky stuff and without using it.)
  3. Create a two-item data set by hand and plot it, and make it look pretty. Create a PNG and submit it.
  4. Create single plot with 6 subplots arranged in a 2×3 grid. Create a PDF and submit it.

The philosophy here is that plotting stuff is fun, and there is a kind of immediate gratification when you create something visual that will hopefully serve as a source of low-hanging fruit for beginning MATLAB users. So students learning MATLAB should be introduced to the ins and outs of basic function and data visualization before they see much of anything else. And plotting also provides a convenient inroads to talking about M-files and other important MATLAB components.

So, how did it all go? I have to admit it didn’t start well. The homework set was due at 10:30 AM on Monday, and a good portion of the submissions had a timestamp in the neighborhood of 1:30AM Monday. There were several instances of students not reviewing their notes or watching the assigned tutorial videos before starting on the homework, which they did after midnight on the day it was due. So several teams took around 30-40 minutes just to get a basic plot of A = e^{-0.2t} \sin(2t) up on the screen — simply because they didn’t know how to create a vector, use the exp( ) command, or use the plot command, all of which we covered last week and in the homework.

I don’t say this to shame my students. Rather, I think it illustrates some important psychological points that play into teaching MATLAB, or any kind of technical computing topic, at this level:

  • Students who are at the entry level of using technical computing software believe that the software will adapt itself to what they are thinking, rather than the harsh reality of the other way around. When students want to plot e^x, they should just type it the way they write it. That may be the ideal case, but MATLAB just doesn’t work that way, and that’s a big problem for some people.
  • Most people who do any amount of computing will vouch for the importance of being able to surf the help files of a program to figure out what you need to do if you have something to get done. But freshmen in college have mostly never heard of such a thing. They are still making that transition from high school, where (typically) the teacher tells you what you need to know. Asking students to look up a command in a help system and then play around with it before coming into a lab session where they’ll be asked to do more of the same, is asking students to make a HUGE developmental leap. Or at least to begin to make it.
  • I’ve said it before, but the notion that today’s students are digitally native is both unfounded and dangerous. My students are not unintelligent, but many of them had no idea what a PNG was or how it was different from a JPG or PDF (this week’s homework asks them to do some research on that topic); many have never seen anything like a command line; a few of them didn’t even know how to attach something to an email to submit it. The so-called and self-appointed ed tech illuminati pushing this digital native idea have simply got to come back to reality so we can teach these ideas and skills rather than assume them.

Anyway, despite the rocky start, we ended well. I softened my stance, just this once, on giving out hints and how-to advice for a couple of things, and the infusion of progress begat more progress, this time by the students themselves. Most people didn’t make it past the second problem but everybody turned in reasonably good work by the deadline this morning. And by the end, teams were getting excited that they had figured some MATLAB out.

I think we can now move forward into next week, which is about working with data files and doing basic statistics, with confidence and with a little more understanding of what this class is all about. Here’s the homework!


Filed under Education, Educational technology, MATLAB, Teaching, Technology