Advertisement
elsemTim

Simpson

Dec 14th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 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. namespace SimpsonAndTrapece
  8. {
  9.     class Program
  10.     {
  11.         static double f(double x)
  12.         {
  13.             return Math.Cos(Math.Pow(x, 2) / 4);
  14.         }
  15.  
  16.         delegate double f1(double x);
  17.  
  18.         static double trapeze(f1 f, double low, double high, double n)
  19.         {
  20.             double step = (high - low) / n;
  21.             double U = (f(low) + f(high)) * 0.5;
  22.  
  23.             for (int i = 1; i < n; i++)
  24.             {
  25.                 U += f(i * step);
  26.             }
  27.             U = U * step;
  28.             return U;
  29.         }
  30.  
  31.         static double Simpson(f1 f, double low, double high, int n)
  32.         {
  33.  
  34.             double step = (high - low) / n;
  35.             double U = f(low) + f(high);
  36.             int t = 1;
  37.             for (int i = 1; i < n; i++)
  38.             {
  39.                 U += (t + 3) * f(i * step);
  40.                 t = -t;
  41.             }
  42.             U = U * step / 3;
  43.             return U;
  44.         }
  45.         static void Main(string[] args)
  46.         {
  47.             double integral1, integral2;
  48.             double e = 0.5 * 0.0001;
  49.            
  50.  
  51.             Console.WriteLine("Вычисление определенного интеграла dx с границами [0;1/2]");
  52.             Console.WriteLine("методом Симпсона и трапеций");
  53.             Console.WriteLine();
  54.  
  55.             Console.WriteLine("Метод симпсона:");
  56.             integral1 = Simpson(f, 0, 0.5, 32);
  57.             Console.WriteLine("Интеграл = {0}", integral1);
  58.  
  59.  
  60.             integral2 = trapeze(f, 0, 0.5, 64);
  61.             Console.WriteLine();
  62.             Console.WriteLine("Метод трапеций:");
  63.             Console.WriteLine("Интеграл = {0}", integral2);
  64.  
  65.             double integral21;
  66.             double integral22;
  67.  
  68.             Console.WriteLine();
  69.             Console.WriteLine("Количество разбиений для Трапеций");
  70.             int n = 3;
  71.             do
  72.             {
  73.                 integral21 = trapeze(f, 0, 0.5, n);
  74.                 integral22 = trapeze(f, 0, 0.5, 2 * n);
  75.  
  76.  
  77.                 n = 2 * n;
  78.             }
  79.             while (Math.Abs(integral21 - integral22) > 3 * e);
  80.  
  81.             Console.WriteLine("{0}, {1}", n, integral22);
  82.  
  83.             double integral11;
  84.             double integral12;
  85.  
  86.             n = 3;
  87.             Console.WriteLine();
  88.             Console.WriteLine("Количество разбиений для Сипсона");
  89.             do
  90.             {
  91.                 integral11 = Simpson(f, 0, 0.5, n);
  92.                 integral12 = Simpson(f, 0, 0.5, 2 * n);
  93.                 n = 2 * n;
  94.             }
  95.             while (Math.Abs(integral21 - integral22) > 3 * e);
  96.  
  97.             Console.WriteLine("{0}, {1}", n, integral22);
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement