Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

newton1.f

c<html>
c<head><title>newton1.f</title></head>
c<body>
c<pre>
      program newton
c
c   Use a Newton iteration to solve the equation
c
c   John Mahaffy  2/14/95
c
c     x**3+x-10=0
c
c    x    -     current approximation to the solution
c    f(x) -     x**3+x-10
c    dfdx    -  derivative of f with respect to x
c    xo    -    previous guess for solution
c    eps   -    convergence criterion
c    dx    -    change in solution approximation
c    it    -    number of iterations
c    itmax -    maximum number of iterations
c
c
      implicit none
      real x,f,dfdx,xo,eps,fx,dx,abs
      integer it,itmax                                              
c
c
c     Now start executable fortran statements
c
      eps=1.e-5
      itmax=10
      print *,'What is your initial guess for the solution?'
      read *, x
      xo= x*(1+10.*eps)
      it=0
c     <a name=1><font color=FF0000>
      do while (abs(x-xo).gt.eps*abs(x).and.it.le.itmax)
c                ^^^^^^^^^^^^^^^^^^^^^       </font></a>
	 it=it+1
         xo=x
         fx=f(xo,dfdx)
c             ^^^^^^^
c                ^  This area between the parentheses is often refered to as the
c                ^  calling sequence, and contains arguments passed as input to
c                ^  the function and passed back as output from the function
c 
         dx = -fx/dfdx
         x=xo+dx
         print *, ' x = ',x,',  f(x) = ',fx, ', dx = ',dx
c<a name="ed"><font color="FF0000">
         end do
c</a></font>
      stop
      end
      function f(x,deriv)
c                ^^^^^^^
c                ^  These are the "dummy" arguments of the function.  They need not be the
c                ^  same variable names as those appearing in the calling sequence
c                ^  where the function is referenced.  However the number of arguments
c                ^  here should match the number in the reference to the function.
c                ^  It is also very important to match the data types between corresponding
c                ^  arguments here and in the calling sequence.
c
c   Evaluate the function f(x)=x**3+x-10
c   also return the derivative of the function
c                                             
      implicit none
      real f,x,deriv
c
c   John Mahaffy  2/14/95
c
c   Function arguments can receive data from the line of the program calling 
c   the function, but can also pass data back to the program using the function.
c   Documentation of arguements should clearly delimit those receiving input for
c   the function from those transmitting output.
c
c   INPUT
c        
c   x   -   independent variable
c
c   OUTPUT
c
c   f     -   value of the function
c   deriv -   derivative of the function with respect to x
c
      deriv=3*x**2+1
      f=x**3+x-10
      return
c<a name="end"><font color="FF0000">
      end
c</font></a>
c</pre>
c</body>
c</html>
Skip to content