Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace kak_da_si_resha_zadachite_lyubo
- {
- class Point
- {
- public float X { get; set; }
- public float Y { get; set; }
- }
- class Program
- {
- const int Power = 3;
- static void Main()
- {
- }
- static float CalculateIntegral(Func<float, float> f, float a, float b, int n)
- {
- float step = Math.Abs(a - b) / n;
- float result = (b - a) / (2 * n);
- float rightPart = f(a);
- float sum = 0;
- for (int i = 1; i < n; i++)
- {
- sum += f(a + (step * i));
- }
- rightPart += 2 * sum;
- rightPart += b;
- return result * rightPart;
- }
- static float ApproximateSecondDerivativeLagrance(Point[] points, float x)
- {
- float secondDerivative = 0;
- for (int k = 0; k < points.Length; k++)
- {
- float product = 1;
- for (int i = 0; i < points.Length; i++)
- {
- if (i == k)
- {
- continue;
- }
- product *= points[k].X - points[i].X;
- }
- secondDerivative += (2 * points[k].Y) / product;
- }
- return secondDerivative;
- }
- static float CalculateFirstExpressionDerivativeLagrance(Point[] points, int k, int power)
- {
- float product = 0;
- for (int i = 0; i < power + 1; i++)
- {
- Point point = points[i];
- if (i == k)
- {
- continue;
- }
- if (product == 0)
- {
- product = 1;
- }
- product *= points[k].X - points[i].X;
- }
- return points[k].Y / product;
- }
- static float CalculateSecondPartDerivativeLagrance(Point[] points, float x, int k, int power)
- {
- float result = 0;
- for (int i = 0; i < power + 1; i++)
- {
- if (i == k)
- {
- continue;
- }
- float product = 0;
- for (int j = 0; j < power + 1; j++)
- {
- if (j == i || j == k)
- {
- continue;
- }
- if (product == 0)
- {
- product = 1;
- }
- product *= x - points[j].X;
- }
- result += product;
- }
- return result;
- }
- static float ApproximateFirstDerivativeLagrance(Point[] points, float x)
- {
- float approximation = 0;
- for (int k = 0; k < points.Length; k++)
- {
- float firstExpressionLagrance = CalculateFirstExpressionDerivativeLagrance(points, k, points.Length - 1);
- float secondExpressionLagrance = CalculateSecondPartDerivativeLagrance(points, x, k, points.Length - 1);
- approximation += firstExpressionLagrance + secondExpressionLagrance;
- }
- return approximation;
- }
- static string LeastSquares(Point[] points, int power)
- {
- if (power == 0)
- {
- return (points.Sum(point => point.X) / points.Length).ToString();
- }
- float[][] matrix = new float[power + 1][];
- for (int i = 0; i < power + 1; i++)
- {
- float[] pointsX = points.Select(point => point.X).ToArray();
- float[] row = new float[power + 2];
- for (int k = 0; k <= power; k++)
- {
- float leftPart = points.Sum(point => (float)Math.Pow(point.X, i + k));
- row[k] = leftPart;
- }
- float rightPart = points.Sum(point => (float)Math.Pow(point.X, i) * point.Y);
- row[power + 1] = rightPart;
- matrix[i] = row;
- }
- float[] gauss = Gauss(matrix);
- string result = gauss[0].ToString();
- for (int i = 0; i < gauss.Length; i++)
- {
- if (i % 2 == 0)
- {
- result += "-";
- }
- else
- {
- result += "+";
- }
- result += Math.Abs((float)Math.Pow(gauss[i], i));
- result += "x^" + i;
- }
- return result;
- }
- static float[] Gauss(float[][] matrix)
- {
- int n = matrix.Length;
- for (int i = 0; i < n; i++)
- {
- float maxEl = Math.Abs(matrix[i][i]);
- int maxRow = i;
- for (int k = i + 1; k < n; k++)
- {
- float tmp = matrix[maxRow][k];
- matrix[maxRow][k] = matrix[i][k];
- matrix[i][k] = tmp;
- }
- for (int k = i + 1; k < n; k++)
- {
- float c = matrix[k][i] / matrix[i][i];
- for (int j = i; j < n + 1; j++)
- {
- if (i == j)
- {
- matrix[k][j] = 0;
- }
- else
- {
- matrix[k][j] += c * matrix[i][j];
- }
- }
- }
- }
- float[] result = new float[n];
- for (int i = n - 1; i > -1; i--)
- {
- result[i] = matrix[i][n] / matrix[i][i];
- for (int k = i - 1; k > -1; k--)
- {
- matrix[k][n] -= matrix[k][i] * result[i];
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement