Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int V = 100;
- static void Main(string[] args)
- {
- double begin = 0;
- double end = V;
- int countPartitions = 15;
- double part = end - begin;
- part /= countPartitions - 1;
- double[] xArray = new double[countPartitions];
- xArray[0] = begin;
- for (int i = 1; i < countPartitions; i++)
- xArray[i] = xArray[i - 1] + part;
- double[] yPreciseArray = new double[xArray.Length];
- for (int i = 0; i < countPartitions; i++)
- yPreciseArray[i] = getYExact(xArray[i]);
- double[] yArray;
- double[][] matrix = getMatrix(xArray, part);
- double[] vector = getVector(xArray);
- yArray = sweepMethod(matrix, vector);
- printArray(xArray, "Значения x:");
- printArray(yPreciseArray, "Точные значения y:");
- printArray(yArray, "Посчитанные значения y:");
- Console.WriteLine("Разница между значениями y (для метода прогонки)");
- Console.Write("| ");
- for (int i = 0; i < yArray.Length; i++)
- Console.Write("{0.0000} ",Math.Abs(yArray[i] - yPreciseArray[i]));
- Console.WriteLine();
- }
- public static double[][] getMatrix(double[] xArray, double h)
- {
- int count = xArray.Length;
- double[][] matrix = new double[xArray.Length][];
- for (int i = 0; i < xArray.Length; i++)
- {
- double x = xArray[i];
- double a = (1 / Math.Pow(h, 2)) - (p(x) / (2 * h));
- double b = (2 / Math.Pow(h, 2)) - q(x);
- double c = (1 / Math.Pow(h, 2)) + (p(x) / (2 * h));
- if (i != 0)
- matrix[i][i - 1] = a;
- matrix[i][i] = -b;
- if (i != xArray.Length - 1)
- matrix[i][i + 1] = c;
- }
- return matrix;
- }
- public static double[] getVector(double[] xArray)
- {
- double[] vector = new double[xArray.Length];
- for (int i = 0; i < vector.Length; i++)
- vector[i] = getF(xArray[i]);
- return vector;
- }
- public static double getYExact(double x)
- {
- return x * x * x - V * x * x;
- }
- public static double getF(double x)
- {
- return Math.Pow(x, 6)
- - V * Math.Pow(x, 5)
- + 3 * Math.Pow(x, 4)
- - 2 * V * Math.Pow(x, 3)
- + 6 * x
- - 2 * V;
- }
- public static double p(double x)
- {
- return Math.Pow(x, 2);
- }
- public static double q(double x)
- {
- return Math.Pow(x, 3);
- }
- public static void printArray(double[] array, String message)
- {
- Console.WriteLine(message);
- Console.Write("| ");
- foreach (double value in array)
- Console.Write("{0.0000} ", value);
- Console.WriteLine("\n");
- }
- public static double[] sweepMethod(double[][] matrix, double[] vector)
- {
- double[] answer = new double[vector.Length];
- double[] arrayP = new double[matrix.Length + 1];
- double[] arrayQ = new double[matrix.Length + 1];
- arrayP[1] = matrix[0][1] / -matrix[0][0];
- arrayQ[1] = -vector[0] / -matrix[0][0];
- for (int i = 1; i < vector.Length; i++) {
- if (i != vector.Length - 1)
- arrayP[i + 1] = matrix[i][i + 1] / (-matrix[i][i] - matrix[i][i - 1] * arrayP[i]);
- else arrayP[i + 1] = 0;
- arrayQ[i + 1] = (matrix[i][i - 1] * arrayQ[i] - vector[i]) / (-matrix[i][i] - matrix[i][i - 1] * arrayP[i]);
- }
- answer[answer.Length - 1] = arrayQ[answer.Length];
- for (int i = answer.Length - 2; i >= 0; i--)
- answer[i] = arrayP[i + 1] * answer[i + 1] + arrayQ[i + 1];
- answer[0] = 0;
- answer[answer.Length - 1] = 0;
- return answer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment