isotonicq

APR2

Jan 13th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. /*
  8. Metody Numeryczne zadanie APR2
  9. Przygotował: Dawid Grzeszuk
  10. Pomoc: Radosław Szkorla
  11. Kierunek: Informatyka 3 semestr
  12. Środowisko: Visual Studio 2017 RC
  13. Język programowania: C#
  14. */
  15.  
  16. namespace APR2
  17. {
  18.     internal static class Details
  19.     {
  20.         static public double eps = 1e-5;
  21.         static public int precision = 10;
  22.     }
  23.     internal static class DisplayMethods
  24.     {
  25.         public static void Show(double[,] tab)
  26.         {
  27.             for (int x = 0; x < tab.GetLength(0); x++)
  28.             {
  29.                 for (int y = 0; y < tab.GetLength(1); y++)
  30.                 {
  31.                     if (y < tab.GetLength(1) - 1)
  32.                         Console.Write($"{Math.Round(tab[x, y], Details.precision)} ");
  33.                     else
  34.                         Console.Write($"| {Math.Round(tab[x, y], Details.precision)} ");
  35.                 }
  36.                 Console.WriteLine();
  37.             }
  38.         }
  39.     }
  40.  
  41.     internal static class MathFunctions
  42.     {
  43.         #region basic functions
  44.         static double f(double x) => x;
  45.         static double g(double x, int j) => f(x) * Math.Pow(x, j);
  46.         public static double h(int k, double a, double b) => (b - a) / Math.Pow(2, k);
  47.         public static double x(long i, int k, double a, double b) => a + i * h(k, a, b);
  48.         public static double y(long i, int k, double a, double b, int j) => g(x(i, k, a, b), j);
  49.         public static double delta(double a, double b, int i) => (Math.Pow(b, i) - Math.Pow(a, i));
  50.         #endregion
  51.  
  52.         #region additional simpson functions
  53.         public static double P(double[,] M, double x)
  54.         {
  55.             double wynik = 0;
  56.             for (int i = 0; i < M.GetLength(0); i++)
  57.             {
  58.                 wynik += M[i, M.GetLength(1) - 1] * Math.Pow(x, i);
  59.             }
  60.             return wynik;
  61.         }
  62.         public static double[,] GlobalM;
  63.         public static double f2(double x) => Math.Pow(f(x) - P(GlobalM, x), 2);
  64.         public static double h2(int k, double a, double b) => (b - a) / Math.Pow(2, k);
  65.         public static double x2(long i, int k, double a, double b) => a + i * h(k, a, b);
  66.         public static double y2(long i, int k, double a, double b, int j) => f(x(i, k, a, b));
  67.         #endregion
  68.  
  69.         #region gauss method
  70.         public static bool GaussMethod(int n, ref double[,] tab)
  71.         {
  72.             int r;
  73.             double max;
  74.             for (int k = 0; k < n; k++)
  75.             {
  76.                 max = tab[k, k];
  77.                 r = k;
  78.                 for (int x = k; x < n; x++)
  79.                 {
  80.                     if (Math.Abs(tab[x, k]) > Math.Abs(max))
  81.                     {
  82.                         max = tab[x, k];
  83.                         r = x;
  84.                     }
  85.                 }
  86.                 if (Math.Abs(max) < Details.eps)
  87.                 {
  88.                     return false;
  89.                 }
  90.                 for (int x = k; x < n + 1; x++)
  91.                 {
  92.                     double tmp = tab[k, x];
  93.                     tab[k, x] = tab[r, x];
  94.                     tab[r, x] = tmp;
  95.                 }
  96.                 for (int x = k; x < n + 1; x++)
  97.                 {
  98.                     tab[k, x] = tab[k, x] / max;
  99.                 }
  100.                 for (int x = 0; x < n; x++)
  101.                 {
  102.                     if (x == k)
  103.                         continue;
  104.                     double t = tab[x, k];
  105.                     for (int y = k; y < n + 1; y++)
  106.                     {
  107.  
  108.                         tab[x, y] = tab[x, y] - t * tab[k, y];
  109.                     }
  110.                 }
  111.             }
  112.             return true;
  113.         }
  114.         #endregion
  115.  
  116.         #region m detection
  117.         public static double[,] mDetection(double a, double b, int k)
  118.         {
  119.             double[,] M = new double[k + 1, k + 2];
  120.  
  121.             for (int i = 0; i < k + 1; i++)
  122.             {
  123.                 for (int j = 0; j < k + 1; j++)
  124.                 {
  125.                     M[i, j] = delta(a, b, i + j + 1) / (i + j + 1.0);
  126.                 }
  127.             }
  128.             for (int i = 0; i < M.GetLength(0); i++)
  129.             {
  130.                 double S1 = 0;
  131.                 double S2 = 0;
  132.                 int counter = 1;
  133.                 S1 = SimpsonSums.SimpsonFunction1(counter, a, b, i);
  134.                 do
  135.                 {
  136.                     counter++;
  137.                     S2 = S1;
  138.                     S1 = SimpsonSums.SimpsonFunction1(counter, a, b, i);
  139.  
  140.                 } while (Math.Abs(S1 - S2) > Details.eps);
  141.                 M[i, M.GetLength(1) - 1] = S1;
  142.             }
  143.             return M;
  144.         }
  145.         #endregion
  146.  
  147.         #region s detection
  148.         public static double S(double a, double b, int i)
  149.         {
  150.             double S1 = 0;
  151.             double S2 = 0;
  152.             int counter = 1;
  153.             S1 = SimpsonSums.SimpsonFunction2(counter, a, b, i);
  154.             do
  155.             {
  156.                 counter++;
  157.                 S2 = S1;
  158.                 S1 = SimpsonSums.SimpsonFunction2(counter, a, b, i);
  159.  
  160.             } while (Math.Abs(S1 - S2) > Details.eps);
  161.             return S1;
  162.         }
  163.  
  164.         #endregion
  165.  
  166.         #region p detection
  167.         public static void P(double[,] M)
  168.         {
  169.             Console.WriteLine();
  170.             Console.Write("P(x)=");
  171.             for (int i = 0; i < M.GetLength(0); i++)
  172.             {
  173.                 if (i == M.GetLength(0) - 1)
  174.                 {
  175.                     Console.Write($"{M[i, M.GetLength(1) - 1]}x^{i} ");
  176.                     break;
  177.                 }
  178.                 Console.Write($"{M[i, M.GetLength(1) - 1]}x^{i} +");
  179.             }
  180.             Console.WriteLine();
  181.         }
  182.         #endregion
  183.     }
  184.  
  185.     internal static class SimpsonSums
  186.     {
  187.         public static double SimpsonSum1(int k, double a, double b, int j)
  188.         {
  189.             double temp = 0;
  190.             for (int i = 1; i < Math.Pow(2, k); i += 2)
  191.             {
  192.                 temp += MathFunctions.y(i, k, a, b, j);
  193.             }
  194.             return temp;
  195.         }
  196.         public static double SimpsonSum2(int k, double a, double b, int j)
  197.         {
  198.             double temp = 0;
  199.             for (int i = 2; i < Math.Pow(2, k) - 1; i += 2)
  200.             {
  201.                 temp += MathFunctions.y(i, k, a, b, j);
  202.             }
  203.             return temp;
  204.         }
  205.         public static double SimpsonSum3(int k, double a, double b, int j)
  206.         {
  207.             double temp = 0;
  208.             for (int i = 1; i < Math.Pow(2, k); i += 2)
  209.             {
  210.                 temp += MathFunctions.y2(i, k, a, b, j);
  211.             }
  212.             return temp;
  213.         }
  214.         public static double SimpsonSum4(int k, double a, double b, int j)
  215.         {
  216.             double temp = 0;
  217.             for (int i = 2; i < Math.Pow(2, k) - 1; i += 2)
  218.             {
  219.                 temp += MathFunctions.y2(i, k, a, b, j);
  220.             }
  221.             return temp;
  222.         }
  223.         public static double SimpsonFunction1(int k, double a, double b, int j) => (MathFunctions.h(k, a, b) / 3) * (MathFunctions.y(0, k, a, b, j) + MathFunctions.y(Convert.ToInt64(Math.Pow(2, k)), k, a, b, j) + 4 * SimpsonSum1(k, a, b, j) + 2 * SimpsonSum2(k, a, b, j));
  224.         public static double SimpsonFunction2(int k, double a, double b, int j) => (MathFunctions.h2(k, a, b) / 3) * (MathFunctions.y2(0, k, a, b, j) + MathFunctions.y2(Convert.ToInt64(Math.Pow(2, k)), k, a, b, j) + 4 * SimpsonSum3(k, a, b, j) + 2 * SimpsonSum4(k, a, b, j));
  225.  
  226.     }
  227.  
  228.     public class Execute
  229.     {
  230.         #region variables
  231.         readonly int k = 0;
  232.         double[,] tab;
  233.         #endregion
  234.  
  235.         public Execute()
  236.         {
  237.             Console.Write("Podaj k: ");
  238.             k = Convert.ToInt32(Console.ReadLine());
  239.  
  240.             Console.WriteLine("\nPodaj przedział");
  241.             Console.Write("od: ");
  242.             double a = Convert.ToDouble(Console.ReadLine());
  243.             Console.Write("do: ");
  244.             double b = Convert.ToDouble(Console.ReadLine());
  245.             Console.Clear();
  246.  
  247.             tab = MathFunctions.mDetection(a, b, k);
  248.  
  249.             Console.WriteLine("Macierz przed rozwiązaniem");
  250.             DisplayMethods.Show(tab);
  251.             Console.WriteLine();
  252.  
  253.             try
  254.             {
  255.                 if (MathFunctions.GaussMethod(k + 1, ref tab))
  256.                 {
  257.                     Console.WriteLine("Macierz po rozwiązaniu");
  258.                     DisplayMethods.Show(tab);
  259.                     MathFunctions.GlobalM = tab;
  260.                     Console.WriteLine();
  261.                     MathFunctions.P(tab);
  262.                     Console.WriteLine($"S(k)= {MathFunctions.S(a, b, k)}");
  263.                 }
  264.                 else throw new Exception();
  265.             }
  266.             catch
  267.             {
  268.                 Console.WriteLine("Macierzu układu osobliwa");
  269.             }
  270.             Console.WriteLine();
  271.         }
  272.     }
  273.  
  274.     class Program
  275.     {
  276.         static void Main(string[] args)
  277.         {
  278.             var program = new Execute();
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment