multidimensional.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. ;;;; Multidimensional root finder, with secant approximation to derivative.
  21. ;;; GJS -- 10 May 2005
  22. (declare (usual-integrations))
  23. ;;; f is a vector-valued function of a vector argument
  24. (define (multidimensional-root f initial-point initial-step min-step)
  25. (let ((N (v:dimension initial-point)))
  26. (define (step xn xn-1)
  27. (let ((fn (f xn))
  28. (fps
  29. (v:generate N
  30. (lambda (i)
  31. (f
  32. (vector-with-substituted-coord xn i
  33. (vector-ref xn-1 i)))))))
  34. (assert (fix:= N (v:dimension fn)))
  35. (let ((M
  36. (matrix:generate N N
  37. (lambda (i j)
  38. (/ (- (vector-ref fn i)
  39. (vector-ref (vector-ref fps j) i))
  40. (- (vector-ref xn j)
  41. (vector-ref xn-1 j)))))))
  42. (vector-vector xn (lu-solve-linear-system M fn)))))
  43. (define (good-root? xn xn-1)
  44. (let lp ((i 0) (diff 0))
  45. (if (fix:= i N)
  46. (< diff min-step)
  47. (lp (fix:+ i 1)
  48. (max diff
  49. (abs (- (vector-ref xn i)
  50. (vector-ref xn-1 i))))))))
  51. (define (try xn xn-1)
  52. (if (good-root? xn xn-1)
  53. xn
  54. (try (step xn xn-1) xn)))
  55. (try initial-point
  56. (if (vector? initial-step)
  57. (v:generate N
  58. (lambda (i)
  59. (+ (vector-ref initial-point i)
  60. (vector-ref initial-step i))))
  61. (v:generate N
  62. (lambda (i)
  63. (+ (vector-ref initial-point i)
  64. initial-step)))))))
  65. #|
  66. (multidimensional-root
  67. (lambda (v)
  68. (let ((x (vector-ref v 0)) (y (vector-ref v 1)))
  69. (vector (+ x y -3) (+ x (- y) -1))))
  70. (vector 1.5 1.5)
  71. .01
  72. 1e-10)
  73. ;Value: #(2. 1.)
  74. (multidimensional-root
  75. (lambda (v)
  76. (let ((x (vector-ref v 0)) (y (vector-ref v 1)))
  77. (vector (square x) (+ x (- y) -1))))
  78. (vector 1.5 1.5)
  79. .01
  80. 1e-10)
  81. ;Value: #(1.194318926912262e-10 -.9999999998805681)
  82. (multidimensional-root
  83. (lambda (v)
  84. (let ((x (vector-ref v 0)) (y (vector-ref v 1)))
  85. (vector (+ x (- y) -1) (square (+ x y -3)))))
  86. (vector 1.5 1.5)
  87. .01
  88. 1e-15)
  89. ;Value: #(1.999999999999999 .9999999999999988)
  90. |#