Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double trapez(double a, double b, double (*fun)(double));
  6. double simpson(double a, double b, double (*fun)(double));
  7. double fun(double x);
  8. double g(double y);
  9.  
  10. int main()
  11. {
  12. // int n;
  13. double a, b, h;
  14. printf("Enter lower limit:\n");
  15. scanf_s("%lf", &a);
  16. printf("Enter upper limit:\n");
  17. scanf_s("%lf", &b);
  18. //printf("Enter number of nodes:\n");
  19. //scanf_s("%d", &n);
  20. double s, t;
  21. double errT, errS;
  22. for(int n=10; n<=100; n+=10)
  23. {
  24. h = (double)(b - a) / (n - 1);
  25. s = 0;
  26. t = 0;
  27. for (int i = 0; i < (n-1); i++)
  28. {
  29.  
  30. t += trapez(a + i * h, a + (i + 1) * h, (*fun));
  31. s += simpson(a + i * h, a + (i + 1) * h, (*fun));
  32. }
  33.  
  34. printf("n=%d\nTrapez: %lf\n", n, t);
  35. printf("Simpson: %lf\n", s);
  36.  
  37. errT = atan(b) - t;
  38. errS = atan(b) - s;
  39. printf("Error Trapezoidal: %lf\n", errT);
  40. printf("Error Simpsonal: %lf\n", errS);
  41. }
  42. return 0;
  43. }
  44.  
  45. double trapez(double a, double b, double (*fun)(double))
  46. {
  47. double v;
  48. v = 0.5 * (b - a) * (fun(a) + fun(b));
  49. return v;
  50. }
  51.  
  52. double fun(double x)
  53. {
  54. double v = g(x);
  55. return v;
  56. }
  57.  
  58. double simpson(double a, double b, double (*fun)(double))
  59. {
  60. double v;
  61. double c = (a + b) / 2;
  62. v = (1.0 / 6) * (b - a) * (fun(a) + 4 * fun(c) + fun(b));
  63. return v;
  64. }
  65.  
  66. double g(double y)
  67. {
  68. double z = 1. / (1 + (y * y));
  69. return z;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement