using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rectangle_method {
class Program {
// значение функции
public static double getFunction(double x) {
double retValue = Math.Sqrt(Math.Pow(Math.E, x) - 1);
return retValue;
}
// значение первообразной
public static double getAntiderivative(double x) {
double retValue = 2.0 * Math.Sqrt(Math.Pow(Math.E, x) - 1) - 2.0 * Math.Atan(Math.Sqrt(Math.Pow(Math.E, x) - 1));
return retValue;
}
// вычисление интеграла
public static double calcIntegral(double a, double b, double n, double eps) {
double retValue = 0, // возвращаемое значение
h = (b - a) / n, // вычисляем шаг сетки
curApproximation = 0.0; // текущее приближение
// составная формула средних прямоугольников
double S = getAntiderivative(b) - getAntiderivative(a);
int i = 1;
while(true) {
curApproximation = a + i * h;
++i;
retValue += getFunction(curApproximation - h * 0.5);
retValue *= h;
if (Math.Abs(S - retValue) < eps)
break;
}
return retValue;
}
static void Main(string[] args) {
// пределы интегрирования
double a = 0,
b = Math.Log(2, Math.E),
n = 100,
eps = 0.0001;
Console.WriteLine("Значение интеграла: {0:f4}", calcIntegral(a, b, n, eps));
Console.WriteLine("Проверка: F(b) - F(a) = {0:f4}", getAntiderivative(b) - getAntiderivative(a));
}
}
}