Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. double funcResult(double x) {
  4.   return x/pow(3 * x + 2, 0.5);
  5. }
  6. double rectangle_integral_left(double (*func)(double), double start, double end, double rect_width) {
  7.   double area = 0;
  8.   for(double x = start; x <= end; x += rect_width) {
  9.     double funcVal = func(x);
  10.     area += rect_width * funcVal;
  11.   }
  12.   return area;
  13. }
  14. double rectangle_integral_center(double (*func)(double), double start, double end, double rect_width) {
  15.   double area = 0;
  16.   for(double x = start; x <= end; x += rect_width) {
  17.     double funcVal = func(x + rect_width/2);
  18.     area += rect_width * funcVal;
  19.   }
  20.   return area;
  21. }
  22. double rectangle_integral_right(double (*func)(double), double start, double end, double rect_width) {
  23.   double area = 0;
  24.   for(double x = start; x <= end; x += rect_width) {
  25.     double funcVal = func(x + rect_width);
  26.     area += rect_width * funcVal;
  27.   }
  28.   return area;
  29. }
  30. double trapezoidal_integral(double (*func)(double), double start, double end, double delta){
  31.   double area = 0;
  32.   for(double x = start; x <= end; x += delta) {
  33.     double funcValA = func(x),
  34.            funcValB = func(x + delta);
  35.     area += ( (funcValA + funcValB) / 2 ) * delta;
  36.   }
  37.   return area;
  38. }
  39. double simpon_integral(double (*func)(double), double start, double end, double delta){
  40.   double sum = func(start);
  41.   for(double x = start + delta; x <= end; x += 2 * delta) {
  42.     sum += func(x);
  43.   }
  44.   for(double x = start +  2 * delta; x <= end; x += 2 * delta) {
  45.     sum += func(x);
  46.   }
  47.   return sum * delta;
  48. }
  49. int isPrecise(double val) {
  50.   return ТУТ НАПИШИ РЕАЛЬНЕ ЗНАЧЕННЯ ІНТЕГРАЛУ < 0.001;
  51. }
  52. int main(){
  53.   double integralLeftRectangle = rectangle_integral_left(funcResult, 0, 2, 0.00001);
  54.   double integralCenterRectangle = rectangle_integral_center(funcResult, 0, 2, 0.00001);
  55.   double integralRightRectangle = rectangle_integral_right(funcResult, 0, 2, 0.00001);
  56.   double integralTrapezoidal = trapezoidal_integral(funcResult, 0, 2, 0.00001);
  57.   double integralSimpson = simpon_integral(funcResult, 0, 2, 0.00001);
  58.   printf("Integral Left Rectangle: %lf %s\n", integralLeftRectangle, isPrecise(integralLeftRectangle) ? "is Percise Enough" : "is not Percese Enough");
  59.   printf("Integral Center Rectangle: %lf %s\n", integralCenterRectangle, isPrecise(integralCenterRectangle) ? "is Percise Enough" : "is not Percese Enough");
  60.   printf("Integral Right Rectangle: %lf %s\n", integralRightRectangle, isPrecise(integralRightRectangle) ? "is Percise Enough" : "is not Percese Enough");
  61.   printf("Integral Trapezoidal: %lf %s\n", integralTrapezoidal, isPrecise(integralTrapezoidal) ? "is Percise Enough" : "is not Percese Enough");
  62.   printf("Integral Simpson: %lf %s\n", integralSimpson, isPrecise(integralSimpson) ? "is Percise Enough" : "is not Percese Enough");
  63.   return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement