Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Квадратурные формулы.docx
Скачиваний:
85
Добавлен:
06.01.2017
Размер:
45.92 Кб
Скачать

Листинг программы.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Collections;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

double a = 0;

double b = 4;

int n = 10;

double h = (b - a) / (double)n;

double[] x = new double[n + 1];

for (int i = 0; i < n; i++)

x[i] = a + i * h;

Del r1 = new Del(Rectangle);

Counter("left rectangle", r1);

Del r2 = new Del(Trapeze);

Counter("trapeze", r2);

Del r3 = new Del(Simpson);

Counter("simpson", r3);

Del r4 = new Del(Gauss);

Counter("gauss", r4);

}

public delegate double Del(int n, double x);

public static void Counter(string methodName, Del method)

{

int a = 0;

int b = 4;

double eps = 0.000001;

double h = 0.4;

using (StreamWriter sr = new StreamWriter("C:/Users/Роман/Desktop/result.txt",true))

{

sr.WriteLine(methodName);

sr.WriteLine("x" + "\t sum" + "\t s1" + "\t Si-s1" + "\t n");

}

for (double x = a; x <= b; x += h)

{

int n = 2;

double s1 = method(n, x);

double s2 = method(2 * n, x);

while (Math.Abs(s1 - s2) > eps)

{

n *= 2;

s1 = s2;

s2 = method(2 * n, x);

if (n == 1024) break;

}

using (StreamWriter sr = new StreamWriter("C:/Users/Роман/Desktop/result.txt",true))

{

sr.Write("{0:0.######} \t", x);

sr.Write("{0:0.######} \t", Sum(x));

sr.Write("{0:0.######} \t", s1);

sr.Write("{0:0.########} \t", Math.Abs(Sum(x) - s1));

sr.WriteLine("\t"+n);

}

}

}

public static double F(double x)

{

if (x == 0) return 1;

return Math.Sin(x) / x;

}

public static double Sum(double x)

{

const double eps = 0.000001;

int i = 0;

double s = 0;

double ai = x;

double qn = 0;

while (Math.Abs(ai) >= eps)

{

s = s + ai;

qn = (-x * x * (2 * i + 1)) / ((2 * i + 3) * (2 * i + 3) * (2 * i + 2));

ai = ai * qn;

i++;

}

return s;

}

public static double Rectangle(int n, double x)

{

double h = x / (n * 1.0);

double s = 0;

for (int i = 0; i < n; i++)

{

double a = (i + 1) * h;

s += F(a);

}

return s * h;

}

public static double Trapeze(int n, double x)

{

double h = x / (n * 1.0);

double s = 0;

for (int i = 0; i < n; i++)

{

double a = i * h;

double b = (i + 1) * h;

s += (F(a) + F(b));

}

return s * h / 2;

}

public static double Simpson(int n, double x)

{

double h = x / (n * 1.0);

double s = 0;

for (int i = 0; i < n; i++)

{

double a = i * h;

double b = (i + 1) * h;

s += (F(a) + 4 * F((a + b) / 2) + F(b));

}

return s * h / 6;

}

public static double Gauss(int n, double x)

{

double h = x / (n * 1.0);

double s = 0;

for (int i = 0; i < n; i++)

{

double a = i * h;

double b = (i + 1) * h;

s += F(a + (b - a) / 2 * (1 - 1 / Math.Sqrt(3)));

s += F(a + (b - a) / 2 * (1 + 1 / Math.Sqrt(3)));

}

return s * h / 2;

}

}

}