newton-kahan.scm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;; In simple cases, if one knows the function and its derivative,
  21. ;;; Newton's method is a quick-and-dirty way to find a root.
  22. ;;; However, one must start close to the root to get it to converge.
  23. (declare (usual-integrations))
  24. (define (newton-search f&df x0 eps)
  25. (define (newton-improve xn)
  26. (f&df xn
  27. (lambda (fn dfn)
  28. (- xn (/ fn dfn)))))
  29. (let lp ((xn x0))
  30. (let ((xn+1 (newton-improve xn)))
  31. (if (close-enuf? xn xn+1 eps)
  32. (average xn xn+1)
  33. (lp xn+1)))))
  34. #|
  35. (newton-search
  36. (lambda (x cont)
  37. (write-line x)
  38. (cont (cos x) (- (sin x))))
  39. 1.0
  40. 1e-15)
  41. 1.
  42. 1.6420926159343308
  43. 1.5706752771612507
  44. 1.5707963267954879
  45. 1.5707963267948966
  46. ;Value: 1.5707963267948966
  47. ;;; If the root is multiple, the convergence is much slower
  48. ;;; and much less accurate.
  49. (newton-search
  50. (lambda (x cont)
  51. (write-line x)
  52. (cont (- 1.0 (sin x)) (- (cos x))))
  53. 1
  54. 1e-15)
  55. 1
  56. 1.2934079930260234
  57. 1.4329983666650792
  58. ;;; 28 iterations here
  59. 1.570796311871084
  60. 1.570796319310356
  61. ;Value: 1.570796319310356
  62. |#
  63. ;;; Kahan's hack speeds up search for multiple roots, but costs
  64. ;;; a bit for simple roots.
  65. (define (newton-kahan-search f&df x0 x1 eps)
  66. (define (kahan-trick x)
  67. (let ((z (round (abs x))))
  68. (if *kahan-wallp* (write-line `(kahan ,z)))
  69. z))
  70. (define (psi x) (f&df x /))
  71. (define (secant-improve xn psn xn-1 psn-1)
  72. (- xn
  73. (* psn
  74. (kahan-trick (/ (- xn xn-1)
  75. (- psn psn-1))))))
  76. (let lp ((xn x1) (psn (psi x1)) (xn-1 x0) (psn-1 (psi x0)))
  77. (if (close-enuf? xn xn-1 eps)
  78. (average xn xn-1)
  79. (let ((xn+1 (secant-improve xn psn xn-1 psn-1)))
  80. (lp xn+1 (psi xn+1) xn psn)))))
  81. (define *kahan-wallp* #f)
  82. #|
  83. ;;; for example
  84. (newton-kahan-search
  85. (lambda (x cont)
  86. (write-line x)
  87. (cont (cos x) (- (sin x))))
  88. 1.0
  89. 2.0
  90. 1e-15)
  91. 1.
  92. 2.
  93. 1.5423424456397141
  94. 1.5708040082580965
  95. 1.5707963267948966
  96. 1.5707963267948966
  97. ;Value: 1.5707963267948966
  98. ;;; Kahan's method really speeds things up here, but it
  99. ;;; doesn't make the result more accurate.
  100. (newton-kahan-search
  101. (lambda (x cont)
  102. (write-line x)
  103. (cont (- 1.0 (sin x)) (- (cos x))))
  104. 1.0
  105. 2.0
  106. 1e-15)
  107. 1.
  108. 2.
  109. 1.564083803078276
  110. 1.5707963519994068
  111. 1.5707963255702555
  112. 1.5707963255702555
  113. ;Value: 1.5707963255702555
  114. |#