Advertisement
Guest User

Untitled

a guest
May 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. double f(double x)
  5. {
  6. return sin(x);
  7. }
  8.  
  9. int main()
  10. {
  11. int N;
  12. printf("Specify N: ");
  13. scanf("%i",&N);
  14.  
  15. double h;
  16. h = M_PI/N;
  17.  
  18. double sum = 0;
  19.  
  20. double x;
  21. x=h;
  22.  
  23. for(int i=0;i<=N;i++)
  24. {
  25. if (i==0)
  26. {
  27. sum+=f(0)/2;
  28. }
  29. if (i==N)
  30. {
  31. sum+=f(M_PI)/2;
  32. }
  33. else
  34. {
  35. sum+=f(x);
  36. x+=h;
  37. }
  38. }
  39.  
  40. double result = h*sum;
  41. printf("The Trapezoid method result for %i is %.10lf",N,result);
  42.  
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement