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

/* Programa_Gauss_Seidel */

#include<math.h>
#include<conio.h>
#include <stdio.h>
#include<iostream>
#include<cmath>

int main() {
float matriz[100][100];
float pop[100][100];
float op[100][100];
float res[100][100];
float resultado;
int F, C, n; /* Filas F, Columnas C */
float pMin = 0;
char ans;

print (“\n Universidad Nacional Autonoma de Mexico ”);


print (“\n Facultad de Ingenieria ”);
print (“\n Division de Ciencias Basicas ”);
print (“\n Analisis Numerico ”);
print (“\n ”);
print (“\n Programa de GAUSS SEIDEL ”);
print (“\n ”);
print (“\n Elaboro: Garnica Magallan Diana Judith ”);
print (“\n ”);

/* Iniciar el programa preguntando las dimensiones de la matriz */


while (F != C || (F<= 0 && C <= 0) || (F> 100 && C > 100)) {
cout << “Ingresa el numero de columnas que deseas que tenga tu matriz" << endl;
cin >> C
cout << "Ingresa el numero de filas que deseas que tenga tu matriz"" << endl;
cin >> F;
if ((F <= 0 || C<= 0) || (F > 100 || C> 100)) {
cout << "Las dimensiones de la matriz son " << F << "x" << C << endl;
cout << “Introducir los numeros entre 0 y 99" << endl << "Presiona alguna tecla por
favor.” << endl;
getch();
system("CLS");
} else {

cout << "Presiona alguna tecla por favor." << endl;


getch();
system("CLS");

if (F!= C) {
cout << "Las dimensiones de la matriz son " << F<< "x" << C<< endl;
cout << “ERROR" << endl << "Presiona alguna tecla para continuar" << endl;
getch();
system("CLS");
} else {

cout << "Las dimensiones de la matriz son " << F << "x" << C << endl;
cout << "Las filas y columnas son Iguales" << endl;
cout << "Presiona una tecla para continuar" << endl;
getch();
system("CLS");
}
}

/* Valores de la matriz */
cout << "Las dimensiones de la matriz son " << F << "x" << C<< endl;
for (int i1 = 0; i1 < F; i1++) {
for (int i2 = 0; i2 < C; i2++) {
cout << "Introduce un numero en la posicion " << i1 + 1 << "," << i2 + 1 << ":" <<
endl;
cin >> matriz[i1][i2];
if (i1 != i2) {
pop[i1][i2 + 1] = matriz[i1][i2];
}
}
}

cout << "Introduce los resultados de cada ecuacion" << endl;


C = C + 1;
for (int i1 = 0; i1 < F; i1++) {
for (int i2 = 0; i2 < C; i2++) {
if (i2 == C - 1) {
cout << "=";
cin >> matriz[i1][i2];
} else {

if (matriz[i1][i2] >= 0) {
cout << " " << "+" << matriz[i1][i2] << "x" << i2 + 1 << " ";
} else {
cout << " " << matriz[i1][i2] << "x" << i2 + 1 << " ";
}
}

}
cout << endl;
}

/* Mostrar los valore de la matriz en forma de ecuaciones */


cout << “La ecuacion es:” << endl;
for (int i1 = 0; i1 < F; i1++) {
for (int i2 = 0; i2 < C; i2++) {
if (i2 == C - 1) {
cout << "=";
cout << " " << matriz[i1][i2] << " ";
pop[i1][0] = matriz[i1][i2];
} else {

if (matriz[i1][i2] >= 0) {
cout << " " << "+" << matriz[i1][i2] << "x" << i2 + 1 << " ";
} else {
cout << " " << matriz[i1][i2] << "x" << i2 + 1 << " ";
}
}
}
cout << endl;
}

int Matrizidentidad[100];
float suma[100];
cout << “Se usa la matriz identidad de la matriz recien creada" << endl;
for (int i1 = 0; i1 < F; i1++) {
suma[i1] = 0;
for (int i2 = 0; i2 < C- 1; i2++) {
if (i1 == i2) {
cout << “La posicion " << i1 + 1 << "," << i2 + 1 << ": ";
cout << matriz[i1][i2];
pop[i1][C] = matriz[i1][i2];
cout << endl;

// cout<<pop[i1][0]<<"\t";
// cout<<pop[i1][C]<<endl;
} else {

if (i1 != i2) {
suma[i1] += matriz[i1][i2];
}
}

}
for (int i2 = 0; i2 < C - 1; i2++) {
if (i1 == i2) {
if (suma[i2] > matriz[i1][i2]) { // Si los 3 valores son 1 entonces la matriz es
diagonalmente dominante
id[i1] = 0;
// cout<<endl<<"Identidad "<<id[i1];
} else {

if (suma[i2] <= matriz[i1][i2]) {


id[i1] = 1;
// cout<<endl<<"Identidad "<<id[i1];
}
}
}
}
}

int dom = 0;
for (int i = 0; i < F; i++) {
if (id[i] == 1) {
dom = dom + 1;
}
}

if (dom >= F) {
cout << endl << "La matriz es diagonalmente dominante" << endl;
} else {
if (dom < F) {
cout << endl << "La matriz no es diagonalmente dominante" << endl;
}
}

getch();
system("CLS");

// /* Error minimo */
do {
cout << “¿Deseas ingresar un porcentaje? Si es asi aprieta la tecla S, en caso
contrario la tecla N ” << endl;
cin >> ans;
if ((ans == 'S') || (ans == 's')) {
do {
cout << "Ingresa un porcentaje: " << endl;
cin >> pMin;
if (pMin < 0) {
cout << “ERROR: Ingresa un porcentaje valido" << endl;
cout << "Presiona alguna tecla para continuar" << endl;
getch();
system("CLS");
}
} while (pMin < 0);
} else {
if ((ans == 'N') || (ans == 'n')) {
cout << "No ingresate porcentaje” << endl;
}
if ((ans != 's') && (ans != 's') && (ans != 'N') && (ans != 'n')) {
cout << "Introduce una letra valida" << endl;
getch();
system("CLS");
}
}
} while ((ans != 's') && (ans != 's') && (ans != 'N') && (ans != 'n'));

cout << "Presiona alguna tecla para continuar" << endl;


getch();
system("CLS");

cout << "Operaciones a ejecutarse” << endl;


for (int i1 = 0; i1 < filasM; i1++) {
n = i1;
cout << "( ";
for (int i2 = 0; i2 <= C; i2++) {
if ((i2 != 0) && (i2 != C)) {
if ((i1 == n) && (i2 == n + 1)) {
/* Si la fila vale n y la columna vale n+1, el programa no mostrara nada */
} else {

if (pop[i1][i2] >= 0) {
cout << "-" << pop[i1][i2] << "x" << i2 << " ";
op[i1][i2] = pop[i1][i2];
} else {
cout << "+" << (-1 * pop[i1][i2]) << "x" << i2 << " ";
op[i1][i2] = pop[i1][i2];
}
}
} else {
if (i2 == C) {
cout << ") / " << pop[i1][i2];
op[i1][i2] = pop[i1][i2];
} else {
cout << pop[i1][i2] << " ";
op[i1][i2] = pop[i1][i2];
}
}
}
cout << endl;
}

/* Dependiendo de los valores que tienen las variables, se realizaran las siguientes
operaciones */
int a = 2;
int i = 0, vi = 0;
float resX[100][100];
float errorAprox = pMin + 1, act, ant;
int pos1, pos2;

if ((ans == 'n') || (ans == 'N')) {


for (int i1 = 0; i1 < 2; i1++) {
cout << "Iteracion " << i1 + 1 << ": " << endl;
for (int i2 = 0; i2 < F; i2++) {
n = i2;
for (int i3 = 0; i3 <= C; i3++) {
if ((i2 == n) && (i3 == n + 1)) {
}

if (i3 == 0) {
resX[i1][i3] = resX[i1][i3] + (op[i2][i3]);
} else {

if (i3 == C) {
resX[i1][i3] = resX[i1][i3] / op[i2][i3];
pos1 = i2;
pos2 = i3;
} else {

if (i3 != i2) {
if ((i1 == 0) && (i2 == 0)) {
resX[i1][i3] = resX[i1][i3] + (op[i2][i3] * vi);
}
if (i1 >= 1) {
if (i2 >= 0) {
res[i1][i3] = resX[i1][i3] + (op[i2][i3] * resX[pos1][pos2]);
}
}
}
}
}

}
cout << "Resultado x" << i2 + 1 << ": " << resX[i1][i2] << endl;

}
cout << endl;
}
}

if ((ans == 's') || (ans == 'S')) {


cout << "El error aproximado sera menor a " << pMin << "%" << endl;
do {
for (int i1 = 0; i1 < F; i1++) {
n = i1;
resultado = 0;
for (int i2 = 0; i2 <= C; i2++) {
if ((i1 == n) && (i2 == n + 1)) {

}
if (i2 == 0) {
res = res + (op[i1][i2]);
} else {

if (i2 == C) {
res = res / op[i1][i2];
pos1 = i1;
pos2 = i2;
} else {

if (i2 >= 1) {
if (i1 <= 0) {
res = res + (op[i1][i2] * vi);
} else {

if (i1 > 0) {
res = res + (op[i1][i2] * resX[pos1][pos2]);

/* cout<<"i1: "<<i1<<endl;
cout<<"i2: "<<i2<<endl;*/
}
}
}

}
}
}
res[i][i1] = res;
cout << resp[i][i1] << endl;
}
if (i > 0) {
cout << endl << "Iteracion " << i + 1 << endl;
for (int j = 0; j < C; j++) {
anterior = resp[i - 1][j];
actual = resp[i][j];
errorAprox = ((actual - anterior) / actual) * 100;
cout << endl << "Respuesta Actual: " << actual << endl;
cout << endl << "Respuesta anterior: " << anterior << endl;
cout << endl << "Error Aproximado actual: " << errorAprox << endl;
}
} else {
cout << endl << "Iteracion " << i + 1;
cout << endl << "~" << endl;
}
i++;
} while (errorAprox > pMin);
}

cout << “Fin del programa”;


getch();
return 0;
}

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