Tuesday, 19 March 2013

Maths on Steroids (Accelerated Mathematics): Tutes 1 and 2

Orrite guys, some of you were asking me about the last tute question in the MATLAB prac sheet today, so I'll try to walk you through how to do the "simple programming" task there. But first, let me tell you a little story.

First week of my first year at uni, I learned a little something called row reduction. Naturally, the first thing that I did was to ignore the lecturer (sorry Paul (*)), and fiddle around with some matrices. And, here's what I did: \begin{align}&\left[\begin{array}{rrr|l} 1 & 0 & 0 & 2 \\ 0 & 1 & 0 & 2 \\ 0 & 0 & 1 & 2 \end{array}\right] \begin{array}{l} R_1\rightarrow R_1-R_2 \\ R_2\rightarrow R_2-R_3 \\ R_3\rightarrow R_3-R_1\end{array} \sim \left[\begin{array} {rrr|l} 1 & -1 & 0 & 0 \\ 0 & 1 & -1 & 0 \\ -1 & 0 & 1 & 0 \end{array} \right]. \end{align} So, that was pretty innocuous right? I mean, all I did was to be a fully-sick efficient guy and do a few row-reductions together; what could possibly go wrong?

And yet, something is very wrong.

I mean, I started off with this augmented matrix that had a unique solution of \(x=y=z=2\) and winded up with a system of equations that has \(\infty\) many solutions (you can check this by getting the row echelon form of this latter matrix)! Being a totally cocky first year, I was all like...oh man oh man oh man oh man, I broke maths.

Soooo, for any one in my tute who's reading this - see if you can figure out what I screwed up, and see if you can clearly articulate your idea/s to me before/during our next tute. =)

Onto this week's Lab sheet exercise 4!

You played around with the following rowswap function today in MATLAB:

function [B,E]=rowswap(A,x,y)
    % this function performs a row swap operation and creates the
    % associated elementary matrix.

And exercise 4 requires you to tweak this function so that this becomes a column-swapping function instead. So let's first try to understand these lines.

1. The very first line:

"function [\(B,E\)]=rowswap(\(A,x,y\))" 

has always been the most inscrutable one to me. Now, I am not programmer and I can only tell you what this practically means. Basically, it tells Matlab that this m-file is going to be a function called rowswap. And that this rowswap function takes in 3 inputs: \(A, x, y\) and is capable of spitting out two things: \(B\) and \(E\). We saw from the lab that \(A\) is a matrix (in our case, a \(4\times 4\) matrix) and we tell the function which rows we want to swap by specifying \(x\) and \(y\).

2,3. The next two lines have a percentage sign out the front and are commented out. That is, MATLAB totes ignores these two lines.

4. In the fourth line, we create a new variable called temp (the name isn't important, I could have called it werewolfram but that'd take more letters to type) and we assign it to be \(A(x,:)\). So let's remind ourselves a little something that we learned last week.

Now, when we run this program, \(x\) is some number that we enter, like \(x=1\). Last week we saw that in MATLAB,
typing \(A(1,1)\) gives you the \((1,1)\)-th entry of the matrix \(A\),
typing \(A(1,2)\) gives you the \((1,2)\)-th entry of the matrix \(A\),
typing \(A(1,3)\) gives you the \((1,3)\)-th entry of the matrix \(A\), and so forth.

We also saw that the colon \(:\) has to do with producing vectors of integers. For example, typing \(1:9\) into MATLAB gives you the vector: \begin{align}[1\, 2\, 3\, 4\, 5\, 6\, 7\, 8\, 9],\end{align}
so it's perhaps not surprising that if you type in \(A(x,:)\), you should get you get the \(x=1\)th row of the matrix \(A\): \begin{align}[A(1,1)\, A(1,2)\, \ldots A(1,n)].\end{align} In short: the fourth line sets temp to be the \(x\)th row of \(A\).

5. If you followed the above point, it should be clear that the fifth line sets \(A(x,:)\) to be \(A(y,:)\), that is: we're replacing the \(x\)th row of \(A\) with the \(y\)th row of \(A\). So, this achieves half of our goal, which is to switch rows \(x\) and \(y\).

6. Now, obviously we can't just replace the \(y\)th row of \(A\) with the (current) \(x\)th row of \(A\), because we've just replaced the \(x\)th row of \(A\) in the last step. But thankfully we've got a backup of the original \(x\)th row of \(A\) stored as temp. And so the sixth line is just to replace the \(y\)th row of \(A\) with temp.

7-10. I'm not going to spell out the specific details of each step because you guys are all smart enough to figure this out. But the aim of these steps is to create a matrix called \(E\) that corresponds to the elementary matrix that does a row swap between rows \(x\) and \(y\) when you multiply a matrix on the left by \(E\). Oh, and if you're thrown off by what \(size(A,1)\) does, go to a lab, type in a (preferably non-square) matrix \(A\) and type \(size(A)\) and \(size(A,1)\) and \(size(A,2)\) and see what you get.

11. In this last step, we set \(B\) to be the row-swapped matrix \(A\) that we've produced in lines 4-6, and this is one of the two matrices that the rowswap function outputs (we mentioned this in line 1).

Now, you've been asked to turn this whole thing into a column-swapping function, well, before I tell you how to do it, you should really have a think about how you might do this. In fact, at the end of my second year, I decided to learn some topology, so I went to the library and found a nice looking book and started reading the definitions. Being the typical maths book, it's got a section of definitions, then some examples (actually, I might be wrong about this, it might have been one of those rare books that gives motivating examples before definitions) followed by some theorems. And I just recall reading the statement of every theorem and then proving/trying to prove the result myself before reading their proof.

And you learn so much from doing this. Because often you'll find that perhaps your proof is more intuitive, but their's is cleaner. In fact, if you find that their proof is really unintuitive and seemingly belaboured, then that's especially useful: it tells you that either you've understood some of the concepts wrongly, or that their proof actually generalises much more easily in a more general framework than the particular result that's given.

So...that was enough time for you to have figured out how to do it yourself, right?

The idea is to just go through every line, and switch the roles of the row-coordinates and column-coordinates. Specifically, replace \(A(x,:)\) with \(A(:,x)\), and similarly for the matrix \(E\). Also, take care with what's going on with the \(size(\cdot)\) function - it shouldn't make a difference for a square matrix, but it will for a non-square matrix.

We finish this section with a few small remarks:

1. Every line of the code ends with a semi-colon. Is this necessary? Maybe you can try to run a version of the code where this semi-colon is taken away. Does the code still work? And if it still works, then what's changed?

2. Also, having now understood this code, you should realise that it seems to be a bit inefficient. In the sense that you should be able to do the whole thing without actually writing lines 4-6, because in lines 7-10 you produce an elementary matrix whose role is to switch rows \(x\) and \(y\). In fact, the following code should suffice:

function [B,E]=rowswap(A,x,y)

So, why isn't it written this way? Maybe it's easier for you guys to figure out what's going on in the code without having to deal with the \(size\) function straight up? Maybe the guy who wrote the sheet didn't think of this? Whatever the case, I think that the original code is possibly more...computer-friendly. And I mean this in the sense that the original algorithm requires computers to do fewer computation. But yea, whatevs...that's more of a random remark. =)

Allllso...some additional shameless advertising (that I'm doing outside of my duties as your tutor, much as writing up this stuff is outside of my duties as your tutor :P): I'm helping to start up a new Bible study thingy on Tuesday arvos (especially for peeps who can speak Chinese, I guess? Coz some of the students who are currently in it are a lot more comfortable with Mandarin), so if you're interested you can always contact me?

(*) you guys are thinking of the wrong Paul (**), I meant Paul Pearce,
(**) I hope that you guys are thinking of the wrong Ron Paul. =P

No comments:

Post a Comment