Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double f(double x)
  5. {
  6.     return sin(x);
  7. }
  8.  
  9. double simpson(double a, double b, double delta)
  10. {
  11.     double s, s1, s2, c;
  12.     s = (b-a)*(f(a) + 4*f(a/2 + b/2) + f(b))/6;
  13.     c = (a+b)/2;
  14.     s1 = (c-a)*(f(a) + 4*f(a/2 + c/2) + f(c))/6;
  15.     s2 = (b-c)*(f(c) + 4*f(c/2 + b/2) + f(b))/6;
  16.  
  17.     if(fabs(s - (s1+s2)) <= delta)
  18.         return s1+s2;
  19.  
  20.     else
  21.         return simpson(a, c, delta/2) + simpson(c, b, delta/2);
  22. }
  23.  
  24. int main(void)
  25. {
  26.     double a, b, delta, wynik;
  27.  
  28.     printf("Podaj a: ");
  29.     scanf("%lf", &a);
  30.     printf("Podaj b: ");
  31.     scanf("%lf", &b);  
  32.     printf("Podaj maksymalny blad: ");
  33.     scanf("%lf", &delta);
  34.  
  35.     wynik = simpson(a, b, delta);
  36.  
  37.     printf("%f\n", wynik);
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement