#include #include // ======================================================================= // Settings for Integrations // ======================================================================= // The tntegrant function. double f(double x) { return x * cos(x); } double A = 0.0; // The lower-limit of the integration interval. double B = 40.0; // The upper-limit of the integration interval. int N = 10000; // The number of tiny intervals for numerical integration. // Be careful of the maximum limit of the "int" value in C programming language. // It depends on your environment, and it might be smaller than you thought. // Use "long long" or "int64_t" and so on, instead of "int", if necessary. // ======================================================================= // Settings for Plottings // ======================================================================= int PRINT_POINTS = 1000; // Number of points to be plotted (*). // *) Actually, the number of plotted points will be PRINT_POINTS + 1, // because the last one point will be output after when integration loops ended. // Also, if N is indivisible by PRINT_POINTS, the actual number of plotted points // will be larger/smaller than PRINT_POINTS in some extent. // ======================================================================= // Compute The Integral Value Numerically // ======================================================================= int main() { double delta = (B - A) / N; // The width of a tiny interval. double value = 0.0; // Add approximated area of all tiny intervals to this variable. int print_interval = N / PRINT_POINTS; // Loop-intervals between points to be plotted. // Traverse tiny intervals from lower-limit (left) to upper-limit (right). for(int i=0; i