c
cnewton1.f
c
c 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
do while (abs(x-xo).gt.eps*abs(x).and.it.le.itmax)
c ^^^^^^^^^^^^^^^^^^^^^
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
end do
c
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
end
c
c

c
c