Skip to content

Instantly share code, notes, and snippets.

@morontt
Last active January 17, 2016 21:07
Show Gist options
  • Save morontt/d2385fb2de23fafaf33c to your computer and use it in GitHub Desktop.
Save morontt/d2385fb2de23fafaf33c to your computer and use it in GitHub Desktop.
integrate
/*
* Compilation: gcc -Wall integrate.c -o integrate -lm
*/
#include <stdio.h>
#include <math.h>
#define N 100
double func(double x) {
return x * log(x);
}
double i_prm(double min, double max);
double i_trp(double min, double max);
double i_par(double min, double max);
int main() {
double a = 1.0;
double b = exp(1.0);
double rezult_prm = i_prm(a, b);
printf("1.\t%14.12f\n", rezult_prm);
double rezult_trp = i_trp(a, b);
printf("2.\t%14.12f\n", rezult_trp);
double rezult_par = i_par(a, b);
printf("3.\t%14.12f\n", rezult_par);
double real_rezult = (1.0 + exp(2.0)) * 0.25;
printf("Real:\t%14.12f\n", real_rezult);
return 0;
}
//метод прямоугольников
double i_prm(double min, double max) {
double x, h, s = 0.0;
int i;
h = (max - min) / N;
for (i = 0; i < N; i++) {
x = min + i * h;
s += func(x) * h;
}
return s;
}
//метод трапеций
double i_trp(double min, double max) {
double x, h, s = 0.0;
int i;
h = (max - min) / N;
for (i = 0; i < N; i++) {
x = min + i * h;
s += (func(x) + func(x + h)) * h;
}
s /= 2.0;
return s;
}
//метод парабол
double i_par(double min, double max) {
double x, h, s = 0.0;
int i;
h = (max - min) / N;
for (i = 0; i < N; i++) {
x = min + i * h;
s += (func(x) + 4.0 * func(x + 0.5 * h) + func(x + h)) * h;
}
s /= 6.0;
return s;
}
  • Метод прямоугольников
  • Метод трапеций
  • Метод параболы
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment