MATLAB For Dummies, 2nd Edition
Book image
Explore Book Buy On Amazon

You can perform reduction using MATLAB, and doing so requires only a couple of steps. Reduction lets you see the structure of what a matrix represents, as well as to write solutions to the system. MATLAB provides the rref() function to produce the Reduced Row Echelon Form (RREF). There is an interesting tool that you can use to see the steps required to produce RREF using any matrix as input.

The first step is to create the matrix. In this case, the example uses a magic square. Type A = magic(5) and press Enter. The magic() function will produce a magic square of any size for you. The output you see is

A =
 17 24  1  8 15
 23  5  7 14 16
  4  6 13 20 22
 10 12 19 21  3
 11 18 25  2  9

The second step is to perform the reduction. Type rref(A) and press Enter. Any nonsingular matrix will reduce to identity, as follows:

ans =
  1  0  0  0  0
  0  1  0  0  0
  0  0  1  0  0
  0  0  0  1  0
  0  0  0  0  1

You can use rref() to solve linear equations. In this case, if A*x=y and y=[1;0;0;0;0], then B=rref([A,y]) solves the equation. The following steps demonstrate how this works:

  1. Type y=[1;0;0;0;0]; and press Enter.

  2. Type A=magic(5); and press Enter.

  3. Type B=rref([A,y]) and press Enter.

    You see the following output:

    B =
    1.0000  0  0  0  0 -0.0049
      0 1.0000  0  0  0 0.0431
      0  0 1.0000  0  0 -0.0303
      0  0  0 1.0000  0 0.0047
      0  0  0  0 1.0000 0.0028
  4. Type x=B(:,6) and press Enter.

    You see the following output:

    x =
     -0.0049
     0.0431
     -0.0303
     0.0047
     0.0028

    At this point, you want to test the equation.

  5. Type A*x and press Enter.

    You see the following output:

    ans =
     0.9999
     -0.0001
     -0.0001
     -0.0001
     -0.0001

    Notice that the output values match the original value of y to within a small amount. In other words, the steps have proven the original equation, A*x=y, true.

About This Article

This article is from the book:

About the book authors:

Jim Sizemore is professor of physics and engineering at Tyler Junior College. For over 25 years, Sizemore has worked in the semiconductor and software industries as a process engineer, device physicist, and software developer. John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++.

This article can be found in the category: