Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double trapez(double a, double b, double (*fun)(double));
- double simpson(double a, double b, double (*fun)(double));
- double fun(double x);
- double g(double y);
- int main()
- {
- // int n;
- double a, b, h;
- printf("Enter lower limit:\n");
- scanf_s("%lf", &a);
- printf("Enter upper limit:\n");
- scanf_s("%lf", &b);
- //printf("Enter number of nodes:\n");
- //scanf_s("%d", &n);
- double s, t;
- double errT, errS;
- for(int n=10; n<=100; n+=10)
- {
- h = (double)(b - a) / (n - 1);
- s = 0;
- t = 0;
- for (int i = 0; i < (n-1); i++)
- {
- t += trapez(a + i * h, a + (i + 1) * h, (*fun));
- s += simpson(a + i * h, a + (i + 1) * h, (*fun));
- }
- printf("n=%d\nTrapez: %lf\n", n, t);
- printf("Simpson: %lf\n", s);
- errT = atan(b) - t;
- errS = atan(b) - s;
- printf("Error Trapezoidal: %lf\n", errT);
- printf("Error Simpsonal: %lf\n", errS);
- }
- return 0;
- }
- double trapez(double a, double b, double (*fun)(double))
- {
- double v;
- v = 0.5 * (b - a) * (fun(a) + fun(b));
- return v;
- }
- double fun(double x)
- {
- double v = g(x);
- return v;
- }
- double simpson(double a, double b, double (*fun)(double))
- {
- double v;
- double c = (a + b) / 2;
- v = (1.0 / 6) * (b - a) * (fun(a) + 4 * fun(c) + fun(b));
- return v;
- }
- double g(double y)
- {
- double z = 1. / (1 + (y * y));
- return z;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement