secant.f
c<html>
c<head><title>secant.f</title></head>
c<body>
c<pre>
program secant
c
c Use a Secant iteration to solve the equation
c
c x**3+x-10=0
c
c x - current approximation to the solution
c f(x) - x**3+x-10
c dfdx(x) - derivative of f with respect to x
c xm1 - 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 <a name="implicit"><font color="FF0000">
implicit none
c </font></a>
integer it,itmax,r8
parameter (r8=selected_real_kind(14,100))
real(r8) x,f,dfdx,xo,eps,fx,dx,x0,xm1,fxm1,xm2,fxm2
c
c Use fortran statement functions to define f and its derivative
c Note that these statements must appear before other executable
c statements
c
f(x)=x**3+x-10.
dfdx(x)=3*x**2+1.
c
c Now start executable fortran statements
c
eps=1.e-6
itmax=10
write(*,*) 'Secant Iteration to find a solution to x**3+x-10=0'
Write(*,'(a)',advance='no')'Initial guess for the solution: '
read *, x0
c
c Set the two starter points for the Secant Iteration
c
x=x0
xm1=.999*x
fxm1=f(xm1)
c
c Secant Iteration
c
do 5 it=1,itmax
xm2=xm1
fxm2=fxm1
xm1=x
fxm1=f(xm1)
dx = -fxm1*(xm1-xm2)/(fxm1-fxm2)
x=xm1+dx
print *, ' x = ',x,', f(xm1) = ',fxm1, ', dx = ',dx
c <a name=abs> <font color = FF0000>
if(abs(dx).lt.eps*abs(x)) go to 10
c ^^^^^^^^^^^^^^^^^^^^^^</font></a>
5 continue
write(6,*)'Secant Iteration Failed to Converge'
10 x=x0
c
c For comparison here is a Newton Iteration
c
write(6,*) 'For comparison here is a Newton Iteration'
do 15 it=1,itmax
xo=x
fx=f(xo)
dx = -fx/dfdx(xo)
x=xo+dx
print *, ' x = ',x,', f(x) = ',fx, ', dx = ',dx
c<a name="go"><font color="FF0000">
if(abs(dx).lt.eps*abs(x)) go to 20
c</font></a>
15 continue
write(6,*)'Newton Iteration Failed to Converge'
20 stop
end
c</pre>
c</body>
c</html>
cc