t1nman

Math.lab7

May 19th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. /*
  4.     \int_0^1 1/(2+x^3)
  5.     f(x) = 1/(2+x^3)
  6. */
  7. float rectangle(float a, float b, float N);
  8. float f(float x);
  9. float f_2(float x);
  10. float find_M(float a, float b);
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.     float N = 10;
  15.     float a = 0;
  16.     float b = 1;
  17.     float h;
  18.     h = (b - a) / N;
  19.  
  20.     float S = rectangle(a,b,N);
  21.     float M = find_M(a,b);
  22.     float R = M * pow(h,2)*(b-a) / 24; 
  23.     printf("S = %f\n", S);
  24.     printf("M = %f\n", M);
  25.     printf("R <= %f\n", R);
  26.  
  27.     return 0;
  28. }
  29.  
  30. float rectangle(float a, float b, float N)
  31. {
  32.     int i;
  33.     float h = (b - a) / N;
  34.     float S = 0, x_i, x_i2;
  35.  
  36.     for (i = 1; i <= N; i++) {
  37.         x_i = a + i*h;
  38.         x_i2 = x_i - 0.5*h;
  39.         S += f(x_i2)*h;
  40.     }
  41.  
  42.     return S;
  43. }
  44.  
  45. float f(float x)
  46. {
  47.     return 1/(2 + pow(x, 3));
  48. }
  49.  
  50. float find_M(float a, float b)
  51. {
  52.     float max, Max;
  53.  
  54.     max = (f_2(a) > f_2(b)) ? f_2(a) : f_2(b);
  55.     Max = (max > f_2((b-a)/2)) ? max : f_2((b-a)/2);
  56.  
  57.     return Max;
  58. }
  59.  
  60. float f_2(float x)
  61. {
  62.     return fabs((-12*x*(1 - pow(x, 3))) / pow((2+pow(x,3)), 3));
  63. }
Advertisement
Add Comment
Please, Sign In to add comment