Advertisement
inf926k

Untitled

Nov 14th, 2016
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace mv_lab2
  7. {
  8.     class Program
  9.     {
  10.         /*
  11.             Y = SIN X
  12.             A = 0
  13.             B = PI / 4
  14.  
  15.         */
  16.         static double A = 0;
  17.         static double B = Math.PI * 0.25f;
  18.  
  19.  
  20.         static double c1 = 1;
  21.         static double c2 = 1;
  22.         static double d1 = 1;
  23.         static double d2 = 1;
  24.  
  25.         static double d = d1 * Math.Sin(B) + d2 * Math.Cos(B);
  26.         static double c = c1 * Math.Sin(A) + c2 * Math.Cos(A);
  27.  
  28.         static int w = 35;
  29.         static void Main(string[] args)
  30.         {
  31.             var res = Math.Sin(B);
  32.             Console.WriteLine("|" + ResizeTo(w, "Нач") + "|" + ResizeTo(w, "Сер") + "|" + ResizeTo(w, "Кон") + "|");
  33.             for (int n = 10; n < 65999; n *= 2)
  34.             {
  35.             solve(n);
  36.             }
  37.             Console.ReadKey();
  38.         }
  39.  
  40.         static void solve(int n)
  41.         {
  42.             double[] xs = new double[n + 1];
  43.             double[] ys = new double[n + 1];
  44.             xs[0] = A;
  45.             xs[n] = B;
  46.             double h = (B - A) / n;
  47.  
  48.             for (int i = 1; i < n; ++i)
  49.             {
  50.                 xs[i] = A + i * h;
  51.             }
  52.  
  53.  
  54.  
  55.             double[] As = new double[n + 1];
  56.             double[] Gs = new double[n + 1];
  57.             double[] Bs = new double[n + 1];
  58.  
  59.  
  60.             double[] MUs = new double[n + 1];
  61.             double[] FIs = new double[n + 1];
  62.             double[] Us = new double[n + 1];
  63.  
  64.             Bs[0] = c1 * h - c2;
  65.             Gs[0] = c2;
  66.             FIs[0] = h * c;
  67.  
  68.             for (int i = 1; i <= n; ++i)
  69.             {
  70.                 As[i] = 1 - p(xs[i]) * h / 2;
  71.                 Bs[i] = -2 + q(xs[i]) * h * h;
  72.                 Gs[i] = 1 + p(xs[i]) * h / 2;
  73.                 FIs[i] = h * h * f(xs[i]);
  74.             }
  75.             As[n] = -d2;
  76.             Bs[n] = h * d1 + d2;
  77.             Gs[n] = 0 /*h * d*/;
  78.             FIs[n] = h * d;
  79.  
  80.             // Прямая прогонка
  81.             MUs[0] = -Gs[0] / Bs[0];
  82.             Us[0] = FIs[0] / Bs[0];
  83.             for (int i = 1; i <=n; i++)
  84.             {
  85.                 MUs[i] = -Gs[i]
  86.                     / (Bs[i] + As[i] * MUs[i - 1]);
  87.  
  88.                 Us[i] = (FIs[i] - As[i] * Us[i - 1])
  89.                     / (Bs[i] + As[i] * MUs[i - 1]);
  90.             }
  91.             // Y[n] ???
  92.             ys[n] = Us[n];
  93.  
  94.             for (int i = n - 1; i >= 0; --i)
  95.             {
  96.                 ys[i] = Us[i] + MUs[i] * ys[i + 1];
  97.             }
  98.  
  99.             //for (int i = 0; i < ys.Length; ++i)
  100.             //    Console.WriteLine(i + " " + ys[i]);
  101.             Console.WriteLine("|"
  102.                 + ResizeTo(w, ys[0] - Math.Sin(xs[0]) + "") + "|"
  103.                 + ResizeTo(w, ys[n / 2] - Math.Sin(xs[n / 2]) + "") + "|"
  104.                 + ResizeTo(w, ys[n] - Math.Sin(xs[n]) + "") + "|");
  105.         }
  106.  
  107.         static string ResizeTo (int n, string s)
  108.         {
  109.             string res = s;
  110.             while (res.Length < n)
  111.             {
  112.                 if (res.Length % 2 == 0)
  113.                     res = res + " ";
  114.                 else
  115.                     res = " " + res;
  116.             }
  117.             return res;
  118.         }
  119.  
  120.         static double f(double x)
  121.         {
  122.             return -Math.Sin(x) + p(x) * Math.Cos(x) + q(x) * Math.Sin(x);
  123.         }
  124.         static double p(double x)
  125.         {
  126.             return 1 / (x * x - 1);
  127.         }
  128.         static double q(double x)
  129.         {
  130.             return 1 / Math.Sqrt(1 - x * x);
  131.         }
  132.  
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement