home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / fweb140x.zip / demos / Newton.web < prev    next >
Text File  |  1993-10-30  |  10KB  |  289 lines

  1. % Newton.web, fweb, version 1.13, January 17, 1991
  2. % Bart Childs, Tom McCurdy, Clarissa Wilson  bart@cs.tamu.edu
  3. % Texas A&M University, 409-845-5470
  4. % Department of Comp Science, TAMU, College Station, TX 77843-3112
  5. %
  6. %   REVISION HISTORY
  7. %   1.0 January 17, 1990  a companion to Intro.tex which uses this
  8. %       file's line numbers <---====  NOTICE, don't change line numbers
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. %
  11. %   LIMBO MATERIAL
  12. %
  13. @x
  14. \font\ninerm=cmr9
  15. \let\mc=\ninerm % medium caps for names like UNIX
  16. \def\WEB{{\tt WEB}}
  17. \def\FWEB{{\tt FWEB}}
  18. \def\Fortran{F{\sc ORTRAN}}
  19. \def\PASCAL{Pascal}
  20. \def\<{$\langle\,$}
  21. \def\>{$\,\rangle$}
  22. \def\C{{\bf C}}
  23. \def\Unix{{\mc UNIX}}  \let\unix=\Unix
  24. \def\today{\ifcase\month\or
  25.    January\or February\or March\or April\or
  26.    May\or June\or July\or August\or
  27.    September\or October\or November\or December\fi
  28.    \space\number\day, \number\year}
  29. \def\title{{The Newton Method as a Short \WEB{} Example}}
  30. \def\topofcontents{\hsize 6in
  31.   \vglue -30pt plus 1fil minus 1.5in
  32.   \centerline{\title}
  33.   \vskip 15pt
  34.   \centerline{\today}
  35. {\bigskip\parskip6pt plus2pt \parindent20pt
  36. The following output has been changed to achieve some efficiency
  37. in paper.  The major modules that appear in the following table of
  38. contents would normally start a new page.  In the interest of
  39. brevity, this has been suppressed in modules 7, 13, and 18.
  40.  
  41. This is written in John Krommes' \FWEB{} because 
  42. \Fortran{} is such a simple language.  It is to show
  43. features of \WEB{}s and is not purported to be the absolutely
  44. best way of coding a solution of this problem.
  45.  
  46. This is therefore an expository note about the \WEB{} style
  47. of Literate Programming.
  48. \vskip0.5in}
  49. \vfil
  50. \centerline{\bf Table of Contents}
  51. \medskip
  52. \def\?##1]{\hbox to 1in{\hfil##1.\ }}}
  53. \def\botofcontents{\vskip 0pt plus 1fil minus 1.5in}
  54. %
  55. %   END OF LIMBO MATERIAL
  56. %
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58. %
  59. %   BEGINNING OF WEB
  60. %
  61. \def\FWEAVE{{\tt FWEAVE}}
  62. \def\sc{\eightrm}
  63.  
  64. @n
  65.  
  66. @* The Newton Method.  The Newton method is based on
  67. the Taylor's Series:
  68. $$ f(x) = \sum_{n=0}^{+\infty}{{f^{(n)}(a)}\over {n!}}(x-a)^n =
  69.     f(a) + f'(a)(x-a) + {f''(a)\over{2!}}(x-a)^2 +\ldots+
  70.     {f^{(n)}(a)\over {n!}}(x-a)^n +\ldots $$
  71.  
  72.   In order to use the Newton method a number of conditions must be met:
  73. $f(x)$ must be analytic and the second derivative of $f(x)$ must be
  74. monotone in the neighborhood of $x$.  If these conditions are met,
  75. then the Newton method will converge quadratically and monotonically
  76. ({\it i.e.} with each iteration the number of significant digits will
  77. double).  The Newton method is an iterative method using the first two
  78. terms of the aforementioned Taylor Series: 
  79. $$f(x_{k+1})= f(x_k) + f'(x_k)(x_k-x_{k+1})$$
  80. Since $f(x_{k+1}) = 0 $, the formula is then transformed into the Newton
  81. Method:
  82. @^analytic@> @^Quadratic@> @^Monotone@>
  83. $$ x_{k+1} = x_k - {{f(x_k)}\over {f'(x_k)}} $$
  84.  
  85. @ This program is written to run under the Cray {\tt cft77} and
  86. the usual compilers for SUNs.
  87.  
  88. @ This is controlled by the environment variables |CRAY| and |unix|.
  89. It should be straightforward to accomodate other environments. 
  90.  
  91. The {\tt ftangle} command line to produce |CRAY| code is:\hfil\break
  92. \centerline{\tt ftangle Newton -m"CRAY" -d}
  93. The {\tt -d} converts  |do|/|end do| constructions into labeled |do|
  94. loops. Cray, like everybody else, has extended their compiler beyond
  95. the standard, but their |do while| requires a labeled ending
  96. statement.  Our coding
  97. style assumes that the standards extended by MIL-STD 1752 are
  98. present.  This added the |do|/|end do| and |implicit none|
  99. statements to \Fortran.
  100. @^32-bit 64-bit@>
  101.  
  102. @ Charles Karney of Princeton University furnished macros
  103. for machine dependencies as part of a large body of code released
  104. with John Krommes' \FWEB{} system.  The default is a system
  105. that is typical of |SUN|'s |unix| systems. \FWEB{} actually takes care
  106. of the |do/end do| but we use these macros to handle |implicit none|
  107. and differences between systems whose native modes are 32-bit or 64-bit.
  108. We would define another environment variable, |VMS|, if we were also
  109. considering VAX-VMS targets.
  110. @^32-bit 64-bit@>
  111.  
  112. @#if defined(CRAY)
  113. @m CRAY 1
  114. @m unix 0
  115. @#else
  116. @m CRAY 0
  117. @m unix 1 
  118. @#endif
  119.  
  120.  
  121. @ We start each \Fortran{} module with a command that defeats
  122. the archaic default typing of variable names.  We don't suggest
  123. that it should be removed from the compiler because of the
  124. need for upward compatability from millions of lines of
  125. existing code.  We question it remaining the default mode.
  126.  
  127. Most |unix| systems use |implicit undefined(a-z)|
  128. while the other target environments
  129. have a more reasonable form, |implicit none|.  We define |implicit_none|
  130. in terms of a macro.  Our program uses this in its
  131. \WEB{} form and the macro substitution affects the resulting
  132. \Fortran{} code.
  133.  
  134. @f implicit_none implicit
  135.  
  136. @#if unix
  137. @m implicit_none implicit undefined(a-z)
  138. @#else
  139. @m implicit_none implicit none
  140. @#endif
  141.  
  142. @ We introduce another macro to make it easy to run the tests
  143. with 64~bit precision in all the environments.  The Cray uses
  144. 64~bit precision for |real| variables by default.  The other
  145. machines will use this when |real*8| is specified.  However,
  146. the actual storage will vary among most of the machines.
  147.  
  148. Another macro is used to convert floating point constants
  149. by appending the ``{\tt d0}'' exponent on 32-bit machines.
  150. @^32-bit 64-bit@>
  151.  
  152. @f floating real
  153.  
  154. @#if CRAY
  155.   @m floating real
  156.   @m const(x) x
  157. @#else
  158.   @m floating real*8
  159.   @m const(x) x##d0
  160.   @#endif
  161.  
  162. @* A Newton method program.
  163. The following program will use the Newton method to solve the equation
  164. $\cos(x)=0$.  The {\it semicolons} at the end of each statement were
  165. added automatically by \FWEAVE{} and denote the end of a statement.
  166. These were added to make the code more sentence-like
  167. ({\it i.e. Literate Programming}) and only appear in the typeset
  168. output.  We will add the |implicit_none| as previously described to avoid
  169. errors in the |implicit| type casting found in \Fortran.
  170.  
  171. @a
  172.     program cosine
  173.       implicit_none
  174.       @<Declare Variables@>
  175.       @<Initialization of Variables@>
  176.       @<Iteration Loop@>
  177.       @<Print Results@>
  178.       stop
  179.       end
  180.  
  181. @ We declare the following variables: |x_0| will represent $x_k$
  182. from the Taylor Series, |x| will correspond to $x_{k+1}$, and
  183. |delta_x| will be calculated as the difference between $x_{k+1}$ and $x_k$.
  184. These will be defined as |floating| which will be translated to the
  185. |real| type in the \Fortran{} source code for the machine specified
  186. in the {\tt FTANGLE} command line.
  187.  
  188. @<Declare Variables@>=
  189.     floating x_0, x, delta_x
  190.  
  191. @ We will also declare two |integer| variables.  These are
  192. an iteration index, |k|, and a |limit| for the number of iterations.
  193.  
  194. @<Declare Variables@>=
  195.     integer k, limit
  196.  
  197. @ The initial value of |x| is not quite arbitrary.  This problem has
  198. an $\infty$ of solutions and the initial value can cause convergence
  199. to roots that are not desired.  In the case of |x| $= 1.2$, |x|
  200. will converge to $\pi\over 2$.  We will also need to set |delta_x| to
  201. any nonzero value.  This is needed to allow the iteration loop to
  202. execute at least once.
  203.  
  204. Of course, in most reasonable programs, this would not be hardcoded,
  205. there would be a dialog to establish these values.  The |limit| of
  206. 10 iterations and |delta_x_max|$=0.5$ are based upon knowing the problem
  207. and algorithm's performance.
  208.  
  209. @<Initialization of Variables@>=
  210.     x = const(1.2)
  211.     delta_x=const(0.000001)
  212.     limit = 10 /* more than safe */
  213.  
  214. @ The following is the heart of the Newton method.  We will continue
  215. calculating $x_{k+1}$ until $\Delta x$ becomes sufficiently small.
  216. With each iteration of the loop, the number of signicant digits
  217. in |x| will double, approximately.
  218.  
  219. @<Iteration Loop@>=
  220.     k = 1
  221.     do while ((delta_x .gt. const(0.0)) .and. (k .le. limit))
  222.       @<Calculate Newton change@>
  223.       @<Apply convergence strategies?@>
  224.       k = k + 1
  225.       end do
  226.  
  227. @ In the vanilla Newton method, |x_0| will receive the value
  228. of |x| from the previous iteration.  To calculate |x|, we use the Newton
  229. formula described in Module 1 by substituting |x_0| for $x_k$.
  230. Now, we will calulate |delta_x| to determine if the iteration loop
  231. should be terminated.
  232.  
  233. @<Calculate Newton change@>=
  234.        x_0 = x
  235.        delta_x = - (cos(x_0))/(-sin(x_0))
  236.        x = delta_x + x_0
  237.  
  238. @* Convergence Strategies.  It is well know that if certain conditions
  239. are met, then the convergence of the Newton method is quadratic
  240. and monotone.  This applies within the {\it radius of convergence}.
  241. In some cases, we can apply strategies that will expand the radius of
  242. convergence and still allow rapid convergence in the immediate proximity
  243. of the solution.  The strategies will include constraining the step or
  244. change and noticing when convergence is not monotone.
  245. @^Monotone@>
  246. @<Declare Variables@>=
  247.       floating delta_x_max, delta_x_prev
  248.  
  249. @  These variables need initial values.
  250.  
  251. @<Initialization of Variables@>=
  252.     delta_x_prev = const(0.0)
  253.     delta_x_max = const(0.5)
  254.  
  255. @ When we solve practical problems, we will have some estimate of the
  256. answer, or at least its order of magnitude.  After calculating
  257. |delta_x|, we do not let its magnitude exceed |delta_x_max|.
  258. We don't check for quadratic convergence.@^Quadratic@>
  259. @<Apply convergence strategies?@>=
  260.        if (delta_x > delta_x_max) then
  261.          delta_x = delta_x_max
  262.        else if (delta_x < (-delta_x_max)) then
  263.          delta_x = -delta_x_max
  264.          end if
  265. @^Damping@>
  266.  
  267. @ This problem is an example of those problems that do not exhibit
  268. monotone convergence.  In this case, the assumptions are not met
  269. because there is an inflection point at the solution.  When we do
  270. not have monotone convergence, we should note it.  We find this
  271. by comparing the signs of consectutive changes.
  272. @^Monotone@>
  273. @<Apply convergence strategies?@>=
  274.       if ((delta_x*delta_x_prev) .lt. const(0.0)) then
  275.         write(*,*) 'Oscillating', k
  276. @.Oscillating@>
  277.         end if
  278.       x = x_0 + delta_x
  279.       delta_x_prev = delta_x
  280.  
  281. @ Finally, it's time to let the world know the results.
  282. @.The solution ...@>
  283. @<Print Results@>=
  284.        write (*,*) 'The solution to cos(x)=0 is ', x
  285.  
  286. @* Index.
  287.  
  288.  
  289.