TI-89 Row Reduction Program Troubleshooting

AI Thread Summary
The TI-89 row reduction program experiences issues where it does not fully reduce matrices, often producing incorrect results unless run multiple times. The user suspects this may be due to rounding errors or a bug in the code. A key problem identified is the incorrect handling of the row swap function, which should include an assignment to update the matrix. Debugging suggestions include printing the matrix at various stages to track errors and checking for values close to zero rather than strictly equal to zero. The discussion highlights the importance of careful code review and testing for numerical stability in matrix operations.
0rthodontist
Science Advisor
Messages
1,229
Reaction score
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.

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.
 
Physics news on Phys.org
Probably the easiest way to debug it is to have it frequently print out the matrix, so you can watch what steps its performing, and in what order. That way, you can see when it's doing something wrong.
 
I notice that you have a conditional checking when x=0, what happens if due to round off errors x is very close to (as close as the calulator can get) but not equal to zero. If you were to check for x< (some very small number) You might resolve your problem.
 
The TI-89 has this function built-in using the simultanious equation solver flash-app that comes installed by default. After you enter your matrix (leave it in homogenious form - the solution side of the augmented matrix doesn't really make a difference) press solve, and after that, I forget the exact function key you have to press, but its labled something along the lines of "view". It will switch between different views, including one that you are looking for. I believe there's also a way to export the solution as a matrix.

As far as your program, it must be rounding error somewhere as others have already stated. I'll give the debugging a go once I get home (don't have my 89 with me).
 
Ah, no, it's not roundoff, I found the bug and forgot to post it. The bug is:
rowSwap(x, k, o+1)
It should be
rowSwap(x, k, o+1) -> x

Thanks evgeny, I didn't know that.
 
Back
Top