Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A
- {
- public static int V = 12;
- static int n = 30;
- class Program
- {
- public static void Main(string[] args)
- {
- double begin = 0;
- double end = 12;
- double part = end - begin;
- part /= n - 1;
- double[] x = new double[n];
- double[][] a = new double[n][];
- for (int i = 0; i < n; i++)
- a[i] = new double[n];
- double[] b = new double[n];
- x[0] = begin;
- for (int i = 1; i < n; i++)
- {
- x[i] = x[i - 1] + part;
- }
- double[] yt = new double[n];
- for (int i = 0; i < n; i++)
- {
- yt[i] = Math.Pow(x[i], 2) + V;
- }
- double h = Math.Abs(x[0] - x[1]);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (j == 0 || j == n - 1)
- {
- if (i == j)
- a[i][j] = 1 + (h * getA(x[i], x[j])) / 2.0;
- else a[i][j] = (h * getA(x[i], x[j])) / 2.0;
- }
- else
- {
- if (i == j)
- a[i][j] = 1 + h * getA(x[i], x[j]);
- else
- a[i][j] = h * getA(x[i], x[j]);
- }
- }
- }
- for (int i = 0; i < n; i++)
- {
- b[i] = getF(x[i]);
- }
- double[] y = gaussMethod(a, b);
- print(x, "Значения x:");
- print(yt, "Y точное");
- print(y, "Y");
- Console.WriteLine("Точность");
- for (int i = 0; i < y.Length; i++)
- Console.Write("{0:F5} | ", Math.Abs(y[i] - yt[i]));
- }
- public static double getF(double x)
- {
- return Math.Pow(x, 2) + V + x * (Math.Pow(V, 4) / 4 + Math.Pow(V, 3) / 2
- + 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);
- }
- public static double getA(double x, double t)
- {
- return x * t + Math.Pow(x, 2) * Math.Pow(t, 2) + Math.Pow(x, 3) * Math.Pow(t, 3);
- }
- public static double[] gaussMethod(double[][] a, double[] b)
- {
- for (int i = 0; i < n; i++)
- {
- if (a[i][i] == 0)
- {
- if (!swap(a, b, i))
- {
- return new double[n];
- }
- }
- b[i] /= a[i][i];
- if (a[i][i] != 1)
- {
- divide(a[i], a[i][i]);
- }
- for (int j = i + 1; j < n; j++)
- {
- double s = a[j][i] / a[i][i];
- if (a[i][j] - s != a[i][j])
- {
- for (int k = 0; k < a[i].Length; k++)
- {
- a[j][k] -= a[i][k] * s;
- }
- b[j] -= b[i] * s;
- }
- }
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = n - i; j < n; j++)
- {
- b[n - i - 1] -= b[j] * a[n - i - 1][j];
- }
- }
- return b;
- }
- public static void divide(double[] line, double value)
- {
- for (int i = 0; i < line.Length; i++)
- {
- line[i] /= value;
- }
- }
- public static bool swap(double[][] a, double[] b, int index)
- {
- for (int i = index; i < n; i++)
- {
- if (a[i][index] != 0)
- {
- int swapIndex = index;
- int j = index;
- double element = a[index][index];
- while (j < b.Length)
- {
- if (element < Math.Abs(a[j][index]))
- {
- element = Math.Abs(a[j][index]);
- swapIndex = j;
- }
- j++;
- }
- if (swapIndex == n)
- {
- break;
- }
- double[] tmpRow = a[index];
- a[index] = a[swapIndex];
- a[swapIndex] = tmpRow;
- double tmp = b[index];
- b[index] = b[swapIndex];
- b[swapIndex] = tmp;
- return true;
- }
- }
- for (int i = index; i < n; i++)
- {
- if (a[index][i] != 0)
- {
- int swapIndex = index;
- double element = a[index][index];
- int j = index;
- while (j < n)
- {
- if (element < Math.Abs(a[index][j]))
- {
- element = Math.Abs(a[index][j]);
- swapIndex = j;
- }
- j++;
- }
- if (swapIndex == n)
- {
- break;
- }
- for (int k = 0; k < n; k++)
- {
- double tmp = a[k][index];
- a[k][index] = a[k][swapIndex];
- a[k][swapIndex] = tmp;
- }
- }
- return true;
- }
- return false;
- }
- public static void print(double[] array, String str)
- {
- Console.WriteLine("\n {0}", str);
- foreach (double i in array)
- Console.Write("{0:F5} | ", i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement