gsl_diff__diff.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* diff/diff.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 David Morrison
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_math.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_diff.h"
  24. int
  25. gsl_diff_backward (const gsl_function * f,
  26. double x, double *result, double *abserr)
  27. {
  28. /* Construct a divided difference table with a fairly large step
  29. size to get a very rough estimate of f''. Use this to estimate
  30. the step size which will minimize the error in calculating f'. */
  31. int i, k;
  32. double h = GSL_SQRT_DBL_EPSILON;
  33. double a[3], d[3], a2;
  34. /* Algorithm based on description on pg. 204 of Conte and de Boor
  35. (CdB) - coefficients of Newton form of polynomial of degree 2. */
  36. for (i = 0; i < 3; i++)
  37. {
  38. a[i] = x + (i - 2.0) * h;
  39. d[i] = GSL_FN_EVAL (f, a[i]);
  40. }
  41. for (k = 1; k < 4; k++)
  42. {
  43. for (i = 0; i < 3 - k; i++)
  44. {
  45. d[i] = (d[i + 1] - d[i]) / (a[i + k] - a[i]);
  46. }
  47. }
  48. /* Adapt procedure described on pg. 282 of CdB to find best value of
  49. step size. */
  50. a2 = fabs (d[0] + d[1] + d[2]);
  51. if (a2 < 100.0 * GSL_SQRT_DBL_EPSILON)
  52. {
  53. a2 = 100.0 * GSL_SQRT_DBL_EPSILON;
  54. }
  55. h = sqrt (GSL_SQRT_DBL_EPSILON / (2.0 * a2));
  56. if (h > 100.0 * GSL_SQRT_DBL_EPSILON)
  57. {
  58. h = 100.0 * GSL_SQRT_DBL_EPSILON;
  59. }
  60. *result = (GSL_FN_EVAL (f, x) - GSL_FN_EVAL (f, x - h)) / h;
  61. *abserr = fabs (10.0 * a2 * h);
  62. return GSL_SUCCESS;
  63. }
  64. int
  65. gsl_diff_forward (const gsl_function * f,
  66. double x, double *result, double *abserr)
  67. {
  68. /* Construct a divided difference table with a fairly large step
  69. size to get a very rough estimate of f''. Use this to estimate
  70. the step size which will minimize the error in calculating f'. */
  71. int i, k;
  72. double h = GSL_SQRT_DBL_EPSILON;
  73. double a[3], d[3], a2;
  74. /* Algorithm based on description on pg. 204 of Conte and de Boor
  75. (CdB) - coefficients of Newton form of polynomial of degree 2. */
  76. for (i = 0; i < 3; i++)
  77. {
  78. a[i] = x + i * h;
  79. d[i] = GSL_FN_EVAL (f, a[i]);
  80. }
  81. for (k = 1; k < 4; k++)
  82. {
  83. for (i = 0; i < 3 - k; i++)
  84. {
  85. d[i] = (d[i + 1] - d[i]) / (a[i + k] - a[i]);
  86. }
  87. }
  88. /* Adapt procedure described on pg. 282 of CdB to find best value of
  89. step size. */
  90. a2 = fabs (d[0] + d[1] + d[2]);
  91. if (a2 < 100.0 * GSL_SQRT_DBL_EPSILON)
  92. {
  93. a2 = 100.0 * GSL_SQRT_DBL_EPSILON;
  94. }
  95. h = sqrt (GSL_SQRT_DBL_EPSILON / (2.0 * a2));
  96. if (h > 100.0 * GSL_SQRT_DBL_EPSILON)
  97. {
  98. h = 100.0 * GSL_SQRT_DBL_EPSILON;
  99. }
  100. *result = (GSL_FN_EVAL (f, x + h) - GSL_FN_EVAL (f, x)) / h;
  101. *abserr = fabs (10.0 * a2 * h);
  102. return GSL_SUCCESS;
  103. }
  104. int
  105. gsl_diff_central (const gsl_function * f,
  106. double x, double *result, double *abserr)
  107. {
  108. /* Construct a divided difference table with a fairly large step
  109. size to get a very rough estimate of f'''. Use this to estimate
  110. the step size which will minimize the error in calculating f'. */
  111. int i, k;
  112. double h = GSL_SQRT_DBL_EPSILON;
  113. double a[4], d[4], a3;
  114. /* Algorithm based on description on pg. 204 of Conte and de Boor
  115. (CdB) - coefficients of Newton form of polynomial of degree 3. */
  116. for (i = 0; i < 4; i++)
  117. {
  118. a[i] = x + (i - 2.0) * h;
  119. d[i] = GSL_FN_EVAL (f, a[i]);
  120. }
  121. for (k = 1; k < 5; k++)
  122. {
  123. for (i = 0; i < 4 - k; i++)
  124. {
  125. d[i] = (d[i + 1] - d[i]) / (a[i + k] - a[i]);
  126. }
  127. }
  128. /* Adapt procedure described on pg. 282 of CdB to find best
  129. value of step size. */
  130. a3 = fabs (d[0] + d[1] + d[2] + d[3]);
  131. if (a3 < 100.0 * GSL_SQRT_DBL_EPSILON)
  132. {
  133. a3 = 100.0 * GSL_SQRT_DBL_EPSILON;
  134. }
  135. h = pow (GSL_SQRT_DBL_EPSILON / (2.0 * a3), 1.0 / 3.0);
  136. if (h > 100.0 * GSL_SQRT_DBL_EPSILON)
  137. {
  138. h = 100.0 * GSL_SQRT_DBL_EPSILON;
  139. }
  140. *result = (GSL_FN_EVAL (f, x + h) - GSL_FN_EVAL (f, x - h)) / (2.0 * h);
  141. *abserr = fabs (100.0 * a3 * h * h);
  142. return GSL_SUCCESS;
  143. }