Below is a modern Fortran implementation of the mynewton subroutine that uses Newton's method to find the minimum of a function f. The subroutine handles both scalar and vector-valued versions of x and r using Fortran's array-oriented features. The Jacobian is calculated via finite differences in a separate subroutine.
module newton_module
implicit none
private
public :: mynewton, generic_f, fd_jacobian
interface
subroutine generic_f(x, r)
import :: dp
real(dp), intent(in) :: x(:)
real(dp), intent(out) :: r(:)
end subroutine generic_f
end interface
integer, parameter :: dp = kind(0.d0)
real(dp), parameter :: eps = 1.0e-6_dp
integer, parameter :: max_iter = 100
contains
subroutine mynewton(f, x)
procedure(generic_f) :: f
real(dp), intent(inout) :: x(:)
real(dp), allocatable :: r(:), J(:, :), delta_x(:)
integer :: n, iter
n = size(x)
allocate(r(n), J(n, n), delta_x(n))
do iter = 1, max_iter
call f(x, r)
if (norm2(r) < eps) exit
call fd_jacobian(f, x, J)
delta_x = -matmul(solve(J), r)
x = x + delta_x
if (norm2(delta_x) < eps) exit
end do
deallocate(r, J, delta_x)
end subroutine mynewton
subroutine fd_jacobian(f, x, J)
procedure(generic_f) :: f
real(dp), intent(in) :: x(:)
real(dp), intent(out) :: J(:, :)
real(dp), allocatable :: r0(:), rp(:), x_pert(:)
real(dp) :: h
integer :: i, n
n = size(x)
h = sqrt(eps)
allocate(r0(n), rp(n), x_pert(n))
call f(x, r0)
x_pert = x
do i = 1, n
x_pert(i) = x(i) + h
call f(x_pert, rp)
J(:, i) = (rp - r0) / h
x_pert(i) = x(i)
end do
deallocate(r0, rp, x_pert)
end subroutine fd_jacobian
function solve(A) result(Ainverse)
real(dp), intent(in) :: A(:, :)
real(dp) :: Ainverse(size(A, 1), size(A, 2))
integer :: n, info
real(dp), allocatable :: work(:)
integer, allocatable :: ipiv(:)
n = size(A, 1)
allocate(work(n), ipiv(n))
Ainverse = A
call dgesv(n, n, Ainverse, n, ipiv, work, n, info)
if (info /= 0) then
error stop "Matrix is singular or not invertible."
end if
deallocate(work, ipiv)
end function solve
end module newton_module
! Example usage with a generic function
program test_newton
use newton_module
implicit none
real(dp) :: x(2)
x = [2.0_dp, 1.0_dp]
call mynewton(f=rosenbrock, x=x)
print *, "Minimum found at:", x
contains
subroutine rosenbrock(x, r)
real(dp), intent(in) :: x(:)
real(dp), intent(out) :: r(:)
r(1) = 1.0_dp - x(1)
r(2) = 10.0_dp*(x(2) - x(1)**2)
end subroutine rosenbrock
end program test_newton
Explanation:
- Module Structure: The code is encapsulated in a module
newton_module for better organization and reusability.
- Interface Block: The
generic_f interface ensures that the user-provided function has the correct signature.
- Newton's Method: The
mynewton subroutine iteratively updates x using Newton's method. It calculates the Jacobian via finite differences and solves the linear system using LAPACK's dgesv.
- Finite Difference Jacobian: The
fd_jacobian subroutine computes the Jacobian matrix using central finite differences.
- Matrix Inversion: The
solve function inverts the Jacobian matrix using LAPACK's dgesv routine.
- Example Usage: The
test_newton program demonstrates how to use mynewton with the Rosenbrock function.
This implementation is flexible, handling both scalar and array inputs, and separates concerns by modularizing the Jacobian calculation.