Certainly! Here’s how you can expand the mynewton interface to accept an optional options argument, which is a derived type containing maxIter, maxFunEvals, and tolX. The subroutine will use default values if options is not provided.
Below is a complete, modern Fortran implementation for both scalar and vector cases, with the requested options handling. The options are passed as an optional argument, and the subroutine uses them if present, otherwise defaults are used.
fortran
module newton_method
implicit none
private
public :: mynewton, newton_options
! Options type
type :: newton_options
integer :: maxIter = 100
integer :: maxFunEvals = 1000
real(8) :: tolX = 1.0e-10_8
end type newton_options
! Abstract interfaces for scalar and vector functions
abstract interface
subroutine scalar_func(x, r)
real(8), intent(in) :: x
real(8), intent(out) :: r
end subroutine scalar_func
end interface
abstract interface
subroutine vector_func(x, r)
real(8), intent(in) :: x(:)
real(8), intent(out) :: r(:)
end subroutine vector_func
end interface
! Generic interface for Newton's method
interface mynewton
module procedure mynewton_scalar
module procedure mynewton_vector
end interface mynewton
real(8), parameter :: eps = 1.0e-8_8
contains
! Newton's method for scalar functions
subroutine mynewton_scalar(f, x, options)
procedure(scalar_func) :: f
real(8), intent(inout) :: x
type(newton_options), intent(in), optional :: options
integer :: maxIter, maxFunEvals, iter, funEvals
real(8) :: tolX
real(8) :: r, dfdx, dx, x_old
! Set options
if (present(options)) then
maxIter = options%maxIter
maxFunEvals = options%maxFunEvals
tolX = options%tolX
else
maxIter = 100
maxFunEvals = 1000
tolX = 1.0e-10_8
end if
funEvals = 0
do iter = 1, maxIter
call f(x, r)
funEvals = funEvals + 1
if (abs(r) < tolX) then
print *, 'Converged in', iter, 'iterations,', funEvals, 'function evaluations'
return
end if
call fd_derivative(f, x, dfdx)
funEvals = funEvals + 1
if (dfdx == 0.0_8) then
print *, 'Zero derivative encountered. Stopping.'
return
end if
x_old = x
dx = -r / dfdx
x = x + dx
if (abs(x - x_old) < tolX) then
print *, 'Step size below tolX. Converged in', iter, 'iterations,', funEvals, 'function evaluations'
return
end if
if (funEvals >= maxFunEvals) then
print *, 'Maximum function evaluations reached.'
return
end if
end do
print *, 'Warning: Max iterations reached without convergence'
end subroutine mynewton_scalar
! Newton's method for vector functions
subroutine mynewton_vector(f, x, options)
procedure(vector_func) :: f
real(8), intent(inout) :: x(:)
type(newton_options), intent(in), optional :: options
integer :: maxIter, maxFunEvals, iter, funEvals, n
real(8) :: tolX, norm_dx
real(8), allocatable :: r(:), dx(:), jac(:,:), x_old(:)
! Set options
if (present(options)) then
maxIter = options%maxIter
maxFunEvals = options%maxFunEvals
tolX = options%tolX
else
maxIter = 100
maxFunEvals = 1000
tolX = 1.0e-10_8
end if
n = size(x)
allocate(r(n), dx(n), jac(n,n), x_old(n))
funEvals = 0
do iter = 1, maxIter
call f(x, r)
funEvals = funEvals + 1
if (sqrt(sum(r**2)) < tolX) then
print *, 'Converged in', iter, 'iterations,', funEvals, 'function evaluations'
return
end if
call fd_jacobian(f, x, jac)
funEvals = funEvals + n
dx = -r
call solve_linear_system(jac, dx)
x_old = x
x = x + dx
norm_dx = sqrt(sum((x - x_old)**2))
if (norm_dx < tolX) then
print *, 'Step size below tolX. Converged in', iter, 'iterations,', funEvals, 'function evaluations'
return
end if
if (funEvals >= maxFunEvals) then
print *, 'Maximum function evaluations reached.'
return
end if
end do
print *, 'Warning: Max iterations reached without convergence'
end subroutine mynewton_vector
! Finite difference derivative for scalar functions
subroutine fd_derivative(f, x, dfdx)
procedure(scalar_func) :: f
real(8), intent(in) :: x
real(8), intent(out) :: dfdx
real(8) :: f_x, f_xh, h
h = eps * max(abs(x), 1.0_8)
call f(x, f_x)
call f(x + h, f_xh)
dfdx = (f_xh - f_x) / h
end subroutine fd_derivative
! Finite difference Jacobian for vector functions
subroutine fd_jacobian(f, x, jac)
procedure(vector_func) :: f
real(8), intent(in) :: x(:)
real(8), intent(out) :: jac(:,:)
real(8), allocatable :: r0(:), r_pert(:), x_pert(:)
integer :: j, n
real(8) :: h
n = size(x)
allocate(r0(n), r_pert(n), x_pert(n))
call f(x, r0)
do j = 1, n
x_pert = x
h = eps * max(abs(x(j)), 1.0_8)
x_pert(j) = x(j) + h
call f(x_pert, r_pert)
jac(:, j) = (r_pert - r0) / h
end do
end subroutine fd_jacobian
! Linear system solver using Gaussian elimination
subroutine solve_linear_system(A, b)
real(8), intent(inout) :: A(:,:)
real(8), intent(inout) :: b(:)
integer :: n, i, j, k, pivot_row
real(8) :: factor, max_val
real(8), allocatable :: temp(:)
n = size(b)
allocate(temp(n))
! Forward elimination with partial pivoting
do k = 1, n-1
! Find pivot row
pivot_row = k
max_val = abs(A(k,k))
do i = k+1, n
if (abs(A(i,k)) > max_val) then
max_val = abs(A(i,k))
pivot_row = i
end if
end do
! Swap rows if necessary
if (pivot_row /= k) then
temp = A(k,:)
A(k,:) = A(pivot_row,:)
A(pivot_row,:) = temp
factor = b(k)
b(k) = b(pivot_row)
b(pivot_row) = factor
end if
! Eliminate below
do i = k+1, n
factor = A(i,k) / A(k,k)
A(i,k+1:n) = A(i,k+1:n) - factor * A(k,k+1:n)
b(i) = b(i) - factor * b(k)
A(i,k) = 0.0_8
end do
end do
! Back substitution
do i = n, 1, -1
b(i) = (b(i) - dot_product(A(i,i+1:n), b(i+1:n))) / A(i,i)
end do
end subroutine solve_linear_system
end module newton_method
! Example usage
program test_newton
use newton_method
implicit none
real(8) :: x_scalar, x_vec(2)
type(newton_options) :: opts
! Set custom options
opts%maxIter = 50
opts%maxFunEvals = 200
opts%tolX = 1.0e-12_8
! Test scalar function: find root of x^2 - 4 = 0
print *, "Solving scalar equation: x^2 - 4 = 0"
x_scalar = 3.0_8
call mynewton(scalar_f, x_scalar, opts)
print *, "Solution:", x_scalar
print *
! Test vector function: solve system of equations
print *, "Solving vector system:"
print *, " x^2 + y^2 = 4"
print *, " x - y = 0"
x_vec = [1.5_8, 1.5_8]
call mynewton(vector_f, x_vec, opts)
print *, "Solution:", x_vec
contains
! Scalar function example
subroutine scalar_f(x, r)
real(8), intent(in) :: x
real(8), intent(out) :: r
r = x**2 - 4.0_8
end subroutine scalar_f
! Vector function example
subroutine vector_f(x, r)
real(8), intent(in) :: x(:)
real(8), intent(out) :: r(:)
r(1) = x(1)**2 + x(2)**2 - 4.0_8
r(2) = x(1) - x(2)
end subroutine vector_f
end program test_newton
How to use:
For default options, just call mynewton(f, x).
For custom options, create and set a newton_options variable, then call mynewton(f, x, options).
The subroutine will automatically use the correct version for scalar or vector functions.