Advertisement
isotonicq

int3

Nov 30th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using int3;
  8.  
  9. namespace int3
  10. {
  11.     class Matrix
  12.     {
  13.         #region variables
  14.         private int columns, equation, size,m;
  15.         private double interval_start, interval_end, x, h;
  16.         private double[,] table;
  17.         private double[] X;
  18.         double Alfa = 1 / 2, Beta = -1 / 2;
  19.         #endregion
  20.  
  21.         public void SetInterval()
  22.         {
  23.             Console.WriteLine("Podaj początek przedziału:");
  24.             interval_start = Convert.ToDouble(Console.ReadLine());
  25.             Console.WriteLine("Podaj koniec przedziału:");
  26.             interval_end = Convert.ToDouble(Console.ReadLine());
  27.             Console.WriteLine("Podaj liczbę przedziałów:");
  28.             size = Convert.ToInt32(Console.ReadLine());
  29.  
  30.             columns = size + 1;
  31.             equation = size;
  32.             Console.Clear();
  33.         }
  34.  
  35.         public void SetTable()
  36.         {
  37.            table = new double[size,columns];  
  38.         }
  39.  
  40.         public void FillTable()
  41.         {
  42.             SetTable();
  43.  
  44.             for (int x = 0; x < equation; x++)
  45.             {
  46.                 for (int y = 0; y < equation; y++)
  47.                 {
  48.                     if (x == y)
  49.                         table[x,y] = 4;
  50.                     else if (x - 1 == y || x == y - 1)
  51.                         table[x,y] = 1;
  52.                     else
  53.                         table[x,y] = 0;
  54.                 }
  55.             }
  56.             table[0,1] = 2;
  57.             table[size - 1,size - 2] = 2;
  58.         }
  59.  
  60.         public void ShowTable()
  61.         {
  62.             for (int x = 0; x < table.GetLength(0); x++)
  63.             {
  64.                 for (int y = 0; y < table.GetLength(1); y++)
  65.                 {
  66.                     Console.Write($"{table[x,y]} ");
  67.                 }
  68.                 Console.WriteLine();
  69.             }
  70.             Console.WriteLine();
  71.         }
  72.  
  73.         public void ReadX()
  74.         {
  75.             Console.WriteLine("Podaj x:");
  76.             x = Convert.ToDouble(Console.ReadLine());
  77.             Console.Clear();
  78.  
  79.             h = (interval_end - interval_start) / size;
  80.             if (x != interval_end)
  81.                 m = (int)Math.Floor((x-interval_start)/h);
  82.             else m = size - 1;
  83.  
  84.             X = new double[size];
  85.             for (int y = 0; y < size; y++)
  86.                 X[y] = interval_start + y * h;
  87.         }
  88.  
  89.         public void Derivatives()
  90.         {
  91.             for (int x = 0; x < size; x++)
  92.             {
  93.                 if (x == 0)
  94.                     table[x, 6] = 1 / (1 + Math.Pow(X[x], 2)) + (1 / 3 * h * Alfa);
  95.                 else if (x == size - 1)
  96.                     table[x, 6] = 1 / (1 + Math.Pow(X[x], 2)) - (1 / 3 * h * Beta);
  97.                 else
  98.                     table[x, 6] = 1 / (1 + Math.Pow(X[x], 2));
  99.             }
  100.         }
  101.  
  102.         public void GaussMethod()  
  103.         {
  104.             for (int column = 0; column < size; column++)
  105.             {
  106.                 double max = table[column, column];
  107.                 equation = column;
  108.  
  109.                 for (int x = column; x < size; x++)
  110.                 {
  111.                     if (Math.Abs(table[x, column]) > Math.Abs(max))
  112.                     {
  113.                         max = table[x, column];
  114.                         equation = x;
  115.                     }
  116.                 }
  117.  
  118.                 if (Math.Abs(max) < 1e-30)
  119.                 {
  120.                     throw new Exception();
  121.                 }
  122.  
  123.                 for (int x = column; x < size + 1; x++)
  124.                 {
  125.                     SwapElements(column, x, equation, x);
  126.                 }
  127.  
  128.                 double p = table[column, column];
  129.  
  130.                 for (int x = column; x < size + 1; x++)
  131.                 {
  132.                     table[column, x] = table[column, x] / p;
  133.                 }
  134.  
  135.                 for (int x = 0; x < size ; x++)
  136.                 {
  137.                     if (x != column)
  138.                     {
  139.                         double z = table[x, column];
  140.                         for (int y = column; y < size + 1; y++)
  141.                         {
  142.                             table[x, y] = table[x, y] - z * table[column, y];
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.    
  149.         public void CalculateResults()
  150.         {
  151.             double Xm = interval_start + m * h, t;
  152.             if (x != interval_end) t = (x - Xm) / h;
  153.             else t = 1;
  154.  
  155.  
  156.             double[] tab = new double[size + 2];
  157.             for (int y = 1; y < size + 1; y++)
  158.             {
  159.                 tab[y] = table[y - 1, 6];
  160.             }
  161.  
  162.             tab[0] = tab[2] - 1 / 3 * h * Alfa;
  163.             tab[size + 1] = tab[size - 1] + 1 / 3 * h * Beta;
  164.  
  165.             for (int i = 0; i < size + 2; i++)
  166.             {
  167.                 Console.WriteLine($"C({ i - 1}) = {tab[i]}");
  168.             }
  169.             double S = CalculateS(tab, m, t);
  170.             Console.WriteLine($"Y= { S }");
  171.         }
  172.  
  173.         public void SwapElements(int x1, int y1, int x2, int y2)
  174.         {
  175.             double temp = table[x1, y1];
  176.             table[x1, y1] = table[x2, y2];
  177.             table[x2, y2] = temp;
  178.         }
  179.  
  180.  
  181.         public static double CalculateS(double[] tab, int m, double t) =>
  182.             tab[m] * Math.Pow(1 - t, 3) +
  183.             tab[m + 1] * (Math.Pow(2 - t, 3) - 4 * Math.Pow(1 - t, 3)) +
  184.             tab[m + 2] * (Math.Pow(1 + t, 3) - 4 * Math.Pow(t, 3)) +
  185.             tab[m + 3] * Math.Pow(t, 3);
  186.  
  187.     }
  188.  
  189.  
  190.  
  191.     class Program
  192.     {
  193.         static void Main(string[] args)
  194.         {
  195.             var user = new Matrix();
  196.  
  197.             user.SetInterval();
  198.             user.FillTable();
  199.             user.ShowTable();
  200.             user.ReadX();
  201.             user.Derivatives();
  202.             user.ShowTable();
  203.             user.GaussMethod();
  204.             user.ShowTable();
  205.             user.CalculateResults();
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement