int main()
{
// We have the Given function:
auto Function = [&](Doub x) -> Doub{return cos((x * x))* exp(-(x * x * x));};
std::function<Doub(Doub)> fun = Function;
// We will use trapezoidal method to integrate our function:
auto Trapezoid_Integral = [&](std::function<Doub(Doub)>& fun, Doub& a, Doub& b, Int& N)
{
Doub sum = 0;
Doub h = (b - a) / N;
for (int i = 1; i < N; i++) {
sum += fun(a + i * h);
}
return h / 2.0 * (fun(a) + fun(b) + 2 * sum);
};
std::function<Doub(std::function<Doub(Doub)>& fun, Doub& a, Doub& b, Int& N)> integral = Trapezoid_Integral;
// We have the given parameters:
int N = 2, a = 1, b = 4;
Doub acc = 1E-5;
while (true)
{
cout << Trapezoid_Integral(fun, a, b, N);
}
}
}
#include <functional>
#include <iostream>
using Doub = double;
using Int = int;
int main()
{
// We have the Given function:
auto Function = [&](Doub x) -> Doub {return cos((x * x)) * exp(-(x * x * x)); };
std::function<Doub(Doub)> fun = Function;
// We will use trapezoidal method to integrate our function:
auto Trapezoid_Integral = [&](std::function<Doub(Doub)>& fun, Doub& a, Doub& b, Int& N)
{
Doub sum = 0;
Doub h = (b - a) / N;
for (int i = 1; i < N; i++) {
sum += fun(a + i * h);
}
return h / 2.0 * (fun(a) + fun(b) + 2 * sum);
};
std::function<Doub(std::function<Doub(Doub)>& fun, Doub& a, Doub& b, Int& N)> integral = Trapezoid_Integral;
// We have the given parameters:
int N = 2;
Doub a = 1, b = 4;
std::cout << Trapezoid_Integral(fun, a, b, N);
}
#include <cmath>
#include <iostream>
usingnamespace std;
double integrand( double x ){ return cos( x * x ) * exp ( -x * x * x ); }
double Trapezoid_Integral( double f( double ), double a, double b, int N )
{
double sum = 0;
double dx = ( b - a ) / N;
// ERROR *** for ( int i = 1; i < N; i++ ) sum += f( a + ( i - 0.5 ) * dx ); *** ERROR
for ( int i = 1; i < N; i++ ) sum += f( a + i * dx );
return dx * ( sum + 0.5 * ( f( a ) + f( b ) ) );
}
int main()
{
int N = 1000;
double a = 1.0, b = 4.0;
cout << "Integral is " << Trapezoid_Integral( integrand, a, b, N ) << '\n';
cout << "Integral is " << Trapezoid_Integral( []( double x ){ return cos( x * x ) * exp ( -x * x * x ); }, a, b, N ) << '\n';
}
It's a good thing that you are keeping an eye on my bad code, Albatross! My previous effort was an unholy mix of trapezium rule and mid-ordinate rule. I hope that this is better.
#include <cmath>
#include <iostream>
usingnamespace std;
double integrand( double x ){ return cos( x * x ) * exp ( -x * x * x ); }
double Trapezoid_Integral( double f( double ), double a, double b, int N )
{
double sum = 0;
double dx = ( b - a ) / N;
for ( int i = 1; i < N; i++ ) sum += f( a + i * dx );
return dx * ( sum + 0.5 * ( f( a ) + f( b ) ) );
}
int main()
{
int N = 1000;
double a = 1.0, b = 4.0;
cout << "Integral is " << Trapezoid_Integral( integrand, a, b, N ) << '\n';
cout << "Integral is " << Trapezoid_Integral( []( double x ){ return cos( x * x ) * exp ( -x * x * x ); }, a, b, N ) << '\n';
}
Integral is 0.0121455
Integral is 0.0121455
For Albatross's linear (and hence, in this case, exact-solution-giving) example:
#include <cmath>
#include <iostream>
usingnamespace std;
double Trapezoid_Integral( double f( double ), double a, double b, int N )
{
double sum = 0;
double dx = ( b - a ) / N;
for ( int i = 1; i < N; i++ ) sum += f( a + i * dx );
return dx * ( sum + 0.5 * ( f( a ) + f( b ) ) );
}
int main()
{
double a = 0.0, b = 1.0;
for ( int N = 2; N <= 8; N *= 2 )
{
cout << "N: " << N << " Integral: " << Trapezoid_Integral( []( double x ){ return x; }, a, b, N ) << '\n';
}
}