Advertisement
markkoval1999

Untitled

Nov 26th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #define eps 0.00001
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. double area_by_rect(double left, double right);
  7. double area_by_trapezia(double left, double right);
  8. double area_by_simpson(double left, double right);
  9. double steps_of_trapezia(double left, double right);
  10. double steps_of_simpson(double left, double right);
  11. double steps_of_rect(double left, double right);
  12. double function(double param);
  13. int main()
  14. {
  15.     char ch;
  16.     double left, right;
  17.     double res1, res2, res3;
  18.     double(*func_pointer)(double,double) = NULL;
  19.     printf("Enter left limit: ");
  20.     scanf_s("%lf", &left);
  21.     printf("Enter right limit: ");
  22.     scanf_s("%lf", &right);
  23.     start:printf("Which method will by chosen: ");
  24.     scanf_s("%c",&ch);
  25.     scanf_s("%c", &ch);
  26.     getchar();
  27.     switch(ch)
  28.     {
  29.         case 't':
  30.         func_pointer = area_by_trapezia;
  31.         func_pointer(left, right);
  32.         break;
  33.  
  34.         case 's':
  35.         func_pointer = area_by_simpson;
  36.         func_pointer(left, right);
  37.         break;
  38.  
  39.         case 'r':
  40.         func_pointer = area_by_rect;
  41.         func_pointer(left, right);
  42.         break;
  43.  
  44.         default:
  45.             goto start;
  46.         break;
  47.     }
  48.  
  49.     /*res2 = area_by_trapezia(left, right);
  50.     res3 = area_by_simpson(left, right);
  51.     res1 = area_by_rect(left, right);
  52.     printf("Average area is %lf\n", (res1 + res2 + res3) / 3);*/
  53.     getchar();
  54.     getchar();
  55.     return 0;
  56. }
  57. double function(double param)
  58. {
  59.     return 1 / pow(cos(param / 3), 2);
  60. }
  61.  
  62. double steps_of_rect(double left, double right)
  63. {
  64.     double save1 = 0, temp, a1 = right;
  65.     int rect_num = 4;
  66.     double width1;
  67.     double width = right - left;
  68.     do
  69.     {
  70.         temp = save1;
  71.         save1 = 0;
  72.         width1 = width / rect_num;
  73.         for (int i = 0; i < rect_num; i++)
  74.         {
  75.             save1 += width1 * (function(a1));
  76.             a1 -= width1;
  77.         }
  78.         a1 = right;
  79.         rect_num *= 2;
  80.     } while (fabs(save1 - temp) > eps);
  81.     return width1;
  82. }
  83.  
  84. double area_by_rect(double left, double right)
  85. {
  86.     double step = steps_of_rect(left, right);
  87.     double area = 0;
  88.     while (left<right)
  89.     {
  90.         area += step*function(left);
  91.         left += step;
  92.     }
  93.     printf("area by rectangle - %lf\n", area);
  94.     return area;
  95. }
  96. double steps_of_trapezia(double left, double right)
  97. {
  98.     double save1 = 0, temp, a1 = right;
  99.     int rect_num = 4;
  100.     double width1;
  101.     double width = right - left;
  102.     do
  103.     {
  104.         temp = save1;
  105.         save1 = 0;
  106.         width1 = width / rect_num;
  107.         for (int i = 0; i < rect_num; i++)
  108.         {
  109.             save1 += width1 * (function(a1) + function(a1 - width1)) / 2;
  110.             a1 -= width1;
  111.         }
  112.         a1 = right;
  113.         rect_num *= 2;
  114.     } while (fabs(save1 - temp) > eps);
  115.     return width1;
  116. }
  117.  
  118. double area_by_trapezia(double left, double right)
  119. {
  120.     double area = 0;
  121.  
  122.     double step = steps_of_trapezia(left, right);
  123.     while (left<right)
  124.     {
  125.         area += step * (function(left) + function(left + step)) / 2;
  126.         left += step;
  127.     }
  128.     printf("area by trapezia - %lf\n", area);
  129.     return area;
  130. }
  131. double steps_of_simpson(double left, double right)
  132. {
  133.     double width = right - left;
  134.     double save2 = 0, temp2 = 0;
  135.     double right2;
  136.     right2 = right;
  137.     double width1;
  138.     width1 = width;
  139.     double rect_num = 4;
  140.     do
  141.     {
  142.         temp2 = save2;
  143.         save2 = 0;
  144.         width1 = width1 / rect_num;
  145.         for (int i = 0; i <= rect_num; i++)
  146.         {
  147.             save2 += (width1 / 6)*(function(right) + function(2 * right - width1) + function(right - width));
  148.             right -= width1;
  149.         }
  150.         right = right2;
  151.         rect_num *= 2;
  152.     } while (fabs(save2 - temp2)>eps);
  153.     return rect_num;
  154. }
  155.  
  156. double area_by_simpson(double left, double right)
  157. {
  158.     double i = 0, j = 0;
  159.     double N = steps_of_simpson(left, right);
  160.     double width = right - left;
  161.     double area3 = 0, area4 = 0, part = width / N, part1 = left + part, part2 = left + 2 * part;
  162.     for (i = 0; i<N / 2; i++)
  163.     {
  164.         area3 += 4 * function(part1);
  165.         part1 = part1 + 2 * part;
  166.         j++;
  167.     }
  168.     for (i = 0; i < j; i++)
  169.     {
  170.         area4 += 2 * function(part2);
  171.         part2 = part2 + 2 * part;
  172.     }
  173.     double result = width / (3 * N)*(function(left) + function(right) + area3 + area4);
  174.     printf("area by Simpson - %lf\n", result);
  175.     return result;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement