52double midpoint(
const std::int32_t N,
const double h,
const double a,
53 const std::function<
double(
double)>& func) {
61 for (std::int32_t i = 0; i < N; i++) {
62 temp = func(xi +
h / 2);
64 std::pair<std::int32_t, double>(i, temp));
70 double evaluate_integral = 0;
71 for (std::int32_t i = 0; i < N; i++) evaluate_integral += data_table.at(i);
74 evaluate_integral *=
h;
78 assert(!std::isnan(evaluate_integral) &&
79 "The definite integral can't be evaluated. Check the validity of "
82 return evaluate_integral;
90double f(
double x) {
return std::sqrt(x) + std::log(x); }
97double g(
double x) {
return std::exp(-x) * (4 - std::pow(x, 2)); }
103double k(
double x) {
return std::sqrt(2 * std::pow(x, 3) + 3); }
109double l(
double x) {
return x + std::log(2 * x + 1); }
123static void test(std::int32_t N,
double h,
double a,
double b,
124 bool used_argv_parameters) {
129 assert((used_argv_parameters || (result_f >= 4.09 && result_f <= 4.10)) &&
130 "The result of f(x) is wrong");
131 std::cout <<
"The result of integral f(x) on interval [" << a <<
", " << b
132 <<
"] is equal to: " << result_f << std::endl;
136 assert((used_argv_parameters || (result_g >= 0.27 && result_g <= 0.28)) &&
137 "The result of g(x) is wrong");
138 std::cout <<
"The result of integral g(x) on interval [" << a <<
", " << b
139 <<
"] is equal to: " << result_g << std::endl;
143 assert((used_argv_parameters || (result_k >= 9.06 && result_k <= 9.07)) &&
144 "The result of k(x) is wrong");
145 std::cout <<
"The result of integral k(x) on interval [" << a <<
", " << b
146 <<
"] is equal to: " << result_k << std::endl;
150 assert((used_argv_parameters || (result_l >= 7.16 && result_l <= 7.17)) &&
151 "The result of l(x) is wrong");
152 std::cout <<
"The result of integral l(x) on interval [" << a <<
", " << b
153 <<
"] is equal to: " << result_l << std::endl;
162int main(
int argc,
char** argv) {
170 bool used_argv_parameters =
177 N = std::atoi(argv[1]);
178 a = std::atof(argv[2]);
179 b = std::atof(argv[3]);
181 assert(a < b &&
"a has to be less than b");
182 assert(N > 0 &&
"N has to be > 0");
183 if (N < 4 || a != 1 || b != 3) {
184 used_argv_parameters =
true;
186 std::cout <<
"You selected N=" << N <<
", a=" << a <<
", b=" << b
189 std::cout <<
"Default N=" << N <<
", a=" << a <<
", b=" << b
196 test(N,
h, a, b, used_argv_parameters);
static void test()
Self-test implementations.
double midpoint(const std::int32_t N, const double h, const double a, const std::function< double(double)> &func)
Main function for implementing the Midpoint Integral Method implementation.
double f(double x)
A function f(x) that will be used to test the method.
double l(double x)
A function l(x) that will be used to test the method.
double g(double x)
A function g(x) that will be used to test the method.
double k(double x)
A function k(x) that will be used to test the method.
Functions for the Midpoint Integral method implementation.