Skip to content

Commit dde41b6

Browse files
committed
Check derivatives using chkder()
1 parent 9787b2d commit dde41b6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

examples/example_lmder1.f90

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module testmod
1+
module testmod_der1
22
implicit none
33
private
44
public fcn, dp
@@ -43,8 +43,8 @@ subroutine fcn(m, n, x, fvec, fjac, ldfjac, iflag)
4343

4444

4545
program example_lmder1
46-
use minpack, only: enorm, dpmpar, lmder1
47-
use testmod, only: dp, fcn
46+
use minpack, only: enorm, dpmpar, lmder1, chkder
47+
use testmod_der1, only: dp, fcn
4848
implicit none
4949

5050
integer :: info
@@ -55,6 +55,8 @@ program example_lmder1
5555
! The following starting values provide a rough fit.
5656
x = [1._dp, 1._dp, 1._dp]
5757

58+
call check_deriv()
59+
5860
! Set tol to the square root of the machine precision. Unless high precision
5961
! solutions are required, this is the recommended setting.
6062
tol = sqrt(dpmpar(1))
@@ -67,4 +69,20 @@ program example_lmder1
6769
5x, 'EXIT PARAMETER', 16x, i10 // &
6870
5x, 'FINAL APPROXIMATE SOLUTION' // &
6971
5x, 3d15.7)
72+
73+
contains
74+
75+
subroutine check_deriv()
76+
real(dp) :: xp(size(x)), fvecp(size(fvec)), err(size(fvec))
77+
call chkder(size(fvec), size(x), x, fvec, fjac, size(fjac, 1), xp, fvecp, &
78+
1, err)
79+
call fcn(size(fvec), size(x), x, fvec, fjac, size(fjac, 1), 1)
80+
call fcn(size(fvec), size(x), x, fvec, fjac, size(fjac, 1), 2)
81+
call fcn(size(fvec), size(x), xp, fvecp, fjac, size(fjac, 1), 1)
82+
call chkder(size(fvec), size(x), x, fvec, fjac, size(fjac, 1), xp, fvecp, &
83+
2, err)
84+
print *, "Derivatives check (1.0 is correct, 0.0 is incorrect):"
85+
print *, err
86+
end subroutine
87+
7088
end program

0 commit comments

Comments
 (0)