Tuesday, October 04, 2005

Eliminacion Gaussiana CON pivoteo

Aqui les presento una nueva version del codigo de Matlab que ya antes habia publicado que implementa eliminacion Gaussina para resolver sistemas lineales del tipo Ax = b. Esta nueva version no es mas rapida que la anterior, pero el error relativo en los resultados es mucho menor que en la version anterior. El error que se obtiene con este metodo se debe en gran medida a que los resultados fueron obtenidos mediante calculos en representacion finita.

%**********************************************************
%Intercambia dos elementos de un arreglo en una dimension
%Recibe el arreglo y los indices de los elementos a ser intercambiados
%**********************************************************

function array = Swap(array,this,with)
temp = array(this);
array(this) = array(with);
array(with)= temp;
return

---------------------------------------------------------
%*********************************************************
%Implementa la sumatoria desde 'from' hasta 'to'
%Devuelve la suma
%*********************************************************
function [suma] = Sumatoria(from,to, A, x, i,index)

suma = 0;

for k=from:to
suma = suma + A(index(i),k)*x(index(k),1);
end

return

-------------------------------------------------
%**********************************************
%Implements backward substitution over an
%upper triangular matrix
%
%Input: Una matriz A triangular superior y un vector b
%Output: Solution vector
%*********************************************

function [flag,x] = solve_UTSP(A,b,index)
[x_rows,x_cols] = size(b);
[A_rows,A_cols] = size(A);
flag =1;
x = zeros(x_rows,x_cols);

for i=x_rows:-1:1
if abs(A(index(i),i)) < eps
flag = 0;
'Division by zero error'
break;
end
x(index(i),1) = (b(index(i),1)-SumatoriaP(i+1,A_cols,A,x,i,index))/A(index(i),i);
end
return

%************************************************
%Resuelve sistemas lineales utilizando eliminacion
%de Gauss con Pivoteo.
%Input: La matriz A y un vector b
%Output: El vector solucion
%************************************************

function [flag,x] = GE(A,b)

%Para trabajar con la matriz aumentada
A(:,end+1) = b(:,1);

flag =1; %Bandera indicando que no ha ocurrido ningun error
col=1; %Columna pivote
row=1; %inicializar fila pivote
nextRow = row+1;
[b_rowCant,b_colCant] = size(b); %tamano de b
[A_rowCant,A_colCant] = size(A); %tamano de A
index(1:A_rowCant,1) = 1:A_rowCant; %tamano del indice de filas de A

tic; %Comenzar a contar
for col=1:(A_colCant-1)
if abs(A(index(row),col)) < 0.01
[largestPivot,largestPivotIndex] = max(A(index(row):end,col));
largestPivotIndex=largestPivotIndex+(index(row)-1);
index = Swap(index,largestPivotIndex,row);
end

for rowIndex=1:(A_rowCant-row)
if abs(A(index(row),col)) < eps
flag = 0;
'Division by zero error'
return;
end
A(index(nextRow),:) = A(index(row),:)*(-A(index(nextRow),col)/A(index(row),col))+A(index(nextRow),:);
nextRow = nextRow + 1;
end
row = row+1;
col = col+1;
nextRow = row+1;
end

%Para regresar al sistema original
b = A(:,end);
A(:,end) = [];

%Resolver sistema triangular superior
[succesful,x] = solve_UTSP(A,b,index);

x(:,1) = x(index(:),1);
toc;

return

Claro este metodo no lo inventé yo este es solo mi versión del código. Este método fue inventado por Liu Hui y se hace referencia a él en el importantísimo libro chino "Jiuzhang suanshu" o "Los nueve capítulos en el arte de la matemática" que data del 263A.D.. Por otra parte el nombre que conocemos del método, se atribuye al famosísimo matemático alemán, C.F.Gauss, que hizo el mismo popular a finales del siglo 18.

Bueno, pues no se es que es mi opinión que estas cosas tambien son bonitas, tan bonitas como un poema o como una hermosa pintura, pero por alguna razon, no mucha gente se toma el tiempo de sentarse a apreciarlas. Pero, bueno por lo menos quisiera que la mayor cantidad de gente posible se tope al menos con la idea, que sepan al menos que estas cosas existen. Y ya que existen por que no compartirlas? Es como si todos pudieramos tener un Quijote, original, escrita con el mismo puño y letra de Cervantes o una la MonaLisa en la sala de nuestra casa. Quizas no lo parezca pero, si se detienen un segundo a ver lo que hay detras, se daran cuenta de lo inmensamente maravilloso que estas cosas pueden llegar a ser. Remember, "Life moves pretty fast, if you don't stop to look around once in a while, you could miss it".

10 Comments:

Blogger iketius@hotmail.com said...

Yo ahí sólo veo números, letras y MUCHO dolor de cabeza. ¡Odio a Gauss!
Leí esa frase no sé dónde hace no sé cuánto tiempo...¿Ves lo que pasa cuando estudias demasiado álgebra? Las neuronas se te derriten y no puedes recordar nada...

2:15 PM  
Anonymous Anonymous said...

ODIO A GUSS!!!!...TODOS LO ODIAN XQ LE HAC LA VIDA IMPOSIBLE A MUCHOSSS...JEJEJE...PRO IGUAL HAY Q STUDIAR ESA LEY JAJAJAJA!!!...X OTRA PARTE SE TE DERRITEN LAS NEURONASSSS!!..EL CEREBRO TODOO!! NAGUARA POBRES D NOSOTROS Q NOS AMARGAMOS LA EXISTENCIA STUDIANDO MATEMATIK JEJEJEJEJE

10:49 PM  
Blogger Adolfo said...

%metodo de gauss-pivoteo
clc
clear all
disp('este programa resuelve sistemas de ecuaciones')
disp('por el metodo de gauss-pivoteo');
A=input('dame la matriz aumentada del sistema: ');
dimen=size(A);n=dimen(1);B=A(1:n,1:n);aux=zeros(1,n+1);
if det(B)==0
disp('el sistema no tiene solucion o tiene infinidad de soluciones');
else
i=1;
while(i<=n-1)
k=i+1;
[pivoteo,renglon]=max(abs(A(i:n,i)))
aux=A(renglon+i-1,:)
A(renglon+i-1,:)=A(i,:)
A(i,:)=aux
while(k<=n)
A(k,:)=A(k,:)-A(k,i)*(A(i,:)/A(i,i));
k=k+1;
end
i=i+1;
end
i=n;
while(i>=2)
k=i-1;
while(k>=1)
A(k,:)=A(k,:)-A(k,i)*(A(i,:)/A(i,i));
k=k-1;
end
i=i-1;
end
for i=1:n;
x(i)=A(i,n+1)/A(i,i);
end
disp('la solucion del sistema es:');disp(x)
end

10:48 PM  
Blogger Unknown said...

hello I have on cuestion
what ecuations result matrices in funtion calculator?
comment here
http://foros.calculadoras.com.mx/foro_numerica/index.php?topic=6.0

5:51 PM  
Anonymous Anonymous said...

It's very simple to find out any matter on web as compared to books, as I found this post at this site.

Feel free to surf to my site: germany

2:22 PM  
Anonymous Anonymous said...

I have learn some good stuff here. Definitely price bookmarking for revisiting.
I surprise how much effort you set to create this sort of excellent informative site.


Feel free to surf to my blog :: retrobeurs

4:14 AM  
Anonymous Anonymous said...

Great post. I was checking continuously this blog and I'm impressed! Extremely helpful information specifically the last part :) I care for such information a lot. I was looking for this certain info for a long time. Thank you and best of luck.

Stop by my web site :: vakantiehuisjes huren

9:50 AM  
Anonymous Anonymous said...

My partner and I absolutely love your blog and find nearly all of your post's to be just what I'm looking for.
Would you offer guest writers to write content in your case?
I wouldn't mind writing a post or elaborating on many of the subjects you write regarding here. Again, awesome web site!

Feel free to visit my homepage - vakantieparken in frankrijk

1:12 AM  
Anonymous Anonymous said...

If some one desires to be updated with most recent technologies therefore he must be pay a visit this web site and be
up to date every day.

Also visit my weblog: luxe villa frankrijk

3:57 AM  
Anonymous Anonymous said...

Your way of describing all in this paragraph is actually good, every one be capable
of easily understand it, Thanks a lot.

Here is my blog; vakantiehuizen (luxevakantiehuizen.wordpress.com)

9:58 AM  

Post a Comment

<< Home




MP3 Player Turtorial