Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. double func(double x)
  4. {
  5. return x*(x+3);
  6. }
  7.  
  8. double simp() {
  9. double a, b;
  10. int m = 10;
  11. printf("Input a\n");
  12. scanf("%lf", &a);
  13. printf("Input b\n");
  14. scanf("%lf", &b);
  15. double h = (b - a) / m;
  16. double sum = 0;
  17.  
  18. double x0 = a;
  19. double x1 = a + h;
  20.  
  21. for (int i=0; i<=m-1; i++) {
  22. sum += (h/3)*(2*func(x0 + h/2) + (func(x0)+ func(x1))/2);
  23.  
  24. x0 += h;
  25. x1 += h;
  26. }
  27.  
  28. return sum;
  29. }
  30.  
  31. double rect()
  32. {
  33. double a, b;
  34. printf("Input a\n");
  35. scanf("%lf", &a);
  36. printf("Input b\n");
  37. scanf("%lf", &b);
  38. int m=10, eps=0.0001;
  39. double sum = 0.0, t=0.0, tp=eps+1;
  40. double h = ((b-a)/m);
  41. for (double x = a; x <=b; x+=h, fabs(t-tp)>eps){
  42. sum += t;
  43. tp = t;
  44. t= h*(func(x-h/2));
  45. }
  46.  
  47. return sum;
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53. int w = 1;
  54. while(w==1){
  55. int inp;
  56. double ans=0;
  57. printf("Please choose what to execute: 1- Simpson's method, 2 - Rectangles\n");
  58. scanf("%d", &inp);
  59. switch (inp)
  60. {
  61. case 1:
  62. {
  63. ans = simp();
  64. printf("%f\n",ans);
  65.  
  66. break;
  67. }
  68. case 2:
  69. {
  70. ans = rect();
  71. printf("%f\n", ans);
  72. break;
  73. }
  74. default:
  75. {
  76. printf("Input right value!\n");
  77. break;
  78. }
  79.  
  80.  
  81. }
  82. printf("Continue?\n");
  83. scanf("%d",&w);
  84. }
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement