Working on a function that performs gaussian elemination

I am working on a matlab function that can perform gaussian elimination on a matrix of any size
Currently I have this
function x = gauss(A,b)
% This function performs the Gauss elimination without pivoting
%
% x = GAUSS(A, b)
[n,n] = size(A);
% Check for zero diagonal elements
if any(diag(A)==0)
error('Division by zero will occur; pivoting not supported')
end
% Forward elimination
for row=1:n-1
for i=row+1:n
factor = A(i,row) / A(row,row);
for j = row:n
A(i,j) = A(i,j) - factor*A(row,j);
end
b(i) = b(i) - factor*b(row);
end
A_and_b = [A b]
end
% Backward substitution
x(n) = b(n) / A(n,n);
for row = n-1:-1:1
sums = b(row);
for j = row+1: n
sums = sums - A(row,j) * x(j);
end
x(row) = sums / A(row,row);
end
I have my function a and b defined in the command window but when I run the function in the command window I get an error message saying
Error using size
Too many output arguments.
Error in gauss (line 5)
[n,n] = size(a,b)
I am not sure how to fix this as I am not very familiar with matlab. How do I get rid of this error?

1 comentario

Thank you very much, I am now able to get an output but my matrix looks like this
a_and_b =
1 2 -1 0 1
0 0 0 -1 -3
0 NaN NaN Inf Inf
0 NaN NaN NaN NaN
ans =
NaN NaN NaN NaN
Any suggestions on how to correct this and turn the output into real values?

Iniciar sesión para comentar.

Respuestas (1)

Your error message shows
[n,n] = size(a,b)
where you have passed two arguments in to size(). When you pass two arguments in to size() you can only have one output.
The code you posted does it right,
[n, n] = size(A)

5 comentarios

Thank you very much, I am now able to get an output but my matrix looks like this
a_and_b =
1 2 -1 0 1
0 0 0 -1 -3
0 NaN NaN Inf Inf
0 NaN NaN NaN NaN
ans =
NaN NaN NaN NaN
Any suggestions on how to correct this and turn the output into real values?
What were the A and b matrices with which you called your function?
John D'Errico
John D'Errico el 3 de Feb. de 2016
Editada: John D'Errico el 3 de Feb. de 2016
To answer your followup question, why you get this:
a_and_b =
1 2 -1 0 1
0 0 0 -1 -3
0 NaN NaN Inf Inf
0 NaN NaN NaN NaN
It looks like your "A" matrix had a zero pivot. The NaNs result from a 0/0 operation. (A guess, since we do not know the matrix A.) But that is what I would expect to see if you got that result from a Gaussian elimination that did not employ pivoting.
This may be an indication that your matrix is singular, or it merely may be a carefully chosen matrix that is non-singular. (I am fairly sure it was carefully chosen by your instructor.)
That is the matrix I am working with, do you think I need to implement code for pivoting to make it work because I know the result is intended to produce values. Thanks again

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Feb. de 2016

Comentada:

el 4 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by