rref matrix with an unkown variable

Given the matrix:
M = [1, 2, 3, 5;
2, 3, 4, 8;
3, 4, 5, c;
1, 1, 0, 2];
How does one code this in matlab so that it outputs the proper rref() matrix
I am expecting to get (did this by hand):
rref(M) = [1, 0, 0, 2;
0, 1, 0, 0;
0, 0, 0, c-11;
0, 0, 1, 1];
When solving for rref(M) using my code it always gives me this:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]

 Respuesta aceptada

Matt J
Matt J el 10 de Sept. de 2025
Editada: Matt J el 10 de Sept. de 2025
I am expecting to get (did this by hand):
Your expected result doesn't look right. For both row-echelon and reduced row-echelon form, the result must be triangular. If you just want (non-reduced) row-echelon form, you could use LU, e.g.,
syms c
M = [1, 2, 3, 5;
2, 3, 4, 8;
3, 4, 5, c;
1, 1, 0, 2];
[L,ref]=lu(M);
ref
ref = 

6 comentarios

Torsten
Torsten el 10 de Sept. de 2025
Editada: Torsten el 10 de Sept. de 2025
For c = 11, M is singular. So I'm surprised that rref(M) gives eye(4) independent of c.
M = [1, 2, 3, 5;
2, 3, 4, 8;
3, 4, 5, 11;
1, 1, 0, 2];
rref(M)
ans = 4×4
1 0 0 2 0 1 0 0 0 0 1 1 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
My answer by hand for ref is the same just rows 3 and 4 are switched. same for the rref.
I do have c=11
but i have been trying to figure out why it gives me a eye(4) but cant figure it out, i assumed it had to do with the variable.
this here is my code:
All the other matrixes work fine a,c,g and i but I cant get m to work as it should
% Define matrices A, C, G, I, and M
A = [2, -3, 1;
5, 1, 2];
C = [1, 2, 4];
G = [1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12];
I = [2, -1, 6;
3, 2, 4;
1, 10, -12;
6, 11, -2];
% Defining variable c
syms c; % Define c as a symbolic variable
M = [1, 2, 3, 5;
2, 3, 4, 8;
3, 4, 5, c;
1, 1, 0, 2];
% Calculate the reduced row echelon form
R_A = rref(A);
R_C = rref(C);
R_G = rref(G);
R_I = rref(I);
R_M = rref(M);
% Convert the RREF results to fractions (except M which is symbolic)
fraction_A = rats(R_A);
fraction_C = rats(R_C);
fraction_G = rats(R_G);
fraction_I = rats(R_I);
% Display the fraction representations of the RREF results
disp('Reduced Row Echelon Form of A:');
disp(fraction_A);
disp('Reduced Row Echelon Form of C:');
disp(fraction_C);
disp('Reduced Row Echelon Form of G:');
disp(fraction_G);
disp('Reduced Row Echelon Form of I:');
disp(fraction_I);
disp('Reduced Row Echelon Form of M:');
disp(R_M);
% MatLab is compiling rref differently then I did by hand.
% My result was:
% [1 0 0 2 ]
% |0 1 0 0 |
% |0 0 0 c-11|
% {0 0 1 1 ]
% Can only get it to do that if I manualy input all rref steps
% Finding c values that make M singular (determinant = 0)
det_M = det(M);
disp('Determinant of M:');
disp(det_M);
% Solving for c when determinant = 0
c_values = solve(det_M == 0, c);
disp('Values of c that make M singular:');
disp(c_values);
Matt J
Matt J el 10 de Sept. de 2025
Editada: Matt J el 10 de Sept. de 2025
i have been trying to figure out why it gives me a eye(4) but cant figure it out,
When all leading coefficients are non-zero, there is nothing else it can be. One of the rules of the reduced row-echelon form is that all other elements in a column with a non-zero diagonal coefficient must be zero. See also,
Since all diagonal coefficients are non-zero for this matrix, that gives you eye(4).
Paul
Paul el 11 de Sept. de 2025
Editada: Paul el 11 de Sept. de 2025
I'm going to guess that rref does not (and possibly cannot) account for all possible values of the symbolic variables. Somewhere along the way it's generating an expression that cancels out the c, even though such cancellation isn't correct for all possible values of c. For example
syms a b c
M = [a b;0 b]
M = 
If I was doing this problem by hand, I might proceed as follows with row operations
R = M;
R(2,:) = R(2,:)/b % 1
R = 
R(1,:) = R(1,:)/a % 2
R = 
R(1,:) = R(1,:) - R(2,:)*b/a % 3
R = 
That happens to be the same result as returned from rref, though I suspect rref got there a different way
rref(M)
ans = 
Of course step 1 is invalid if b = 0, but the SMT is happy to do it. We see the same effect with the example from the rref
A = [a b c; b c a; a + b, b + c, c + a]
A = 
rref(A)
ans = 
That result can't be correct for any combination of (a,b,c) s.t. a*c - b^2 = 0
rref(subs(A,[a,b,c],[1,1,1])) % Case A
ans = 
rref(subs(A,[a,b,c],[2,4,8])) % Case B
ans = 
Interestingly, assumptions do influence rref
assume(a*c == b^2)
rref(A)
ans = 
simplify(ans)
ans = 
But I don't see how that result can recover Case A

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 10 de Sept. de 2025

Comentada:

el 11 de Sept. de 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by