Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define eps 0.00001
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- double area_by_rect(double left, double right);
- double area_by_trapezia(double left, double right);
- double area_by_simpson(double left, double right);
- double steps_of_trapezia(double left, double right);
- double steps_of_simpson(double left, double right);
- double steps_of_rect(double left, double right);
- double function(double param);
- int main()
- {
- char ch;
- double left, right;
- double res1, res2, res3;
- double(*func_pointer)(double,double) = NULL;
- printf("Enter left limit: ");
- scanf_s("%lf", &left);
- printf("Enter right limit: ");
- scanf_s("%lf", &right);
- start:printf("Which method will by chosen: ");
- scanf_s("%c",&ch);
- scanf_s("%c", &ch);
- getchar();
- switch(ch)
- {
- case 't':
- func_pointer = area_by_trapezia;
- func_pointer(left, right);
- break;
- case 's':
- func_pointer = area_by_simpson;
- func_pointer(left, right);
- break;
- case 'r':
- func_pointer = area_by_rect;
- func_pointer(left, right);
- break;
- default:
- goto start;
- break;
- }
- /*res2 = area_by_trapezia(left, right);
- res3 = area_by_simpson(left, right);
- res1 = area_by_rect(left, right);
- printf("Average area is %lf\n", (res1 + res2 + res3) / 3);*/
- getchar();
- getchar();
- return 0;
- }
- double function(double param)
- {
- return 1 / pow(cos(param / 3), 2);
- }
- double steps_of_rect(double left, double right)
- {
- double save1 = 0, temp, a1 = right;
- int rect_num = 4;
- double width1;
- double width = right - left;
- do
- {
- temp = save1;
- save1 = 0;
- width1 = width / rect_num;
- for (int i = 0; i < rect_num; i++)
- {
- save1 += width1 * (function(a1));
- a1 -= width1;
- }
- a1 = right;
- rect_num *= 2;
- } while (fabs(save1 - temp) > eps);
- return width1;
- }
- double area_by_rect(double left, double right)
- {
- double step = steps_of_rect(left, right);
- double area = 0;
- while (left<right)
- {
- area += step*function(left);
- left += step;
- }
- printf("area by rectangle - %lf\n", area);
- return area;
- }
- double steps_of_trapezia(double left, double right)
- {
- double save1 = 0, temp, a1 = right;
- int rect_num = 4;
- double width1;
- double width = right - left;
- do
- {
- temp = save1;
- save1 = 0;
- width1 = width / rect_num;
- for (int i = 0; i < rect_num; i++)
- {
- save1 += width1 * (function(a1) + function(a1 - width1)) / 2;
- a1 -= width1;
- }
- a1 = right;
- rect_num *= 2;
- } while (fabs(save1 - temp) > eps);
- return width1;
- }
- double area_by_trapezia(double left, double right)
- {
- double area = 0;
- double step = steps_of_trapezia(left, right);
- while (left<right)
- {
- area += step * (function(left) + function(left + step)) / 2;
- left += step;
- }
- printf("area by trapezia - %lf\n", area);
- return area;
- }
- double steps_of_simpson(double left, double right)
- {
- double width = right - left;
- double save2 = 0, temp2 = 0;
- double right2;
- right2 = right;
- double width1;
- width1 = width;
- double rect_num = 4;
- do
- {
- temp2 = save2;
- save2 = 0;
- width1 = width1 / rect_num;
- for (int i = 0; i <= rect_num; i++)
- {
- save2 += (width1 / 6)*(function(right) + function(2 * right - width1) + function(right - width));
- right -= width1;
- }
- right = right2;
- rect_num *= 2;
- } while (fabs(save2 - temp2)>eps);
- return rect_num;
- }
- double area_by_simpson(double left, double right)
- {
- double i = 0, j = 0;
- double N = steps_of_simpson(left, right);
- double width = right - left;
- double area3 = 0, area4 = 0, part = width / N, part1 = left + part, part2 = left + 2 * part;
- for (i = 0; i<N / 2; i++)
- {
- area3 += 4 * function(part1);
- part1 = part1 + 2 * part;
- j++;
- }
- for (i = 0; i < j; i++)
- {
- area4 += 2 * function(part2);
- part2 = part2 + 2 * part;
- }
- double result = width / (3 * N)*(function(left) + function(right) + area3 + area4);
- printf("area by Simpson - %lf\n", result);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement