Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SimpsonAndTrapece
- {
- class Program
- {
- static double f(double x)
- {
- return Math.Cos(Math.Pow(x, 2) / 4);
- }
- delegate double f1(double x);
- static double trapeze(f1 f, double low, double high, double n)
- {
- double step = (high - low) / n;
- double U = (f(low) + f(high)) * 0.5;
- for (int i = 1; i < n; i++)
- {
- U += f(i * step);
- }
- U = U * step;
- return U;
- }
- static double Simpson(f1 f, double low, double high, int n)
- {
- double step = (high - low) / n;
- double U = f(low) + f(high);
- int t = 1;
- for (int i = 1; i < n; i++)
- {
- U += (t + 3) * f(i * step);
- t = -t;
- }
- U = U * step / 3;
- return U;
- }
- static void Main(string[] args)
- {
- double integral1, integral2;
- double e = 0.5 * 0.0001;
- Console.WriteLine("Вычисление определенного интеграла dx с границами [0;1/2]");
- Console.WriteLine("методом Симпсона и трапеций");
- Console.WriteLine();
- Console.WriteLine("Метод симпсона:");
- integral1 = Simpson(f, 0, 0.5, 32);
- Console.WriteLine("Интеграл = {0}", integral1);
- integral2 = trapeze(f, 0, 0.5, 64);
- Console.WriteLine();
- Console.WriteLine("Метод трапеций:");
- Console.WriteLine("Интеграл = {0}", integral2);
- double integral21;
- double integral22;
- Console.WriteLine();
- Console.WriteLine("Количество разбиений для Трапеций");
- int n = 3;
- do
- {
- integral21 = trapeze(f, 0, 0.5, n);
- integral22 = trapeze(f, 0, 0.5, 2 * n);
- n = 2 * n;
- }
- while (Math.Abs(integral21 - integral22) > 3 * e);
- Console.WriteLine("{0}, {1}", n, integral22);
- double integral11;
- double integral12;
- n = 3;
- Console.WriteLine();
- Console.WriteLine("Количество разбиений для Сипсона");
- do
- {
- integral11 = Simpson(f, 0, 0.5, n);
- integral12 = Simpson(f, 0, 0.5, 2 * n);
- n = 2 * n;
- }
- while (Math.Abs(integral21 - integral22) > 3 * e);
- Console.WriteLine("{0}, {1}", n, integral22);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement