Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double f(double x)
- {
- return sin(x);
- }
- double simpson(double a, double b, double delta)
- {
- double s, s1, s2, c;
- s = (b-a)*(f(a) + 4*f(a/2 + b/2) + f(b))/6;
- c = (a+b)/2;
- s1 = (c-a)*(f(a) + 4*f(a/2 + c/2) + f(c))/6;
- s2 = (b-c)*(f(c) + 4*f(c/2 + b/2) + f(b))/6;
- if(fabs(s - (s1+s2)) <= delta)
- return s1+s2;
- else
- return simpson(a, c, delta/2) + simpson(c, b, delta/2);
- }
- int main(void)
- {
- double a, b, delta, wynik;
- printf("Podaj a: ");
- scanf("%lf", &a);
- printf("Podaj b: ");
- scanf("%lf", &b);
- printf("Podaj maksymalny blad: ");
- scanf("%lf", &delta);
- wynik = simpson(a, b, delta);
- printf("%f\n", wynik);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement