fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <locale.h>
  4.  
  5. double f(double x)
  6. {
  7. return acos(x) - sqrt(1 - 0.3 * pow(x, 3.0));
  8. }
  9. double rectangle_method(double xmin, double xmax, int n)
  10. {
  11. //Вычисляем интеграл методом прямоугольников:
  12. double h = (xmax - xmin) / n;
  13. double integral = 0.0;
  14. for (int i = 0; i < n; i++) {
  15. double x = xmin + i * h;
  16. integral += f(x) * h;
  17. }
  18. return integral;
  19. }
  20. double trapezoidal_method(double xmin, double xmax, int n)
  21. {
  22. //Вычисляем интеграл методом трапеций:
  23. double h = (xmax - xmin) / n;
  24. double integral = (f(xmin) + f(xmax)) / 2.0;
  25. for (int i = 1; i < n; i++) {
  26. double x = xmin + i * h;
  27. integral += f(x);
  28. }
  29. integral *= h;
  30. return integral;
  31. }
  32. double calculate_error(double approx_value, double true_value)
  33. {
  34. //Вычисляем относительную погрешность:
  35. if (true_value == 0.0) {
  36. return INFINITY;
  37. }
  38. return fabs((true_value - approx_value) / true_value) * 100;
  39. }
  40. int main()
  41. {
  42. setlocale(LC_ALL, "Rus");
  43. double xmin = 0.0;
  44. double xmax = 1.0;
  45. int iterations[] = {10, 100, 1000, 10000, 100000};
  46. int num_iterations = sizeof(iterations) / sizeof(iterations[0]);
  47.  
  48.  
  49. double true_value = trapezoidal_method(xmin, xmax, 10000000);
  50.  
  51. printf("----------------------------------------------------------------------------------\n");
  52. printf("| Число | Метод прямоугольников | Метод трапеций |\n");
  53. printf("| Итераций |----------------------------------|-----------------------------------|\n");
  54. printf("| | Значение интеграла | Погрешность | Значение интеграла | Погрешность |\n");
  55. printf("|----------|--------------------|-------------|--------------------|--------------|\n");
  56.  
  57. for (int i = 0; i < num_iterations; i++) {
  58. double integral_rect = rectangle_method(xmin, xmax, iterations[i]);
  59. double error_rect = calculate_error(integral_rect, true_value);
  60. double integral_trap = trapezoidal_method(xmin, xmax, iterations[i]);
  61. double error_trap = calculate_error(integral_trap, true_value);
  62.  
  63. printf("| %6d | %18.10f | %11.4f | %18.10f | %11.4f |\n",
  64. iterations[i], integral_rect, error_rect, integral_trap, error_trap);
  65. }
  66.  
  67. printf("--------------------------------------------------------------------------------\n");
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 0.65s 5288KB
stdin
Standard input is empty
stdout
----------------------------------------------------------------------------------
| Число    |      Метод прямоугольников       |          Метод трапеций           |
| Итераций |----------------------------------|-----------------------------------|
|          | Значение интеграла | Погрешность | Значение интеграла | Погрешность  |
|----------|--------------------|-------------|--------------------|--------------|
|     10 |       0.1016528180 |    158.6232 |       0.0312800003 |     20.4180 |
|    100 |       0.0460614476 |     17.1887 |       0.0390241659 |      0.7154 |
|   1000 |       0.0399999353 |      1.7671 |       0.0392962071 |      0.0233 |
|  10000 |       0.0393754561 |      0.1783 |       0.0393050833 |      0.0007 |
| 100000 |       0.0393124040 |      0.0179 |       0.0393053667 |      0.0000 |
--------------------------------------------------------------------------------