
Untitled
By: a guest on
Sep 21st, 2012 | syntax:
C# | size: 1.65 KB | hits: 16 | expires: Never
public static double[] Jacobi(double[,] Matrix, double[,] Vektor, double e)
{
double[,] factorMatrix = Matrix;
double[,] factorVektor = Vektor;
double eps = e;
int rowLength = factorMatrix.GetLength(0);
double[] previosVektor = new double[rowLength];
for (int i = 0; i < rowLength; i++)
previosVektor[i] = factorVektor[i, 0];
int iterationCount = 1;
while (true)
{
double[] currentVektor = new double[rowLength];
for (int i = 0; i < rowLength; i++)
{
currentVektor[i] = factorVektor[i, 0];
for (int j = 0; j < rowLength; j++)
{
if (i != j)
{
currentVektor[i] -= factorMatrix[i, j] * previosVektor[j];
}
}
currentVektor[i] /= factorMatrix[i, i];
}
double error = 0;
for (int i = 0; i < currentVektor.Length; i++)
{
error += Math.Abs(currentVektor[i] - previosVektor[i]);
}
iterationCount++;
Console.WriteLine("Iteration count: " + iterationCount);
Console.WriteLine("Error: " + error);
if (error < eps)
{
break;
}
previosVektor = currentVektor;
}
return previosVektor;
}