Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 21st, 2012  |  syntax: C#  |  size: 1.65 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         public static double[] Jacobi(double[,] Matrix, double[,] Vektor, double e)
  2.         {
  3.             double[,] factorMatrix = Matrix;
  4.             double[,] factorVektor = Vektor;
  5.  
  6.             double eps = e;
  7.  
  8.             int rowLength = factorMatrix.GetLength(0);
  9.             double[] previosVektor = new double[rowLength];
  10.  
  11.             for (int i = 0; i < rowLength; i++)
  12.                 previosVektor[i] = factorVektor[i, 0];
  13.  
  14.             int iterationCount = 1;
  15.  
  16.             while (true)
  17.             {
  18.                 double[] currentVektor = new double[rowLength];
  19.  
  20.                 for (int i = 0; i < rowLength; i++)
  21.                 {
  22.                     currentVektor[i] = factorVektor[i, 0];
  23.  
  24.                     for (int j = 0; j < rowLength; j++)
  25.                     {
  26.                         if (i != j)
  27.                         {
  28.                             currentVektor[i] -= factorMatrix[i, j] * previosVektor[j];
  29.                         }
  30.                     }
  31.  
  32.                     currentVektor[i] /= factorMatrix[i, i];
  33.                 }
  34.  
  35.                 double error = 0;
  36.  
  37.                 for (int i = 0; i < currentVektor.Length; i++)
  38.                 {
  39.                     error += Math.Abs(currentVektor[i] - previosVektor[i]);
  40.                 }
  41.  
  42.                 iterationCount++;
  43.                 Console.WriteLine("Iteration count: " + iterationCount);
  44.                 Console.WriteLine("Error: " + error);
  45.  
  46.                 if (error < eps)
  47.                 {
  48.                     break;
  49.                 }
  50.  
  51.                 previosVektor = currentVektor;
  52.             }
  53.  
  54.             return previosVektor;
  55.         }