Advertisement
Guest User

Numeric methods

a guest
May 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.19 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace kak_da_si_resha_zadachite_lyubo
  5. {
  6.     class Point
  7.     {
  8.         public float X { get; set; }
  9.  
  10.         public float Y { get; set; }
  11.     }
  12.  
  13.     class Program
  14.     {
  15.         const int Power = 3;
  16.  
  17.         static void Main()
  18.         {
  19.  
  20.         }
  21.  
  22.         static float CalculateIntegral(Func<float, float> f, float a, float b, int n)
  23.         {
  24.             float step = Math.Abs(a - b) / n;
  25.             float result = (b - a) / (2 * n);
  26.             float rightPart = f(a);
  27.             float sum = 0;
  28.  
  29.             for (int i = 1; i < n; i++)
  30.             {
  31.                 sum += f(a + (step * i));
  32.             }
  33.  
  34.             rightPart += 2 * sum;
  35.             rightPart += b;
  36.             return result * rightPart;
  37.         }
  38.        
  39.         static float ApproximateSecondDerivativeLagrance(Point[] points, float x)
  40.         {
  41.             float secondDerivative = 0;
  42.  
  43.             for (int k = 0; k < points.Length; k++)
  44.             {
  45.                 float product = 1;
  46.  
  47.                 for (int i = 0; i < points.Length; i++)
  48.                 {
  49.                     if (i == k)
  50.                     {
  51.                         continue;
  52.                     }
  53.  
  54.                     product *= points[k].X - points[i].X;
  55.                 }
  56.  
  57.                 secondDerivative += (2 * points[k].Y) / product;
  58.             }
  59.  
  60.             return secondDerivative;
  61.         }
  62.  
  63.         static float CalculateFirstExpressionDerivativeLagrance(Point[] points, int k, int power)
  64.         {
  65.             float product = 0;
  66.            
  67.             for (int i = 0; i < power + 1; i++)
  68.             {
  69.                 Point point = points[i];
  70.  
  71.                 if (i == k)
  72.                 {
  73.                     continue;
  74.                 }
  75.                
  76.                 if (product == 0)
  77.                 {
  78.                     product = 1;
  79.                 }
  80.  
  81.                 product *= points[k].X - points[i].X;
  82.             }
  83.  
  84.             return points[k].Y / product;
  85.         }
  86.  
  87.         static float CalculateSecondPartDerivativeLagrance(Point[] points, float x, int k, int power)
  88.         {
  89.             float result = 0;
  90.  
  91.             for (int i = 0; i < power + 1; i++)
  92.             {
  93.                 if (i == k)
  94.                 {
  95.                     continue;
  96.                 }
  97.  
  98.                 float product = 0;
  99.  
  100.                 for (int j = 0; j < power + 1; j++)
  101.                 {
  102.                     if (j == i || j == k)
  103.                     {
  104.                         continue;
  105.                     }
  106.  
  107.                     if (product == 0)
  108.                     {
  109.                         product = 1;
  110.                     }
  111.  
  112.                     product *= x - points[j].X;
  113.                 }
  114.  
  115.                 result += product;
  116.             }
  117.  
  118.             return result;
  119.         }
  120.    
  121.         static float ApproximateFirstDerivativeLagrance(Point[] points, float x)
  122.         {
  123.             float approximation = 0;
  124.  
  125.             for (int k = 0; k < points.Length; k++)
  126.             {
  127.                 float firstExpressionLagrance = CalculateFirstExpressionDerivativeLagrance(points, k, points.Length - 1);
  128.                 float secondExpressionLagrance = CalculateSecondPartDerivativeLagrance(points, x, k, points.Length - 1);
  129.                 approximation += firstExpressionLagrance + secondExpressionLagrance;
  130.             }
  131.  
  132.             return approximation;
  133.         }
  134.  
  135.         static string LeastSquares(Point[] points, int power)
  136.         {
  137.             if (power == 0)
  138.             {
  139.                 return (points.Sum(point => point.X) / points.Length).ToString();
  140.             }
  141.  
  142.             float[][] matrix = new float[power + 1][];
  143.  
  144.             for (int i = 0; i < power + 1; i++)
  145.             {
  146.                 float[] pointsX = points.Select(point => point.X).ToArray();
  147.                 float[] row = new float[power + 2];
  148.  
  149.                 for (int k = 0; k <= power; k++)
  150.                 {
  151.                     float leftPart = points.Sum(point => (float)Math.Pow(point.X, i + k));
  152.                     row[k] = leftPart;
  153.                 }
  154.  
  155.                 float rightPart = points.Sum(point => (float)Math.Pow(point.X, i) * point.Y);
  156.                 row[power + 1] = rightPart;
  157.                 matrix[i] = row;
  158.             }
  159.  
  160.             float[] gauss = Gauss(matrix);
  161.             string result = gauss[0].ToString();
  162.  
  163.             for (int i = 0; i < gauss.Length; i++)
  164.             {
  165.                 if (i % 2 == 0)
  166.                 {
  167.                     result += "-";
  168.                 }
  169.                 else
  170.                 {
  171.                     result += "+";
  172.                 }
  173.  
  174.                 result += Math.Abs((float)Math.Pow(gauss[i], i));
  175.                 result += "x^" + i;
  176.             }
  177.  
  178.             return result;
  179.         }
  180.  
  181.         static float[] Gauss(float[][] matrix)
  182.         {
  183.             int n = matrix.Length;
  184.  
  185.             for (int i = 0; i < n; i++)
  186.             {
  187.                 float maxEl = Math.Abs(matrix[i][i]);
  188.                 int maxRow = i;
  189.  
  190.                 for (int k = i + 1; k < n; k++)
  191.                 {
  192.                     float tmp = matrix[maxRow][k];
  193.                     matrix[maxRow][k] = matrix[i][k];
  194.                     matrix[i][k] = tmp;
  195.                 }
  196.  
  197.                 for (int k = i + 1; k < n; k++)
  198.                 {
  199.                     float c = matrix[k][i] / matrix[i][i];
  200.  
  201.                     for (int j = i; j < n + 1; j++)
  202.                     {
  203.                         if (i == j)
  204.                         {
  205.                             matrix[k][j] = 0;
  206.                         }
  207.                         else
  208.                         {
  209.                             matrix[k][j] += c * matrix[i][j];
  210.                         }
  211.                     }
  212.                 }
  213.             }
  214.  
  215.             float[] result = new float[n];
  216.  
  217.             for (int i = n - 1; i > -1; i--)
  218.             {
  219.                 result[i] = matrix[i][n] / matrix[i][i];
  220.  
  221.                 for (int k = i - 1; k > -1; k--)
  222.                 {
  223.                     matrix[k][n] -= matrix[k][i] * result[i];
  224.                 }
  225.             }
  226.  
  227.             return result;
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement