PROGRAM deriv1
!
! Author: Br. David Carlson
!
! Date: January 4, 2000
!
! Revised: May 2, 2004
!
! This program finds the approximate value of the derivative of function f(x)
! = 1 / x at a particular value of x. Suggested values of x to try: 0.1, 0.05.
! Just for fun try x = 0.
IMPLICIT NONE
REAL::delta, delta2, x, derivf, derivc
! Function used:
REAL::f
WRITE (*, *) ' Enter value of x at which to evaluate the derivative:'
READ (*, *) x
delta = 2.0E-5
delta2 = 1.0E-5
derivf = (f(x + delta) - f(x)) / delta
derivc = (f(x + delta2) - f(x - delta2)) / delta
WRITE (*, *) 'The forward difference approx. of the derivative is: ', derivf
WRITE (*, *) 'The central difference approx. of the derivative is: ', derivc
WRITE (*, *) 'Using calculus, the value of the derivative is: ', &
-1.0 / (x * x)
END PROGRAM
! Given: x a real value
! Task: Compute f(x) and return it.
! Return: Computed value in function name.
REAL FUNCTION f(x)
IMPLICIT NONE
REAL, INTENT(IN)::x
f = 1.0 / x
END FUNCTION