Вы находитесь на странице: 1из 4

UNIVERSIDAD AUTÓNOMA DEL ESTADO

DE MORELOS.
CENTRO DE INVESTIGACIÓN EN INGENIERÍA Y CIENCIAS APLICADAS.
MAESTRÍA EN SUSTENTABILIDAD ENERGÉTICA.

“Sistemas lineales: Método de Gauss-Jordan, Solución.”


#include<cmath>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
/*
M-->matriz aumentada
n-->numero de ecuaciones
*/
void gauss_jordan(double M[20][20],int n){
double may;//variable para almacenar el mayor de la columna k
int ind;//indice del mayor-->indice de may
double aux;
double pivote;

for(int k=0;k<n;k++){//recorrer columnas de la matriz reducida


may=abs(M[k][k]);
ind=k;
//recorrer filas de la columna k para buscar el indice del mayor
for(int l=k+1;l<n;l++){
if(may<abs(M[l][k])){
may=abs(M[l][k]);
ind=l;
}

//cambiar filas
if(k!=ind){
for(int i=0;i<n+1;i++){
aux=M[k][i];
M[k][i]=M[ind][i];
M[ind][i]=aux;
}

}
if(M[k][k]==0){
cout<<"no tiene solucion";
break;
}

Ing. Ángel Sánchez Cruz Dr. Antonio Rodríguez Martínez Métodos numéricos, C++
else{

for(int i=0;i<n;i++){//recorrer fila


if(i!=k){
pivote=-M[i][k];
for(int j=k;j<n+1;j++){//recorrer elementos de una fila

M[i][j]=M[i][j]+pivote*M[k][j]/M[k][k];
}
}
else{
pivote=M[k][k];
for(int j=k;j<n+1;j++){
M[i][j]=M[i][j]/pivote;
}
}
}
}

}
}
void ingresar_coeficientes(double M[20][20],int n){
cout<<"\nIngrese las ecuaciones para:"<<endl;
for(int i=0;i<n;i++){
cout<<"\t"<<"Fila "<<i+1<<" : "<<endl;
for(int j=0;j<n+1;j++){
cin>>M[i][j];
}
}
}
int main (int argc, char *argv[]) {

cout<<("\nSolucion de sistemas lineales");


cout<<("\nMETODO DE GAUSS-JORDAN\n");
cout<<("Autor: Angel Sanchez Cruz\n");
time_t tiempo = time(0);
char output[128];
struct tm *tlocal = localtime(&tiempo);
strftime(output,128,"Fecha: %d/%m/%y %H:%M:%S\n\n",tlocal);
cout<<("%s\n",output);

int n;
double M[20][20];
cout<<"Ingrese el numero de ecuaciones:";
cin>>n;
ingresar_coeficientes(M,n);

Ing. Ángel Sánchez Cruz Dr. Antonio Rodríguez Martínez Métodos numéricos, C++
cout<<"\nMatriz aumentada:"<<endl;
for(int i=0;i<n;i++){
for(int j=0;j<n+1;j++){
cout<<M[i][j]<<"\t";
}cout<<endl;
}
cout<<"\nMatriz reducida a ceros:"<<endl;
gauss_jordan(M,n);
for(int i=0;i<n;i++){
for(int j=0;j<n+1;j++){
cout<<M[i][j]<<"\t";
}cout<<endl;
}
cout<<"\nSolucion de incognitas:"<<endl;
for(int i=0;i<n;i++){
cout<<"X"<<i+1<<"="<<M[i][n]<<"\t";
}cout<<endl;
return 0;
}

Ing. Ángel Sánchez Cruz Dr. Antonio Rodríguez Martínez Métodos numéricos, C++
Ing. Ángel Sánchez Cruz Dr. Antonio Rodríguez Martínez Métodos numéricos, C++

Вам также может понравиться