fork download
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. double macheps()
  6. {
  7.  
  8. double e = 1.0;
  9.  
  10. while (1.0 + e / 2.0 > 1.0)
  11. e /= 2.0;
  12. return e;
  13.  
  14. }
  15.  
  16.  
  17. struct Point
  18. {
  19.  
  20. double x;
  21. double y;
  22.  
  23. };
  24.  
  25.  
  26. double f(unsigned p, double x)
  27. {
  28.  
  29. double y = 0.;
  30.  
  31. for(unsigned i = 0; i <= p; ++i) {
  32.  
  33. y += (2 * (pow(x , 2 * i + 1) / (2 * i + 1)));
  34.  
  35. }
  36.  
  37. return y;
  38.  
  39. }
  40.  
  41. double g(double x)
  42. {
  43.  
  44. return log((1 + x) / (1 - x));
  45.  
  46. }
  47.  
  48.  
  49. void TaylorCalculation(unsigned iterationCount, double a, double b, double (*taylor_f)(unsigned, double), double (*real_f)(double))
  50. {
  51.  
  52. double step = ( b - a ) / iterationCount;
  53. struct Point* points = (struct Point*)malloc(sizeof(struct Point) * iterationCount);
  54. double eps = macheps();
  55. double x = a;
  56.  
  57. for(unsigned i = 0; i < iterationCount; ++i, x+=step) {
  58.  
  59. unsigned p = 0;
  60. points[i].y = 10000;
  61. while(fabs(real_f(x) - taylor_f(p, x)) > eps * 100)
  62. {
  63.  
  64. points[i].x = x;
  65. points[i].y = taylor_f(p, x);
  66. ++p;
  67. if(p >= 100) {
  68.  
  69. break;
  70.  
  71. }
  72. }
  73.  
  74. printf("%d| %lf %lf %lf\n", i, x, real_f(x), points[i].y);
  75.  
  76. }
  77. }
  78.  
  79. int main()
  80. {
  81.  
  82. unsigned n;
  83. double a = 0., b = 0.5;
  84. scanf("%u", &n);
  85. TaylorCalculation(n, a, b, f, g);
  86.  
  87. }
Success #stdin #stdout 0s 4944KB
stdin
10
stdout
0| 0.000000 0.000000 10000.000000
1| 0.050000 0.100083 0.100083
2| 0.100000 0.200671 0.200671
3| 0.150000 0.302281 0.302281
4| 0.200000 0.405465 0.405465
5| 0.250000 0.510826 0.510826
6| 0.300000 0.619039 0.619039
7| 0.350000 0.730888 0.730888
8| 0.400000 0.847298 0.847298
9| 0.450000 0.969401 0.969401