Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdlib.h"
- #include "stdio.h"
- #include "math.h"
- double f(double x);
- const double a = 2.0;
- const double b = 3.0;
- int main(int argc, char **argv) {
- int n = 1000;
- double step = 0.001;
- // Левые прямоугольники
- double s = 0;
- for (int i = 0; i < n; i++)
- s += f(a + i * step) * step;
- printf("А(левая)>> %f\n", s);
- // Правые прямоугольники
- s = 0;
- for (int i = 1; i <= n; i++)
- s += f(a + i * step) * step;
- printf("А(правая)>> %f\n\n", s);
- // Трапеции
- s = 0;
- double fA = f(a);
- for (int i = 1; i <= n; i++) {
- double fB = f(a + step*i);
- s += (fA + fB) / 2 * step;
- fA = fB;
- }
- printf("Б>> %f\n\n", s);
- // Симпсон
- s = 0;
- fA = f(a);
- for (int i = 1; i <= n; i++) {
- double fMid = f(a + step*((double)i - 0.5));
- double fB = f(a + step*i);
- s += (step/6)*(fA + fMid*4 + fB);
- fA = fB;
- }
- printf("В>> %f\n\n", s);
- // Средние прямоугольники
- s = 0;
- for (double x = a + step/2; x < b; x += step)
- s += f(x) * step;
- printf("Г>> %f\n\n", s);
- return 0;
- }
- double f(double x) {
- return log(x + 2) / x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement