67double evaluate_by_simpson(std::int32_t N,
double h,
double a,
68 const std::function<
double(
double)>& func) {
69 std::map<std::int32_t, double>
75 for (std::int32_t i = 0; i <= N; i++) {
78 std::pair<std::int32_t, double>(i, temp));
84 double evaluate_integral = 0;
85 for (std::int32_t i = 0; i <= N; i++) {
86 if (i == 0 || i == N) {
87 evaluate_integral += data_table.at(i);
88 }
else if (i % 2 == 1) {
89 evaluate_integral += 4 * data_table.at(i);
91 evaluate_integral += 2 * data_table.at(i);
96 evaluate_integral *=
h / 3;
100 assert(!std::isnan(evaluate_integral) &&
101 "The definite integral can't be evaluated. Check the validity of "
104 return evaluate_integral;
113double f(
double x) {
return std::sqrt(x) + std::log(x); }
115double g(
double x) {
return std::exp(-x) * (4 - std::pow(x, 2)); }
117double k(
double x) {
return std::sqrt(2 * std::pow(x, 3) + 3); }
119double l(
double x) {
return x + std::log(2 * x + 1); }
132static void test(std::int32_t N,
double h,
double a,
double b,
133 bool used_argv_parameters) {
135 double result_f = numerical_methods::simpson_method::evaluate_by_simpson(
137 assert((used_argv_parameters || (result_f >= 4.09 && result_f <= 4.10)) &&
138 "The result of f(x) is wrong");
139 std::cout <<
"The result of integral f(x) on interval [" << a <<
", " << b
140 <<
"] is equal to: " << result_f << std::endl;
142 double result_g = numerical_methods::simpson_method::evaluate_by_simpson(
144 assert((used_argv_parameters || (result_g >= 0.27 && result_g <= 0.28)) &&
145 "The result of g(x) is wrong");
146 std::cout <<
"The result of integral g(x) on interval [" << a <<
", " << b
147 <<
"] is equal to: " << result_g << std::endl;
149 double result_k = numerical_methods::simpson_method::evaluate_by_simpson(
151 assert((used_argv_parameters || (result_k >= 9.06 && result_k <= 9.07)) &&
152 "The result of k(x) is wrong");
153 std::cout <<
"The result of integral k(x) on interval [" << a <<
", " << b
154 <<
"] is equal to: " << result_k << std::endl;
156 double result_l = numerical_methods::simpson_method::evaluate_by_simpson(
158 assert((used_argv_parameters || (result_l >= 7.16 && result_l <= 7.17)) &&
159 "The result of l(x) is wrong");
160 std::cout <<
"The result of integral l(x) on interval [" << a <<
", " << b
161 <<
"] is equal to: " << result_l << std::endl;
170int main(
int argc,
char** argv) {
177 bool used_argv_parameters =
184 N = std::atoi(argv[1]);
185 a = std::atof(argv[2]);
186 b = std::atof(argv[3]);
188 assert(a < b &&
"a has to be less than b");
189 assert(N > 0 &&
"N has to be > 0");
190 if (N < 16 || a != 1 || b != 3) {
191 used_argv_parameters =
true;
193 std::cout <<
"You selected N=" << N <<
", a=" << a <<
", b=" << b
196 std::cout <<
"Default N=" << N <<
", a=" << a <<
", b=" << b
203 test(N,
h, a, b, used_argv_parameters);
double k(double x)
Another test function.
double g(double x)
Another test function.
double f(double x)
A function f(x) that will be used to test the method.
double l(double x)
Another test function.
static void test()
Self-test implementations.
Contains the Simpson's method implementation.