Advertisement
Bob103

C#_Differen

Dec 1st, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     class Program
  7.     {
  8.         public static double[,] T = new double[,] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } };
  9.         static void Main(string[] args)
  10.         {
  11.             List<double> y = new List<double>();
  12.  
  13.             for (int i = 0; i < T.GetLength(0); i++)
  14.             {
  15.                 for (int j = 0; j < T.GetLength(1)-1; j++)
  16.                 {
  17.                     T[i, j] = DoubleDiff(T[i, j], ref i, ref j) + Math.Pow(F(T[i, j], ref i, ref j), 2) * Diff(T[i, j], ref i, ref j) + Math.Pow(F(T[i, j], ref i, ref j), 3) * (F(T[i, j], ref i, ref j) - T[i,j]);
  18.                 }
  19.             }
  20.  
  21.             for (int i = 0; i < T.GetLength(0); i++)
  22.             {
  23.                 for (int j = 0; j < T.GetLength(1); j++)
  24.                 {
  25.                     Console.Write(T[i, j] + " ");
  26.                 }
  27.                 Console.WriteLine();
  28.             }
  29.            
  30.             Console.WriteLine();
  31.  
  32.             Gauss(T);
  33.  
  34.  
  35.             /*Console.WriteLine();
  36.             for (int i = 0; i < T.Length - 1; i++)
  37.             {
  38.                 Console.Write(i + 1 + " ");
  39.             }
  40.             Console.WriteLine();
  41.             for (int i = 0; i < T.Length - 1; i++)
  42.             {
  43.                 Console.Write(y[i] + " ");
  44.             }
  45.             Console.WriteLine();
  46.             for (int i = 0; i < T.Length - 1; i++)
  47.             {
  48.                 Console.Write(y_e[i] + " ");
  49.             }
  50.             Console.WriteLine();
  51.             for (int i = 0; i < y.Count; i++)
  52.             {
  53.                 e.Add(y_e[i] - y[i]);
  54.                 Console.Write(e[i] + " ");
  55.             }*/
  56.  
  57.         }
  58.  
  59.         static double F(double x, ref int i, ref int j)
  60.         {
  61.             return 14 * Math.Pow(x, 3) * (x - T[i, j]);
  62.         }
  63.  
  64.         static double Diff(double x, ref int i, ref int j)
  65.         {
  66.             if(j==0)
  67.             {
  68.                 return (F(x + T[i, j + 1], ref i, ref j) - F(T[i, j], ref i, ref j)) / (2 * 1);
  69.             }
  70.             return (F(T[i,j+1], ref i, ref j) - F(T[i,j-1], ref i, ref j)) / (2 * 1);
  71.         }
  72.  
  73.         static double DoubleDiff(double x, ref int i, ref int j)
  74.         {
  75.             if(j==0)
  76.             {
  77.                 return (F(T[i, j + 1], ref i, ref j) - 2 * F(x, ref i, ref j) + F(T[i, j], ref i, ref j)) / Math.Pow(1, 2);
  78.             }
  79.             return (F(T[i, j + 1], ref i, ref j) - 2 * F(x, ref i, ref j) + F(T[i,j-1], ref i, ref j)) / Math.Pow(1, 2);
  80.         }
  81.  
  82.         static void Gauss(double[,] M)
  83.         {
  84.  
  85.             Console.WriteLine("Start:");
  86.             for (int i = 0; i < M.GetLength(0); i++)
  87.             {
  88.                 for (int j = 0; j < M.GetLength(1); j++)
  89.                 {
  90.                     Console.Write("| " + Math.Round(M[i, j], 1) + " |");
  91.                 }
  92.                 Console.WriteLine();
  93.             }
  94.  
  95.             // size
  96.             int rowCount = M.GetLength(0);
  97.  
  98.             for (int col = 0; col + 1 < rowCount; col++)
  99.                 if (M[col, col] == 0)
  100.                 {
  101.                     // find non-zero coefficient
  102.                     int swapRow = col + 1;
  103.                     for (; swapRow < rowCount; swapRow++)
  104.                         if (M[swapRow, col] != 0) break;
  105.  
  106.                     if (M[swapRow, col] != 0)
  107.                     {
  108.                         // swap it with the above
  109.                         double[] tmp = new double[rowCount + 1];
  110.                         for (int i = 0; i < rowCount + 1; i++)
  111.                         {
  112.                             tmp[i] = M[swapRow, i];
  113.                             M[swapRow, i] = M[col, i];
  114.                             M[col, i] = tmp[i];
  115.                         }
  116.                     }
  117.                 }
  118.  
  119.             // elimination
  120.             for (int sourceRow = 0; sourceRow + 1 < rowCount; sourceRow++)
  121.             {
  122.                 for (int destRow = sourceRow + 1; destRow < rowCount; destRow++)
  123.                 {
  124.                     double df = M[sourceRow, sourceRow];
  125.                     double sf = M[destRow, sourceRow];
  126.                     for (int i = 0; i < rowCount + 1; i++)
  127.                         M[destRow, i] = M[destRow, i] * df - M[sourceRow, i] * sf;//умножаем n-ые элементы n строки на 1 эл-т 1 строки и наоборот,а затем вычитаем
  128.                 }
  129.             }
  130.  
  131.  
  132.             for (int row = rowCount - 1; row >= 0; row--)
  133.             {
  134.                 double f = M[row, row];
  135.  
  136.                 //divide in all rows on first number this row(сокращаем по возможности всю строку)
  137.                 for (int i = 0; i < rowCount + 1; i++)
  138.                     M[row, i] /= f;
  139.  
  140.                 // substiling the value(подставляем значения x1,x2,x3....)
  141.                 for (int destRow = 0; destRow < row; destRow++)
  142.                 {
  143.                     M[destRow, rowCount] -= M[destRow, row] * M[row, rowCount];
  144.                     M[destRow, row] = 0;
  145.                 }
  146.             }
  147.  
  148.             Console.WriteLine("Result:");
  149.             for (int i = 0; i < M.GetLength(0); i++)
  150.             {
  151.                 for (int j = 0; j < M.GetLength(1); j++)
  152.                 {
  153.                     Console.Write("| " + Math.Round(M[i, j], 1) + " |");
  154.                 }
  155.                 Console.WriteLine();
  156.             }
  157.             Console.WriteLine();
  158.  
  159.             for (int i = 0; i < M.GetLength(0); i++)
  160.             {
  161.                 Console.WriteLine("X[{0}]=" + Math.Round(M[i, rowCount], 1), i + 1);
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement