Advertisement
Guest User

lab1,1

a guest
May 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace lab1
  5. {
  6.     class Program
  7.     {
  8. /* Заданное k */
  9.         public static double f2(double x, int km)
  10.         {
  11.             double k=0.0, S = 0.0, a = 1.0, y;
  12.             S = S + a;
  13.             while (k <= km)
  14.             {
  15.                 y = ((2 * k + 3) / (2 * k + 1)) * ((x * x) / (k + 1));
  16.                 a = a * y;
  17.                 S += a;
  18.                 k++;
  19.             }
  20.             return S;
  21.         }
  22. /* Заданнная точность */
  23.         public static double f3(double x, double eps)
  24.         {
  25.             double k = 0, S = 0.0, a = 1.0, y;
  26.             S = S + a;
  27.             while (Math.Abs(a) >= eps)
  28.             {
  29.                 y = ((2 * k + 3) / (2 * k + 1)) * ((x * x) / (k + 1));
  30.                 a = a * y;
  31.                 S += a;
  32.                 k++;
  33.             }
  34.             return S;
  35.         }
  36.  
  37.         /*Для "точного" значения*/
  38.         public static double f1(double x)
  39.         {
  40.             return (1 + 2 * x * x) * Math.Exp(x * x); ;
  41.         }
  42.  
  43.         /* Главная программа*/
  44.         public static void Main(String[] args)
  45.         {
  46.             System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); //изменение языка и региональных параметров текущего потока
  47.  
  48.             Console.Write("Введите значение k = ");
  49.             int ksum = Convert.ToInt32(Console.ReadLine());
  50.  
  51.             Console.Write("Введите значение eps = ");
  52.             double eps = Convert.ToDouble(Console.ReadLine());
  53.  
  54.             double a = 0.0, // begin
  55.                     b = 1.0,  // end
  56.                     x;  // текущее значение
  57.             int n = 10;  // количество точек x
  58.             double y1, y2, y3, h, ep1, ep2;
  59.             h = (b - a) / (double)n;
  60.  
  61.             Console.WriteLine("Вычисление функции");
  62.             Console.WriteLine("|    x    |     y     |     SN    |     SE    | погр_SN | погр_SE |");
  63.             Console.WriteLine("|_________|___________|___________|___________|_________|_________|");
  64.  
  65.             x = a;
  66.             for (int i = 0; i < n; i++)
  67.             {
  68.                 y1 = f1(x);
  69.                 y2 = f2(x, ksum);
  70.                 y3 = f3(x, eps);
  71.                 ep1 = (Math.Abs((y2 - y1) / y1)) * 100;
  72.                 ep2 = (Math.Abs((y3 - y1) / y1)) * 100;
  73.                
  74.                 string str1 = String.Format("{0:f1}", x);
  75.                 string str2 = String.Format("{0:f3}", y1);
  76.                 string str3 = String.Format("{0:f3}", y2);
  77.                 string str4 = String.Format("{0:f3}", y3);
  78.                 string str5 = String.Format("{0:P2}", ep1);
  79.                 string str6 = String.Format("{0:P2}", ep2);
  80.  
  81.                 Console.Write("|   ");
  82.                 Console.Write(str1);        //x
  83.                 Console.Write("   |   ");
  84.                 Console.Write(str2);        //y
  85.                 Console.Write("   |   ");
  86.                 Console.Write(str3);        //SN
  87.                 Console.Write("   |   ");
  88.                 Console.Write(str4);        //SE
  89.                 Console.Write("   |  ");
  90.                 Console.Write(str5);        //pogr SN
  91.                 Console.Write("  |  ");
  92.                 Console.Write(str6);        //pogr SE
  93.                 Console.Write("  |  ");
  94.                 Console.WriteLine();
  95.  
  96.                 x = x + h;
  97.             }
  98.             Console.ReadKey(); // System.Pause C#
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement