fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <fstream>
  5. using namespace std;
  6. double f(double x) {
  7. if(x==0.0){
  8. return 1.0; }
  9. return sin(x) / x;
  10. }
  11. double trapezoidal(double a, double b, int n) {
  12. double h = (b - a) / n;
  13. double sum = 0.5 * (f(a) + f(b));
  14. for (int i = 1; i < n; ++i) {
  15. sum += f(a + i * h);
  16. }
  17. return h * sum;
  18. }
  19. double richardson(double T1, double T2) {
  20. return (4 * T2 - T1) / 3;
  21. }
  22. double romberg(double a, double b, int maxLevel) {
  23. const int N = maxLevel + 1;
  24. double R[N][N];
  25. for (int i = 0; i <= maxLevel; ++i) {
  26. int n = pow(2, i);
  27. R[i][0] = trapezoidal(a, b, n);
  28. for (int k = 1; k <= i; ++k) {
  29. R[i][k] = (pow(4, k) * R[i][k - 1] - R[i - 1][k - 1]) / (pow(4, k) - 1);
  30. }
  31. }
  32. return R[maxLevel][maxLevel];
  33. }
  34. int main() {
  35. ofstream file1("ass13.txt");
  36. double a = 0.0, b = 1.0;
  37. cout << fixed << setprecision(10);
  38. double reference = 0.9460830703671831;
  39. int steps[] = {10, 20,30,40,50,60,70,80,90,100,110,120,130,140};
  40. cout << "Trapezoidal Approximations:\n";
  41. for (int n : steps) {
  42. double h = 1.0 / n;
  43. double result = trapezoidal(a, b, n);
  44. double error = abs (reference - result);
  45. cout << "n = " << n << ", I ≈ " << result << endl;
  46. double T1 = trapezoidal(a, b, n);
  47. double T2 = trapezoidal(a, b, 2*n);
  48. double R = richardson(T1, T2);
  49. double error1 = abs(reference - R);
  50. cout << "\nRichardson Extrapolation I ≈ " << R << endl;
  51. double romberg_result = romberg(a, b, 4);
  52. double error2 = abs(reference - romberg_result);
  53. cout << "\nRomberg Integration (R[4][4]): I ≈ " << romberg_result << endl;
  54. file1 << log(h) << " " << log(error) << " " << log(error1) << " " << log(error2) << endl;
  55. }
  56. file1.close();
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Trapezoidal Approximations:
n = 10, I ≈ 0.9458320719

Richardson Extrapolation I ≈ 0.9460830765

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 20, I ≈ 0.9460203254

Richardson Extrapolation I ≈ 0.9460830708

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 30, I ≈ 0.9460551841

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 40, I ≈ 0.9460673844

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 50, I ≈ 0.9460730314

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 60, I ≈ 0.9460760989

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 70, I ≈ 0.9460779484

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 80, I ≈ 0.9460791489

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 90, I ≈ 0.9460799719

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 100, I ≈ 0.9460805606

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 110, I ≈ 0.9460809962

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 120, I ≈ 0.9460813275

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 130, I ≈ 0.9460815853

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704
n = 140, I ≈ 0.9460817899

Richardson Extrapolation I ≈ 0.9460830704

Romberg Integration (R[4][4]): I ≈ 0.9460830704