Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <math.h>
- double func(double x) {
- return x * x;
- }
- double IntegrateRect(double a, double b, double step) {
- double ans = 0.0;
- int stepRect = 0;
- for (double x = a; x < b; x += step) {
- ans += fmin(step, b - x) * func(x);
- stepRect += 1;
- }
- return ans;
- }
- double IntegrateTrap(double a, double b, double step) {
- double ans = 0.0;
- int stepTrap = 0;
- for (double x = a; x < b; x += step) {
- ans += fmin(step, b - x) * (func(x) + func(x + step)) / 2;
- stepTrap += 1;
- }
- //printf("%d\n", stepTrap);
- return ans;
- }
- double IntegrateT(double a, double b, double precision) {
- double area = (b - a) * (func(b) + func(a)) / 2;
- double areaOld = -10000000.0;
- int i = 1;
- int step = 0;
- while ((fabs(area - areaOld)) > precision) {
- step += 1;
- areaOld = area;
- i++;
- area = IntegrateTrap(a, b, (b - a) / i);
- }
- printf("%d\n", step);
- return area;
- }
- int main(void) {
- printf("Enter interval [a, b] and step (x)\n");
- double a, b, step;
- scanf_s("%lf %lf %lf", &a, &b, &step);
- printf("%f %f %f\n", a, b, step);
- //a = 0.0; b = 1.0; step = 0.01;
- //printf("Integrated with rectangles:");
- printf("Integrated with rectangles: %f\n", IntegrateRect(a, b, step));
- printf("Integrated with trapezoids: %f\n", IntegrateTrap(a, b, step));
- printf("Integrated with another function: %f\n", IntegrateT(a, b, step));
- scanf_s("%lf", &a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement