myname0

хз

Dec 19th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1.  static  int V = 100;  
  2.  
  3.      static void Main(string[] args)
  4.     {
  5.         double begin = 0;
  6.         double end = V;
  7.         int countPartitions = 15;
  8.         double part = end - begin;
  9.         part /= countPartitions - 1;
  10.         double[] xArray = new double[countPartitions];
  11.         xArray[0] = begin;
  12.  
  13.         for (int i = 1; i < countPartitions; i++)
  14.             xArray[i] = xArray[i - 1] + part;
  15.        
  16.  
  17.         double[] yPreciseArray = new double[xArray.Length];
  18.         for (int i = 0; i < countPartitions; i++)
  19.             yPreciseArray[i] = getYExact(xArray[i]);
  20.        
  21.  
  22.         double[] yArray;
  23.  
  24.         double[][] matrix = getMatrix(xArray, part);
  25.         double[] vector = getVector(xArray);
  26.  
  27.         yArray = sweepMethod(matrix, vector);
  28.  
  29.         printArray(xArray, "Значения x:");
  30.         printArray(yPreciseArray, "Точные значения y:");
  31.         printArray(yArray, "Посчитанные значения y:");
  32.  
  33.         Console.WriteLine("Разница между значениями y (для метода прогонки)");
  34.         Console.Write("| ");
  35.         for (int i = 0; i < yArray.Length; i++)
  36.             Console.Write("{0.0000} ",Math.Abs(yArray[i] - yPreciseArray[i]));
  37.        
  38.         Console.WriteLine();
  39.     }
  40.  
  41.     public static double[][] getMatrix(double[] xArray, double h)
  42.     {
  43.         int count = xArray.Length;
  44.         double[][] matrix = new double[xArray.Length][];
  45.  
  46.         for (int i = 0; i < xArray.Length; i++)
  47.         {
  48.             double x = xArray[i];
  49.  
  50.             double a = (1 / Math.Pow(h, 2)) - (p(x) / (2 * h));
  51.             double b = (2 / Math.Pow(h, 2)) - q(x);
  52.             double c = (1 / Math.Pow(h, 2)) + (p(x) / (2 * h));
  53.  
  54.             if (i != 0)
  55.                 matrix[i][i - 1] = a;
  56.            
  57.             matrix[i][i] = -b;
  58.  
  59.             if (i != xArray.Length - 1)
  60.                 matrix[i][i + 1] = c;        
  61.         }
  62.  
  63.         return matrix;
  64.     }
  65.  
  66.     public static double[] getVector(double[] xArray)
  67.     {
  68.  
  69.         double[] vector = new double[xArray.Length];
  70.  
  71.         for (int i = 0; i < vector.Length; i++)
  72.             vector[i] = getF(xArray[i]);
  73.        
  74.         return vector;
  75.     }
  76.  
  77.     public static double getYExact(double x)
  78.     {
  79.         return x * x * x - V * x * x;
  80.     }
  81.  
  82.     public static double getF(double x)
  83.     {
  84.         return Math.Pow(x, 6)
  85.                 - V * Math.Pow(x, 5)
  86.                 + 3 * Math.Pow(x, 4)
  87.                 - 2 * V * Math.Pow(x, 3)
  88.                 + 6 * x
  89.                 - 2 * V;
  90.     }
  91.  
  92.     public static double p(double x)
  93.     {
  94.         return Math.Pow(x, 2);
  95.     }
  96.  
  97.     public static double q(double x)
  98.     {
  99.         return Math.Pow(x, 3);
  100.     }
  101.  
  102.     public static void printArray(double[] array, String message)
  103.     {
  104.         Console.WriteLine(message);
  105.         Console.Write("| ");
  106.         foreach (double value in array)
  107.             Console.Write("{0.0000} ", value);
  108.         Console.WriteLine("\n");
  109.     }
  110.  
  111.     public static double[] sweepMethod(double[][] matrix, double[] vector)
  112.     {
  113.         double[] answer = new double[vector.Length];
  114.         double[] arrayP = new double[matrix.Length + 1];
  115.         double[] arrayQ = new double[matrix.Length + 1];
  116.  
  117.         arrayP[1] = matrix[0][1] / -matrix[0][0];
  118.         arrayQ[1] = -vector[0] / -matrix[0][0];
  119.  
  120.         for (int i = 1; i < vector.Length; i++) {
  121.             if (i != vector.Length - 1)
  122.                 arrayP[i + 1] = matrix[i][i + 1] / (-matrix[i][i] - matrix[i][i - 1] * arrayP[i]);
  123.              else  arrayP[i + 1] = 0;
  124.            
  125.             arrayQ[i + 1] = (matrix[i][i - 1] * arrayQ[i] - vector[i]) / (-matrix[i][i] - matrix[i][i - 1] * arrayP[i]);
  126.         }
  127.  
  128.         answer[answer.Length - 1] = arrayQ[answer.Length];
  129.         for (int i = answer.Length - 2; i >= 0; i--)
  130.             answer[i] = arrayP[i + 1] * answer[i + 1] + arrayQ[i + 1];
  131.        
  132.         answer[0] = 0;
  133.         answer[answer.Length - 1] = 0;
  134.  
  135.         return answer;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment