Zach
Zach

Reputation: 131

Reduced Row Echelon of a Matrix containing unknown constants in Matlab

the problem:

    >> syms('a', 'b', 'c');

    >>A =

    [ -2, 3,  1, a]
    [  1, 1, -1, b]
    [  0, 5, -1, c]

    >>rref(A)

    ans =

    [ 1, 0, -4/5, 0]
    [ 0, 1, -1/5, 0]
    [ 0, 0,    0, 1]

The problem I'm having is that I need the answer to be in terms of a b and c, so I need something like:

    ans =
    [1, 0, 0, a+2b-c]
    [0, 1, 0, 3a-c]
    [0, 0, 1, a+b+c]

Is there any way to get Matlab to accomplish this?

Upvotes: 3

Views: 4820

Answers (3)

You want the elimination matrix that gives you rref(A).

But What is rref(A)?

UpperTri=rref(A)=[LastStep]...[Step3][Step2][Step1]*A.

That is a series of steps that reduces A to an upper triangle or the best one possible.

Matlab had,

[ 1, 0, -4/5, (3*b)/5 - a/5]
[ 0, 1, -1/5, a/5 + (2*b)/5]
[ 0, 0,    0,             1]

Then Matlab just rolled it up and you no longer have the steps. So it appears as:

    [ 1, 0, -4/5, 0]
    [ 0, 1, -1/5, 0]
    [ 0, 0,    0, 1]

If you want the series of steps you want the elimination matrix with the series of steps so recorded it using an Identity matrix. Instead of:

A = 
[ -2, 3,  1, a]
[  1, 1, -1, b]
[  0, 5, -1, c]

Use

>>  A = [ -2, 3,  1, 1 0 0; 1, 1, -1, 0 1 0;  0, 5, -1, 0 0 1]

A =

    -2     3     1     1     0     0
     1     1    -1     0     1     0
     0     5    -1     0     0     1

>> E=rref(A)

E =

    1.0000         0   -0.8000         0    1.0000   -0.2000
         0    1.0000   -0.2000         0         0    0.2000
         0         0         0    1.0000    2.0000   -1.0000

This is like [A][I], Now [rref Steps matrix]*[A][I]=[rref(A)][rref Steps matrix]

Now E=[rref(A)][rref Steps matrix] =

 E =

        1.0000         0   -0.8000         0    1.0000   -0.2000
             0    1.0000   -0.2000         0         0    0.2000
             0         0         0    1.0000    2.0000   -1.0000

By inspection E1=[rref Steps matrix]=

E1 =

         0    1.0000   -0.2000
         0         0    0.2000
    1.0000    2.0000   -1.0000

so now:

E1 =

         0    1.0000   -0.2000
         0         0    0.2000
    1.0000    2.0000   -1.0000

>> B=[a;b;c;]

B =

 a
 b
 c

>> B1=E1*B

B1 =

     b - c/5
         c/5
 a + 2*b - c

To check:

A1 = [ -2, 3,  1, a; 1, 1, -1, b;  0, 5, -1, c]

A1 =

[ -2, 3,  1, a]
[  1, 1, -1, b]
[  0, 5, -1, c]

>> A2=E1*A1

A2 =

[ 1, 0, -4/5,     b - c/5]
[ 0, 1, -1/5,         c/5]
[ 0, 0,    0, a + 2*b - c]

Note a + 2b - c =0, therefore c=a+2b, therefore sub c, (c/5)= ((a+2b)/5), and (b-c/5)=(5b-(a+2b))/5

therefore

[(-a+3*b)/5; (a+2*b)/5; a + 2*b - c ]

Upvotes: 1

Strategy Thinker
Strategy Thinker

Reputation: 375

Matlab actually gave the correct answer for your example, which may seem surprising as it does not depend on a, b or c; if you use another example, e.g.

B = [1, 2, a; 2, 1, b];
rref(B)

then the answer will depend on a and b as expected.


Here I just explain why the reduced row echelon form of your A is indeed

[ 1, 0, -4/5, 0]
[ 0, 1, -1/5, 0]
[ 0, 0,    0, 1]

by showing some of the important steps of the row reduction. Matlab starts with A, and reduces it to

[ 1, 0, -4/5, (3*b)/5 - a/5]
[ 0, 1, -1/5, a/5 + (2*b)/5]
[ 0, 0,    0,   a + 2*b - c]

it then divides the last row by (a + 2*b - c) to get

[ 1, 0, -4/5, (3*b)/5 - a/5]
[ 0, 1, -1/5, a/5 + (2*b)/5]
[ 0, 0,    0,             1]

and then it proceeds to subtract that row from the rows above it to give

[ 1, 0, -4/5, 0]
[ 0, 1, -1/5, 0]
[ 0, 0,    0, 1]

Matlab assumed that a + 2*b - c is not equal to zero (try replacing c with a + 2*b, then rref(A) will give you a different answer). I know of no way to force Matlab not to divide by a + 2*b - c, i.e. to make Matlab assume that a + 2*b + c may be equal to zero.

Upvotes: 1

bla
bla

Reputation: 26069

You just need to convert the problem to a relevant form in order to use rref properly, that is, instead of :

[1, 0, 0, a+2b-c]
[0, 1, 0, 3a-c]
[0, 0, 1, a+b+c]

where the x,y,z form the unity matrix block, write:

 syms x y z
 D =
[ 1, 2, -1, x]
[ 3, 0, -1, y]
[ 1, 1,  1, z]

rref(D)
ans =
[ 1, 0, 0,     (3*y)/10 - x/10 + z/5]
[ 0, 1, 0,       (2*x)/5 - y/5 + z/5]
[ 0, 0, 1, (3*z)/5 - y/10 - (3*x)/10]

Upvotes: 3

Related Questions