Advertisement
myname0

кв метод

Dec 20th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.86 KB | None | 0 0
  1. class A
  2.     {
  3.         public static int V = 12;
  4.         static int n = 30;
  5.         class Program
  6.         {
  7.  
  8.             public static void Main(string[] args)
  9.             {
  10.                 double begin = 0;
  11.                 double end = 12;
  12.  
  13.                 double part = end - begin;
  14.                 part /= n - 1;
  15.                 double[] x = new double[n];
  16.                 double[][] a = new double[n][];
  17.                 for (int i = 0; i < n; i++)
  18.                     a[i] = new double[n];
  19.                 double[] b = new double[n];
  20.                 x[0] = begin;
  21.  
  22.                 for (int i = 1; i < n; i++)
  23.                 {
  24.                     x[i] = x[i - 1] + part;
  25.                 }
  26.  
  27.                 double[] yt = new double[n];
  28.                 for (int i = 0; i < n; i++)
  29.                 {
  30.                     yt[i] = Math.Pow(x[i], 2) + V;
  31.                 }
  32.                 double h = Math.Abs(x[0] - x[1]);
  33.                 for (int i = 0; i < n; i++)
  34.                 {
  35.                     for (int j = 0; j < n; j++)
  36.                     {
  37.                         if (j == 0 || j == n - 1)
  38.                         {
  39.                             if (i == j)
  40.                                 a[i][j] = 1 + (h * getA(x[i], x[j])) / 2.0;
  41.                             else a[i][j] = (h * getA(x[i], x[j])) / 2.0;
  42.                         }
  43.                         else
  44.                         {
  45.                             if (i == j)
  46.                                 a[i][j] = 1 + h * getA(x[i], x[j]);
  47.                             else
  48.                                 a[i][j] = h * getA(x[i], x[j]);
  49.                         }
  50.                     }
  51.                 }
  52.                 for (int i = 0; i < n; i++)
  53.                 {
  54.                     b[i] = getF(x[i]);
  55.                 }
  56.  
  57.                 double[] y = gaussMethod(a, b);
  58.  
  59.                 print(x, "Значения x:");
  60.                 print(yt, "Y точное");
  61.                 print(y, "Y");
  62.  
  63.                 Console.WriteLine("Точность");
  64.                 for (int i = 0; i < y.Length; i++)
  65.                     Console.Write("{0:F5} | ", Math.Abs(y[i] - yt[i]));
  66.  
  67.             }
  68.  
  69.             public static double getF(double x)
  70.             {
  71.                 return Math.Pow(x, 2) + V + x * (Math.Pow(V, 4) / 4 + Math.Pow(V, 3) / 2
  72.                         + x * Math.Pow(V, 5) / 5 + x * Math.Pow(V, 4) / 3 + x * x * Math.Pow(V, 6) / 6 + x * x * Math.Pow(V, 5) / 4);
  73.  
  74.             }
  75.  
  76.             public static double getA(double x, double t)
  77.             {
  78.                 return x * t + Math.Pow(x, 2) * Math.Pow(t, 2) + Math.Pow(x, 3) * Math.Pow(t, 3);
  79.             }
  80.  
  81.             public static double[] gaussMethod(double[][] a, double[] b)
  82.             {
  83.                 for (int i = 0; i < n; i++)
  84.                 {
  85.                     if (a[i][i] == 0)
  86.                     {
  87.                         if (!swap(a, b, i))
  88.                         {
  89.                             return new double[n];
  90.                         }
  91.                     }
  92.                     b[i] /= a[i][i];
  93.                     if (a[i][i] != 1)
  94.                     {
  95.                         divide(a[i], a[i][i]);
  96.                     }
  97.                     for (int j = i + 1; j < n; j++)
  98.                     {
  99.                         double s = a[j][i] / a[i][i];
  100.                         if (a[i][j] - s != a[i][j])
  101.                         {
  102.                             for (int k = 0; k < a[i].Length; k++)
  103.                             {
  104.                                 a[j][k] -= a[i][k] * s;
  105.                             }
  106.                             b[j] -= b[i] * s;
  107.                         }
  108.                     }
  109.                 }
  110.                 for (int i = 0; i < n; i++)
  111.                 {
  112.                     for (int j = n - i; j < n; j++)
  113.                     {
  114.                         b[n - i - 1] -= b[j] * a[n - i - 1][j];
  115.                     }
  116.                 }
  117.                 return b;
  118.             }
  119.  
  120.             public static void divide(double[] line, double value)
  121.             {
  122.                 for (int i = 0; i < line.Length; i++)
  123.                 {
  124.                     line[i] /= value;
  125.                 }
  126.             }
  127.  
  128.             public static bool swap(double[][] a, double[] b, int index)
  129.             {
  130.                 for (int i = index; i < n; i++)
  131.                 {
  132.                     if (a[i][index] != 0)
  133.                     {
  134.                         int swapIndex = index;
  135.                         int j = index;
  136.                         double element = a[index][index];
  137.                         while (j < b.Length)
  138.                         {
  139.                             if (element < Math.Abs(a[j][index]))
  140.                             {
  141.                                 element = Math.Abs(a[j][index]);
  142.                                 swapIndex = j;
  143.                             }
  144.                             j++;
  145.                         }
  146.                         if (swapIndex == n)
  147.                         {
  148.                             break;
  149.                         }
  150.                         double[] tmpRow = a[index];
  151.                         a[index] = a[swapIndex];
  152.                         a[swapIndex] = tmpRow;
  153.  
  154.                         double tmp = b[index];
  155.                         b[index] = b[swapIndex];
  156.                         b[swapIndex] = tmp;
  157.                         return true;
  158.                     }
  159.                 }
  160.                 for (int i = index; i < n; i++)
  161.                 {
  162.                     if (a[index][i] != 0)
  163.                     {
  164.                         int swapIndex = index;
  165.                         double element = a[index][index];
  166.                         int j = index;
  167.                         while (j < n)
  168.                         {
  169.                             if (element < Math.Abs(a[index][j]))
  170.                             {
  171.                                 element = Math.Abs(a[index][j]);
  172.                                 swapIndex = j;
  173.                             }
  174.                             j++;
  175.                         }
  176.                         if (swapIndex == n)
  177.                         {
  178.                             break;
  179.                         }
  180.  
  181.                         for (int k = 0; k < n; k++)
  182.                         {
  183.                             double tmp = a[k][index];
  184.                             a[k][index] = a[k][swapIndex];
  185.                             a[k][swapIndex] = tmp;
  186.                         }
  187.                     }
  188.                     return true;
  189.                 }
  190.                 return false;
  191.             }
  192.  
  193.             public static void print(double[] array, String str)
  194.             {
  195.                 Console.WriteLine("\n {0}", str);
  196.                 foreach (double i in array)
  197.                     Console.Write("{0:F5} | ", i);
  198.  
  199.  
  200.  
  201.             }
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement