You are on page 1of 4

EL PROGRAMA

using System;
using System.Collections.Generic;
using System.Text;

namespace Proyecto
{
class Program
{
static void Main(string[] args)
{
Sample1();
}
static void Sample1()
{

/* Solucionar el siguiente sistema de ecuaciones lineales

x+y+z=6 |1116|

x+z=4 |1014|

x+y=3 | 1 1 0 3 | */
double[,] a = new double[3, 4];
Console.WriteLine("\n * Ingrese los elementos de la Matriz * \n");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 3; j++)
{
Console.Write("\tIngrese el elemento [{0},{1}] : ", i, j);
a[i, j] = double.Parse(Console.ReadLine());
}

} Console.Write("\n");

double[] r = new double[3];

ShowMatrix(a, "Eliminación de Gauss-Jordan Aplicada a Números Reales");

if (LinearEquationsSolver.GaussianElimination(a, r))

ShowSolution(r);

else

Console.WriteLine("No es un sistema de ecuaciones lineales");


Console.ReadLine();

#region formated output

static void ShowMatrix(double[,] a, string Title)


{

Console.WriteLine(Title + '\n');
for (int i = 0; i <= a.GetUpperBound(0); i++)
{

Console.Write('|');

for (int j = 0; j <= a.GetUpperBound(1); j++)


{

Console.Write(ToStringSign(a[i, j]));

Console.Write(" | \n");

Console.WriteLine('\n');

static void ShowSolution(double[] r)


{

Console.WriteLine("Solución por Eliminación Gaussiana");

for (int i = 0; i <= r.GetUpperBound(0); i++)


{

Console.WriteLine(ToStringSign(r[i]));

Console.WriteLine("\n");

static private string ToStringSign(double v)


{

if (v < 0) return ' ' + v.ToString(); else return " " + v.ToString();

#endregion
}
}
LA CLASE
using System;
using System.Collections.Generic;
using System.Text;

namespace Proyecto
{
class LinearEquationsSolver
{
public static bool GaussianElimination(double[,] a, double[] r)
{

double t, s;

int i, l, j, k, m, n;

try
{

n = r.Length - 1;

m = n + 1;

for (l = 0; l <= n - 1; l++)


{

j = l;

for (k = l + 1; k <= n; k++)


{

if (!(Math.Abs(a[j, l]) >= Math.Abs(a[k, l]))) j = k;

if (!(j == l))
{

for (i = 0; i <= m; i++)


{

t = a[l, i];

a[l, i] = a[j, i];

a[j, i] = t;

for (j = l + 1; j <= n; j++)


{

t = (a[j, l] / a[l, l]);


for (i = 0; i <= m; i++) a[j, i] -= t * a[l, i];

r[n] = a[n, m] / a[n, n];

for (i = 0; i <= n - 1; i++)


{

j = n - i - 1;

s = 0;

for (l = 0; l <= i; l++)


{

k = j + l + 1;

s += a[j, k] * r[k];

r[j] = ((a[j, m] - s) / a[j, j]);

return true;

catch
{

return false;

}
}
}

You might also like