- #1
- 1,231
- 0
I wrote this in September so that I could do matrix row reduction on my TI-89. It works, usually, but sometimes it doesn't row reduce all the way. Any ideas what I've done wrong? Or is it just some strange quirk of the calculator?
If you don't know TI-89 code, it is self explanatory except for a few built in matrix functions which I explained with C++ style comments.
The matrix I am trying to do is
1 -1 0 5
-1 1 5 2
0 1 1 0
The solution of this matrix is
1 0 0 18/5
0 1 0 -7/5
0 0 1 7/5
But my function, solvemat(x), spits this out instead
1 1/4 0 13/4
0 5/4 0 -7/4
0 -1/4 1 7/4
If I do solvemat(solvemat(x)) it finishes the row reduction and returns the correct result. And the strangest thing is that if I alter the "1" in position (2, 2) of the original matrix x, the program runs correctly even if I only altered the 1 to, say, 1.000001.
If you don't know TI-89 code, it is self explanatory except for a few built in matrix functions which I explained with C++ style comments.
Code:
solvemat(x) // the name of the function. x is the matrix
Func //stating that it is a function and not a program
Local j, L, i, k, o // These are integers. Note:
//Sorry about the non-
//descriptive names, I had to type on an
//alphabetic non-qwerty
//keyboard so I didn't want them to be long
// j and L will be dealt with shortly
// i is the loop variable for columns
// k is the loop variable for rows
// o keeps track of the last row that properly starts with a 1
colDim(x) - 1 -> j //colDim(x) = number of columns in x. -> j
// means "assign to j"
rowDim(x) -> L //rowDim(x) = number of rows in x
0->o //o is initialized to 0
For i, 1, j, 1 // for(i = 1; i <= j; i++)
o+1 -> k
While x[k, i] = 0
k + 1 -> k
If k > L
Goto down //I know it's lame but there
EndWhile //is no "continue" in TI-89 basic
mRow(1/(x[k, i]), x, k) -> x
/* this built-in matrix solving function means
"multiply row k of matrix x
by 1/(x[k, i]) and put the result back into
matrix x." What I am doing here is putting
a 1 at the beginning of the row */
rowSwap(x, k, o+1) // swaps row k and row o+1
o+1 -> o //I could have done this a line earlier
// and had neater code, but I didn't
/* Now I have a row that is in the proper location, and it has
a 1 at the beginning of it. Now I will use that row to
eliminate all the nonzero elements of the matrix above and below the 1. */
For k, 1, L, 1 // for(k = 1; k <= L; k++) also L =
// rowDim(x) from earlier
If k = o // that's an 'o'
Goto d2
If x[k, i] != 0
mRowAdd(-x[k, i], x, o, k) -> x
/* This built-in matrix solving function means
"multiply row o of matrix x by -x[k, i] and add the
result to row k, and the resulting matrix goes back
into matrix x */
Lbl d2
EndFor
Lbl down
EndFor
Return x
EndFunc
The matrix I am trying to do is
1 -1 0 5
-1 1 5 2
0 1 1 0
The solution of this matrix is
1 0 0 18/5
0 1 0 -7/5
0 0 1 7/5
But my function, solvemat(x), spits this out instead
1 1/4 0 13/4
0 5/4 0 -7/4
0 -1/4 1 7/4
If I do solvemat(solvemat(x)) it finishes the row reduction and returns the correct result. And the strangest thing is that if I alter the "1" in position (2, 2) of the original matrix x, the program runs correctly even if I only altered the 1 to, say, 1.000001.