Advertisement
NyanCoder

no2.c

May 20th, 2022
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "math.h"
  4.  
  5. double f(double x);
  6.  
  7. const double a = 2.0;
  8. const double b = 3.0;
  9.  
  10. int main(int argc, char **argv) {
  11.     int n = 1000;
  12.     double step = 0.001;
  13.  
  14.     // Левые прямоугольники
  15.     double s = 0;
  16.     for (int i = 0; i < n; i++)
  17.         s += f(a + i * step) * step;
  18.     printf("А(левая)>> %f\n", s);
  19.     // Правые прямоугольники
  20.     s = 0;
  21.     for (int i = 1; i <= n; i++)
  22.         s += f(a + i * step) * step;
  23.     printf("А(правая)>> %f\n\n", s);
  24.  
  25.     // Трапеции
  26.     s = 0;
  27.     double fA = f(a);
  28.     for (int i = 1; i <= n; i++) {
  29.         double fB = f(a + step*i);
  30.         s += (fA + fB) / 2 * step;
  31.         fA = fB;
  32.     }
  33.     printf("Б>> %f\n\n", s);
  34.  
  35.     // Симпсон
  36.     s = 0;
  37.     fA = f(a);
  38.     for (int i = 1; i <= n; i++) {
  39.         double fMid = f(a + step*((double)i - 0.5));
  40.         double fB = f(a + step*i);
  41.         s += (step/6)*(fA + fMid*4 + fB);
  42.         fA = fB;
  43.     }
  44.     printf("В>> %f\n\n", s);
  45.  
  46.     // Средние прямоугольники
  47.     s = 0;
  48.     for (double x = a + step/2; x < b; x += step)
  49.         s += f(x) * step;
  50.     printf("Г>> %f\n\n", s);
  51.  
  52.     return 0;
  53. }
  54.  
  55. double f(double x) {
  56.     return log(x + 2) / x;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement