TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
integral_approximation.cpp File Reference

Compute integral approximation of the function using Riemann sum More...

#include <cassert>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iostream>
Include dependency graph for integral_approximation.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

Functions

double math::integral_approx (double lb, double ub, const std::function< double(double)> &func, double delta=.0001)
 Computes integral approximation.
 
void math::test_eval (double approx, double expected, double threshold)
 Wrapper to evaluate if the approximated value is within .XX% threshold of the exact value.
 
static void test ()
 
int main ()
 Main function.
 

Detailed Description

Compute integral approximation of the function using Riemann sum

In mathematics, a Riemann sum is a certain kind of approximation of an integral by a finite sum. It is named after nineteenth-century German mathematician Bernhard Riemann. One very common application is approximating the area of functions or lines on a graph and the length of curves and other approximations. The sum is calculated by partitioning the region into shapes (rectangles, trapezoids, parabolas, or cubics) that form a region similar to the region being measured, then calculating the area for each of these shapes, and finally adding all of these small areas together. This approach can be used to find a numerical approximation for a definite integral even if the fundamental theorem of calculus does not make it easy to find a closed-form solution. Because the region filled by the small shapes is usually not the same shape as the region being measured, the Riemann sum will differ from the area being measured. This error can be reduced by dividing up the region more finely, using smaller and smaller shapes. As the shapes get smaller and smaller, the sum approaches the Riemann integral.

Author
Benjamin Walton
Shiqi Sheng

Definition in file integral_approximation.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 133 of file integral_approximation.cpp.

133 {
134 test(); // run self-test implementations
135 return 0;
136}
void test()

◆ test()

static void test ( )
static

Definition at line 75 of file integral_approximation.cpp.

75 {
77 3.24, 7.56, [](const double x) { return log(x) + exp(x) + x; });
78 std::cout << "Test Case 1" << std::endl;
79 std::cout << "function: log(x) + e^x + x" << std::endl;
80 std::cout << "range: [3.24, 7.56]" << std::endl;
81 std::cout << "value: " << test_1 << std::endl;
82 math::test_eval(test_1, 1924.80384023549, .001);
83 std::cout << "Test 1 Passed!" << std::endl;
84 std::cout << "=====================" << std::endl;
85
86 double test_2 = math::integral_approx(0.023, 3.69, [](const double x) {
87 return x * x + cos(x) + exp(x) + log(x) * log(x);
88 });
89 std::cout << "Test Case 2" << std::endl;
90 std::cout << "function: x^2 + cos(x) + e^x + log^2(x)" << std::endl;
91 std::cout << "range: [.023, 3.69]" << std::endl;
92 std::cout << "value: " << test_2 << std::endl;
93 math::test_eval(test_2, 58.71291345202729, .001);
94 std::cout << "Test 2 Passed!" << std::endl;
95 std::cout << "=====================" << std::endl;
96
98 10.78, 24.899, [](const double x) { return x * x * x - x * x + 378; });
99 std::cout << "Test Case 3" << std::endl;
100 std::cout << "function: x^3 - x^2 + 378" << std::endl;
101 std::cout << "range: [10.78, 24.899]" << std::endl;
102 std::cout << "value: " << test_3 << std::endl;
103 math::test_eval(test_3, 93320.65915078377, .001);
104 std::cout << "Test 3 Passed!" << std::endl;
105 std::cout << "=====================" << std::endl;
106
107 double test_4 = math::integral_approx(
108 .101, .505,
109 [](const double x) { return cos(x) * tan(x) * x * x + exp(x); },
110 .00001);
111 std::cout << "Test Case 4" << std::endl;
112 std::cout << "function: cos(x)*tan(x)*x^2 + e^x" << std::endl;
113 std::cout << "range: [.101, .505]" << std::endl;
114 std::cout << "value: " << test_4 << std::endl;
115 math::test_eval(test_4, 0.566485986311631, .001);
116 std::cout << "Test 4 Passed!" << std::endl;
117 std::cout << "=====================" << std::endl;
118
119 double test_5 = math::integral_approx(
120 -1, 1, [](const double x) { return exp(-1 / (x * x)); });
121 std::cout << "Test Case 5" << std::endl;
122 std::cout << "function: e^(-1/x^2)" << std::endl;
123 std::cout << "range: [-1, 1]" << std::endl;
124 std::cout << "value: " << test_5 << std::endl;
125 math::test_eval(test_5, 0.1781477117815607, .001);
126 std::cout << "Test 5 Passed!" << std::endl;
127}
static void test_1()
static void test_2()
static void test_3()
void log(T msg)
A function to print given message on console.
uint256_t exp(uint256_t number, uint256_t power, const uint256_t &mod)
This function calculates number raised to exponent power under modulo mod using Modular Exponentiatio...
void test_eval(double approx, double expected, double threshold)
Wrapper to evaluate if the approximated value is within .XX% threshold of the exact value.
double integral_approx(double lb, double ub, const std::function< double(double)> &func, double delta=.0001)
Computes integral approximation.