Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. //LAB 3
  2.  
  3. //METHODOS GAUSS
  4.  
  5. #include <math.h>
  6. #include <stdio.h>
  7.  
  8. double F(const double t)
  9. {
  10. return exp(-(t + 3) / 4) + cos(7 * (t + 3) / 4) - 6 / (t - 5);
  11. }
  12.  
  13. main()
  14. {
  15.  
  16. double I2 = (1. * F(0.577350269189626) + 1. * F(-0.577350269189626)) / 2;
  17. double I3 = (0.888888888888889 * F(0) + 0.555555555555556 * F(0.774596669241483) + 0.555555555555556 * F(-0.774596669241483)) / 2;
  18. double I5 = (0.568888888888889 * F(0) + 0.478628670499366 * F(0.538469310105683) + 0.478628670499366 * F(-0.538469310105683) + 0.236926885056189 * F(0.906179845938664) + 0.236926885056189 * F(-0.906179845938664)) / 2;
  19. printf("n=2 apot=%10.8f\nn=3 apot=%10.8f\nn=5 apot=%10.8f\n", I2, I3, I5);
  20.  
  21. return 0;
  22. }
  23.  
  24. //METHODOS TRAPEZIOY
  25.  
  26. #include <math.h>
  27. #include <stdio.h>
  28.  
  29. double f(const double x)
  30. {
  31. return exp(-x / 2) + cos(3.5 * x) - 3 / (x - 4);
  32. }
  33.  
  34. double I(const int n, const double a, const double b)
  35. {
  36. int i;
  37. const double h = (b - a) / n;
  38. double I = 0;
  39. for (i = 0; i <= n; ++i) {
  40. I += (i == 0 || i == n ? f(a + i * h) / 2 : f(a + i * h));
  41. }
  42. I *= h;
  43. return I;
  44. }
  45.  
  46. main()
  47. {
  48.  
  49. double I1 = I(50, 1, 2), I2 = I(100, 1, 2);
  50. printf("a=%f b=%f\nn=%3d apot=%10.8f\nn=%3d apot=%10.8f\nerror=%f\n", 1., 2., 50, I1, 100, I2, fabs(I1 - I2));
  51.  
  52. return 0;
  53. }
  54.  
  55. //METHODOS SIMSON
  56.  
  57. #include <math.h>
  58. #include <stdio.h>
  59.  
  60. double f(const double x)
  61. {
  62. return exp(-x / 2) + cos(3.5 * x) - 3 / (x - 4);
  63. }
  64.  
  65. double I(const int n, const double a, const double b)
  66. {
  67. int i;
  68. const double h = (b - a) / n;
  69. double I = 0;
  70. for (i = 0; i <= n; ++i) {
  71. if (i == 0 || i == n) {
  72. I += f(a + i * h);
  73. } else if (i % 2 == 0) {
  74. I += 2 * f(a + i * h);
  75. } else {
  76. I += 4 * f(a + i * h);
  77. }
  78. }
  79. I *= h / 3;
  80. return I;
  81. }
  82.  
  83. main()
  84. {
  85.  
  86. double I1 = I(50, 1, 2), I2 = I(100, 1, 2);
  87. printf("a=%f b=%f\nn=%3d apot=%10.8f\nn=%3d apot=%10.8f\nerror=%.10f\n", 1., 2., 50, I1, 100, I2, fabs(I1 - I2));
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement