Advertisement
isotonicq

POCH

Dec 18th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 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 POCH
  9. Przygotował: Dawid Grzeszuk
  10. Kierunek: Informatyka 3 semestr
  11. Środowisko: Visual Studio 2015
  12. Język programowania: C#
  13. */
  14.  
  15. namespace POCH
  16. {
  17.     public static class MathFunctions
  18.     {
  19.  
  20.         public static readonly double h = 1e-3;
  21.         public static readonly double x0 = 1;
  22.         public static readonly double t0 = 1;
  23.         public static readonly int i = 0;
  24.  
  25.         public static double f(double x) => Math.Exp(-0.5*x)*Math.Sin(5*Math.PI*x);
  26.         public static double Xi(int i) => x0 + i * h;
  27.     }
  28.  
  29.     public static class RSP
  30.     {
  31.         public static double RSPFunkcja1(int i) => (-MathFunctions.f(MathFunctions.Xi(i + 2)) + 4 * MathFunctions.f(MathFunctions.Xi(i + 1)) - 3 * MathFunctions.f(MathFunctions.Xi(i))) / (2 * MathFunctions.h);
  32.         public static double RSPFunkcja2(int i) => (-MathFunctions.f(MathFunctions.Xi(i + 3)) + 4 * MathFunctions.f(MathFunctions.Xi(i + 2)) - 5 * MathFunctions.f(MathFunctions.Xi(i + 1)) + 2 * MathFunctions.f(MathFunctions.Xi(i))) / (MathFunctions.h * MathFunctions.h);
  33.         public static double RSPFunkcja3(int i) => (-3 * MathFunctions.f(MathFunctions.Xi(i + 4)) + 14 * MathFunctions.f(MathFunctions.Xi(i + 3)) - 24 * MathFunctions.f(MathFunctions.Xi(i + 2)) + 18 * MathFunctions.f(MathFunctions.Xi(i + 1)) - 5 * MathFunctions.f(MathFunctions.Xi(i))) / (2 * Math.Pow(MathFunctions.h, 3));
  34.     }
  35.  
  36.     public static class RST
  37.     {
  38.         public static double RSTFunkcja1(int i) => (3 * MathFunctions.f(MathFunctions.Xi(i)) - 4 * MathFunctions.f(MathFunctions.Xi(i - 1)) + MathFunctions.f(MathFunctions.Xi(i - 2))) / (2 * MathFunctions.h);
  39.         public static double RSTFunkcja2(int i) => (2 * MathFunctions.f(MathFunctions.Xi(i)) - 5 * MathFunctions.f(MathFunctions.Xi(i - 1)) + 4 * MathFunctions.f(MathFunctions.Xi(i - 2)) - MathFunctions.f(MathFunctions.Xi(i - 3))) / (MathFunctions.h * MathFunctions.h);
  40.         public static double RSTFunkcja3(int i) => (5 * MathFunctions.f(MathFunctions.Xi(i)) - 18 * MathFunctions.f(MathFunctions.Xi(i - 1)) + 24 * MathFunctions.f(MathFunctions.Xi(i - 2)) - 14 * MathFunctions.f(MathFunctions.Xi(i - 3)) + 3 * MathFunctions.f(MathFunctions.Xi(i - 4))) / (2 * Math.Pow(MathFunctions.h, 3));
  41.     }
  42.  
  43.     public static class RSC
  44.     {
  45.         public static double RSCFunkcja1(int i) => (MathFunctions.f(MathFunctions.Xi(i + 1)) - MathFunctions.f(MathFunctions.Xi(i - 1))) / (2 * MathFunctions.h);
  46.         public static double RSCFunkcja2(int i) => (MathFunctions.f(MathFunctions.Xi(i + 1)) - 2 * MathFunctions.f(MathFunctions.Xi(i)) + MathFunctions.f(MathFunctions.Xi(i - 1))) / (MathFunctions.h * MathFunctions.h);
  47.         public static double RSCFunkcja3(int i) => (MathFunctions.f(MathFunctions.Xi(i + 2)) - 2 * MathFunctions.f(MathFunctions.Xi(i + 1)) + 2 * MathFunctions.f(MathFunctions.Xi(i - 1)) - MathFunctions.f(MathFunctions.Xi(i - 2))) / (2 * Math.Pow(MathFunctions.h, 3));
  48.     }
  49.         class Program
  50.     {
  51.         static void Main(string[] args)
  52.         {
  53.             #region result tables
  54.             List<double>[] results = new List<double>[3];
  55.             #endregion
  56.  
  57.             #region execute and show rsp
  58.             results[0] = new List<double>();
  59.             Console.WriteLine("RSP results: ");
  60.             for(int x=0;x<3;x++)
  61.             {
  62.                 switch(x)
  63.                 {
  64.                     case 0:
  65.                         results[0].Add(RSP.RSPFunkcja1(MathFunctions.i));
  66.                         Console.WriteLine($"function {x + 1}: {results[0][0]}");
  67.                         break;
  68.  
  69.                     case 1:
  70.                         results[0].Add(RSP.RSPFunkcja2(MathFunctions.i));
  71.                         Console.WriteLine($"function {x + 1}: {results[0][1]}");
  72.                         break;
  73.  
  74.                     case 2:
  75.                         results[0].Add(RSP.RSPFunkcja3(MathFunctions.i));
  76.                         Console.WriteLine($"function {x + 1}: {results[0][2]}");
  77.                         break;
  78.                 }
  79.             }
  80.             #endregion
  81.  
  82.             #region execute and show rst
  83.             results[1] = new List<double>();
  84.             Console.WriteLine("\nRST results: ");
  85.             for (int x = 0; x < 3; x++)
  86.             {
  87.                 switch(x)
  88.                 {
  89.                     case 0:
  90.                         results[1].Add(RST.RSTFunkcja1(MathFunctions.i));
  91.                         Console.WriteLine($"function {x + 1}: {results[1][0]}");
  92.                         break;
  93.  
  94.                     case 1:
  95.                         results[1].Add(RST.RSTFunkcja2(MathFunctions.i));
  96.                         Console.WriteLine($"function {x + 1}: {results[1][1]}");
  97.                         break;
  98.  
  99.                     case 2:
  100.                         results[1].Add(RST.RSTFunkcja3(MathFunctions.i));
  101.                         Console.WriteLine($"function {x + 1}: {results[1][2]}");
  102.                         break;
  103.                 }
  104.             }
  105.             #endregion
  106.  
  107.             #region execute and show rsc
  108.             results[2] = new List<double>();
  109.             Console.WriteLine("\nRSC results: ");
  110.             for (int x = 0; x < 3; x++)
  111.             {
  112.                 switch (x)
  113.                 {
  114.                     case 0:
  115.                         results[2].Add(RSC.RSCFunkcja1(MathFunctions.i));
  116.                         Console.WriteLine($"function {x + 1}: {results[2][0]}");
  117.                         break;
  118.  
  119.                     case 1:
  120.                         results[2].Add(RSC.RSCFunkcja2(MathFunctions.i));
  121.                         Console.WriteLine($"function {x + 1}: {results[2][1]}");
  122.                         break;
  123.  
  124.                     case 2:
  125.                         results[2].Add(RSC.RSCFunkcja3(MathFunctions.i));
  126.                         Console.WriteLine($"function {x + 1}: {results[2][2]}");
  127.                         break;
  128.                 }
  129.             }
  130.             #endregion
  131.  
  132.             Console.WriteLine();
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement