Advanced Math/MATLAB

Advertisement


Question
Hi Scott,

Could you help me create a MATLAB program that reduces ANY 5x5 matrix to its reduced row echelon form without using rref? Below is the pseudocode... this is all I have and not sure how accurate.
%This function uses Gauss' Elimination to reduce any 5x % matrix to its
%reduced row echelon form
function ReducedREF(myMatrix)
lead := 0
rowCount := the number of rows in myMatrix
columnCount := the number of columns in myMatrix
for r < 0 < rowCount
   if columnCount < lead
       stop;
   end if
       i = r;
       while myMatrix;
           [i, lead] = 0;
           i = i + 1
           if rowCount = i then
               i = r
               lead = lead + 1
               if columnCount = lead then
                   stop
               end if
               end if
               end while
                   Swap rows i and r
                   Divide row r by myMatrix[r, lead]
                   for 0 <= i < rowCount do
                       if i ~= r do
                           Subtract myMatrix[i, lead] multiplied by row r from row i
                       end if
                       end for
                           lead = lead + 1
                       end for
                       end function

Thanks so much for all of your help.

Evelyn


Answer
I've never programmed in MATLAB, but after a few questions, I should learn.
I like to space two spaces in for each operation we're doing.
The 'end' should go in just one space farther than what it is ending.
I aslo like to put comments about the program in.
In C++, // signifies that the rest of the line is a comment.
Using MatLab, it looks like comments are started with %

As far as that, here is how I read the program:

function ReducedREF(myMatrix)
 rowCount := the number of rows in myMatrix
 columnCount := the number of columns in myMatrix
 for r = 0 to rowCount-1
   if columnCount < lead   % stops the program at the end
     stop;
    end if

   i = r;
   while myMatrix;
     [i, lead] = 0;            % checks for a 0 at the start
     i = i + 1
     if rowCount = i then
       i = r
       lead = lead + 1
       if columnCount = lead then   // if on last row of matrix, end
         stop
        end if
      end if
    end while

   Swap rows i and r
   Divide row r by myMatrix[r, lead]

   for 0 <= i < rowCount do
     if i ~= r do
       Subtract myMatrix[i, lead] multiplied by row r from row i
      end if
     end for
   lead = lead + 1
  end for
end function

Problems:
 When a 0 is checked for at the start, it needs to increment i until a row with a 0 is found.
 Once this has been found, swap the two rows.

Improvements
 I would use a for r=1 to rowCount.
 Where a zero was found at the critical element, swap the two rows.
 After this was done, proceed to zero the rest of the rows in that element out.
 I would also have a 5x10 matrix.  That would be a 5x5 added on at the end that started as a
 5x5 identity matrix.  This would give the inverse of the matrix as well.


Here is my version of pseudocode, almost in C++:
// m = matrix passed
// rc = row count
// cc = column count
constant
 rowCount
 colCount

void reduce_matrix (rc, cc)
 r=1;
 c=1;
 i=r;   // which row is being worked on

 for (r=1 to rowCount) do
   % finds which element in that column to work on
   while (m[i,c]=0)
     r++; // add one to r
     if r>rowCount
       c++; r=i;    // try the next column over
       if c>colCount
         exit with error "Not Invertible"
        end if
      end if
    end while

   if r<rowCount and c<colCount
     % this is done if the row being worked on has a zero at the key spot
     if r<>i SwapRows (row(i),row(r))   % swaps row i and row r

     for (j=r+1 to rowCount)
       Make_A_Zero (row(j), row(i))   % makes zeros below the nonzero element in proper column
      end for
    end if

Again, like any program, it has errors that would need to be taken out.
Also, I believe that it is like your code and only puts the matrix in row echelon form
since it does not worry about what's over the element.

Advanced Math

All Answers


Answers by Expert:


Ask Experts

Volunteer


Scott A Wilson

Expertise

I can answer any question in general math, arithetic, discret math, algebra, box problems, geometry, filling a tank with water, trigonometry, pre-calculus, linear algebra, complex mathematics, probability, statistics, and most of anything else that relates to math. I can even tell you it takes me over 2,000 steps to go a mile, but is that relevant?

Experience

Experience in the area; I have tutored people in the above areas of mathematics for almost two years in AllExperts.com. I have tutored people here and there in mathematics since before I received a BS degree almost 25 years ago. In just two more years, I received an MS degree as well, but more on that later. I tutored at OSU in the math center for all six years I was there. Most students offering assistance were juniors, seniors, or graduate students. I was allowed to tutor as a freshman. I tutored at Mathnasium for well over a year. I worked at The Boeing Company for over 5 years. I received an MS degreee in Mathematics from Oregon State Univeristy. The classes I took were over 100 hours of upper division credits in mathematical courses such as calculus, statistics, probabilty, linear algrebra, powers, linear regression, matrices, and more. I graduated with honors in both my BS and MS degrees. Past/Present Clients: College Students at Oregon State University, various math people since college, over 7,500 people on the PC from the US and rest the world.

Publications
My master's paper was published in the OSU journal. The subject of it was Numerical Analysis used in shock waves and rarefaction fans. It dealt with discontinuities that arose over time. They were solved using the Leap Frog method. That method was used and improvements of it were shown. The improvements were by Enquist-Osher, Godunov, and Lax-Wendroff.

Education/Credentials
Master of Science at OSU with high honors in mathematics. Bachelor of Science at OSU with high honors in mathematical sciences. This degree involved mathematics, statistics, and computer science. I also took sophmore level physics and chemistry while I was attending college. On the side I took raquetball, but that's still not relevant.

Awards and Honors
I earned high honors in both my BS degree and MS degree from Oregon State. I was in near the top in most of my classes. In several classes in mathematics, I was first. In a class of over 100 students, I was always one of the first ones to complete the test. I graduated with well over 50 credits in upper division mathematics.

Past/Present Clients
My clients have been students at OSU, people nearby, friends with math questions, and several people every day on the PC, and you're probably make one more.

©2012 About.com, a part of The New York Times Company. All rights reserved.