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;
123static void test(std::int32_t N,
double h,
double a,
double b,
124 bool used_argv_parameters) {
127 double result_f = numerical_methods::midpoint_rule::midpoint(
128 N,
h, a, numerical_methods::midpoint_rule::f);
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;
134 double result_g = numerical_methods::midpoint_rule::midpoint(
135 N,
h, a, numerical_methods::midpoint_rule::g);
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;
141 double result_k = numerical_methods::midpoint_rule::midpoint(
142 N,
h, a, numerical_methods::midpoint_rule::k);
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;
148 double result_l = numerical_methods::midpoint_rule::midpoint(
149 N,
h, a, numerical_methods::midpoint_rule::l);
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;
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.