diff --git a/Maths/numerical_integration.cpp b/Maths/numerical_integration.cpp new file mode 100644 index 0000000..c22092c --- /dev/null +++ b/Maths/numerical_integration.cpp @@ -0,0 +1,295 @@ +#include +#include +#include +#include +#include +#define PI 3.14159265359 +using namespace std; +typedef double dd; + +// global array to store coefficient and power of polynomial + +dd coeff[1000], power[1000][10]; + +// 2d array for eqations like xsin^4xcos^5x + +char equation[1000][10]; + +int size; + +// If the function cannot be given as input then hardcode the function + +/* + dd EQUATION(dd x) { + dd value = 0; + value = pow(2.71828, -(x*x)/2) / sqrt(2*PI); + return value; + } +*/ + +// This is how the function looks like + +void visualise() { + cout << "F(x) = "; + + for (int i = 0; i < size; ++i) { + + if (coeff[i] > 0) + cout << "+"; + cout << coeff[i]; + for (int j = 0; j < 10; ++j) { + + if (equation[i][j] == 'x') + cout << "x^"<< power[i][j]; + + if (equation[i][j] == 's') + cout << "sin^" <> size; + cout << "Enter the coefficient, function and power of the equation" << endl; + + /* + if the polynomial is ax^2 + b^x + c; + enter in the format a 2 b 1 c 0; + */ + + for (int i = 0; i < size; ++i) { + cin >> coeff[i]; + for (int j = 0; j < 10; ++j) { + flush = getchar(); + if (flush == '\n') + break; + cin >> equation[i][j]; + if (equation[i][j] == 't') + tan_flag = 1; + cin >> power[i][j]; + // cout << equation[i][j] << endl; + // if (equation[i][j] == '\n') + // break; + } + } + // Calling function to see how the function actually looks like + visualise(); + + LOOP: + cout << endl ; + cout << "Enter lower bound: \n"; + cin >> a; + cout << "Enter upper bound: \n"; + cin >> b; + cout << "Enter number of iterations: \n"; + cin >> n; + + + if ((int(floor((b*2)/PI) - floor((a*2)/PI)) % 2) && tan_flag == 1) { + cout << "Divergent"; + goto LOOP; + } + dd h = (b-a)/n; + + cout << endl; + cout << "Numerical Methods of Integration: " << endl; + + cout << "1. Midpoint Rule: "; + t = clock(); + cout << MIDPOINT_EQUATION(a, b, n) << endl; + t = clock() - t; + time_taken = ((double)t)/CLOCKS_PER_SEC; + cout << " Computation Time: " << time_taken << endl << endl; + + cout << "2. Rectangle Rule: "; + t = clock(); + cout << RECTANGULAR_EQUATION(a, b, n) << endl; + t = clock() - t; + time_taken = ((double)t)/CLOCKS_PER_SEC; + cout << " Computation Time: " << time_taken << endl << endl; + + cout << "3. Trapezoidal Rule: "; + t = clock(); + cout << TRAPEZOIDAL_EQUATION(a, b, n) << endl << endl; + t = clock() - t; + time_taken = ((double)t)/CLOCKS_PER_SEC; + cout << " Computation Time: " << time_taken << endl << endl; + + cout << "4. Simpsons Rule: "; + t = clock(); + cout << SIMPSON_EQUATION(a, b, n) << endl; + t = clock() - t; + time_taken = ((double)t)/CLOCKS_PER_SEC; + cout << " Computation Time: " << time_taken << endl << endl; + + cout << "5. Romberg Rule: "; + t = clock(); + cout << ROMBERG_EQUATION(a, b) << endl; + t = clock() - t; + time_taken = ((double)t)/CLOCKS_PER_SEC; + cout << " Computation Time: " << time_taken << endl << endl; + + cout << endl; + cout << "Press 0 to exit" << endl; + cout << "Press 1 to change integral bounds" << endl; + cout << "Press 2 to change the function"<< endl; + + cin >> opt; + if(opt == 1) + goto LOOP; + if(opt == 2) + continue; + else + exit(0); + } + + return 0; +}