Sie sind auf Seite 1von 6

// ***Gauss Eliminaton*** //

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

namespace Gauss_elimination
{
class Program
{
static void Main(string[] args)
{
// Declare and Initialize the variable
int i, j, n, k;
float[,] a = new float[10, 10];
float[,] e = new float[10, 10];
float[] x = new float[10];
float[] c = new float[10];
float[] c1 = new float[10];
float[] d = new float[10];
float[,] b = new float[10, 10];

// To get the order of Matrix


Console.WriteLine("Enter the value of n:");
n = int.Parse(Console.ReadLine());

//To get RHS Constants


Console.WriteLine("Enter the RHS Constants:");
for (i = 0; i < n; i++)
c[i] = float.Parse(Console.ReadLine());

//Enter the Matrix Co=efficients one by one


Console.WriteLine("Enter the Co-efficients row wise:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
a[i, j] = float.Parse(Console.ReadLine());
}

// For Reducing below the diagonal elements to get zero


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

if ((k == 0) && (i == 2))


{
e[i, j] = ((a[i, j] / a[i, k]) * a[k, k]) - a[k, j];
c1[i] = ((c[i] / a[i, k]) * a[k, k]) - c[k];
}
if (k==1)
{
b[i, j] = ((e[i, j] / e[i, k]) * b[k, k]) - b[k, j];
d[i] = ((c1[i] / e[i, k]) * b[k, k]) - d[k];
}
if((k==0)&&(i==1))
{
b[i, j] = ((a[i, j] / a[i, k]) * a[k, k]) - a[k, j];
d[i] = ((c[i] / a[i, k]) * a[k, k]) - c[k];
}
}
}
}

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


{
for (j = 0; j < n; j++)
{
b[i, j] = a[i, j];
}
}

// Before using Back substitution


for (i = 0; i < n ; i++)
{
for (j = 0; j < n; j++)
{
Console.WriteLine(b[i, j]);

}
}
for (i = 0; i < n - 2; i++)
{
d[i] = c[i];
}
for (i = 0; i < n; i++)
{
Console.WriteLine(d[i]);
}

//Using Back Substitution


x[n - 1] = d[n - 1] / b[n - 1, n - 1];

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


{
i = n - k - 2;
if (k > 0)
{
n = n - 1;
}
for (j = i + 1; j < n; j++)
{
c1[i] = d[i] - (b[i, j] * x[j]);
c1[i] = c1[i] - (b[i, (j + 1)] * x[(j + 1)]);
x[i] = c1[i] / b[i, (j - 1)];
}
}
n = n + 1;
// To get the solution
Console.WriteLine("The Solution is:");
for (i = 0; i < n; i++)
{
Console.WriteLine("x({0})={1}", i, System.Math.Round( x[i],3));
}
Console.ReadLine();
}
}
}

// *** OUTPUT *** //


// *** Gauss Seidal ***//

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Declare and Initialize the variable
int i, j, n, k,t,ite;
float [,]a =new float[10,10];
float []x = new float[10];
float[] b = new float[10];
float c;

// To get the order of Matrix


Console.WriteLine("Enter the value of n:");
n = int.Parse(Console.ReadLine());

// To get Number of Iteration


Console.WriteLine("Enter the Number of Iteration:");
ite = int.Parse(Console.ReadLine());

//To get RHS Constants


Console.WriteLine("Enter the RHS Constants:");
for (i = 1; i<=n;i++ )
b[i] = int.Parse(Console.ReadLine());

//Enter the Matrix Co-efficients one by one


Console.WriteLine("Enter the Co-efficients :");
for (i = 1; i <=n; i++)
{
x[i] = 0;
for (j = 1; j <= n; j++)
a[i, j] = int.Parse(Console.ReadLine());
}

//Iteration Process
for (t = 1; t<= ite; t++)
{
Console.WriteLine(" ");
Console.WriteLine("Iteration:{0}", t);
Console.WriteLine("______________");
for (i = 1; i <= n; i++)
{
c = b[i];
for (j = 1; j <= n; j++)
{
if (i != j)
{
c = c - a[i, j] * x[j];
}
}
for (k = 1; k <= n; k++)
if (i == k)
x[i] = c / a[i, k];
}

//To get Solution


Console.WriteLine("The Solution is:");
for (i = 1; i <= n; i++)
Console.WriteLine("x({0})={1}", i, System.Math.Round(x[i],4));
}
Console.ReadLine();

}
}
}

// *** OUTPUT *** //

Das könnte Ihnen auch gefallen