Skip to content

Commit 774f0b2

Browse files
committed
starting to modernize the examples
1 parent e26542b commit 774f0b2

File tree

2 files changed

+49
-59
lines changed

2 files changed

+49
-59
lines changed

examples/example_hybrd.f90

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,41 @@
55
!> -x(8) + (3-2*x(9))*x(9) = -1
66
program example_hybrd
77

8-
use minpack_module, only: hybrd, enorm, dpmpar
9-
implicit none
10-
integer j, n, maxfev, ml, mu, mode, nprint, info, nfev, ldfjac, lr, nwrite
11-
double precision xtol, epsfcn, factor, fnorm
12-
double precision x(9), fvec(9), diag(9), fjac(9, 9), r(45), qtf(9), &
13-
wa1(9), wa2(9), wa3(9), wa4(9)
14-
15-
!> Logical output unit is assumed to be number 6.
16-
data nwrite/6/
8+
use minpack_module, only: wp, hybrd, enorm, dpmpar
9+
use iso_fortran_env, only: nwrite => output_unit
1710

18-
n = 9
19-
20-
!> The following starting values provide a rough solution.
21-
do j = 1, 9
22-
x(j) = -1.0d0
23-
end do
11+
implicit none
2412

25-
ldfjac = 9
26-
lr = 45
13+
integer,parameter :: n = 9
14+
integer,parameter :: ldfjac = n
15+
integer,parameter :: lr = (n*(n+1))/2
2716

28-
!> Set xtol to the square root of the machine precision.
29-
!> unless high precision solutions are required,
30-
!> this is the recommended setting.
31-
xtol = dsqrt(dpmpar(1))
17+
integer :: maxfev, ml, mu, mode, nprint, info, nfev
18+
real(wp) :: epsfcn, factor, fnorm, xtol
19+
real(wp) :: x(n), fvec(n), diag(n), fjac(n, n), r(lr), qtf(n), &
20+
wa1(n), wa2(n), wa3(n), wa4(n)
3221

22+
xtol = dsqrt(dpmpar(1)) ! square root of the machine precision.
3323
maxfev = 2000
3424
ml = 1
3525
mu = 1
36-
epsfcn = 0.0d0
26+
epsfcn = 0.0_wp
3727
mode = 2
38-
do j = 1, 9
39-
diag(j) = 1.0d0
40-
end do
41-
factor = 1.0d2
28+
factor = 100.0_wp
4229
nprint = 0
30+
diag = 1.0_wp
31+
x = -1.0_wp ! starting values to provide a rough solution.
4332

4433
call hybrd(fcn, n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, &
4534
mode, factor, nprint, info, nfev, fjac, ldfjac, &
4635
r, lr, qtf, wa1, wa2, wa3, wa4)
4736
fnorm = enorm(n, fvec)
48-
write (nwrite, 1000) fnorm, nfev, info, (x(j), j=1, n)
4937

50-
1000 format(5x, "FINAL L2 NORM OF THE RESIDUALS", d15.7// &
51-
5x, "NUMBER OF FUNCTION EVALUATIONS", i10// &
52-
5x, "EXIT PARAMETER", 16x, i10// &
53-
5x, "FINAL APPROXIMATE SOLUTION"//(5x, 3d15.7))
38+
write (nwrite, '(5x,a,d15.7//5x,a,i10//5x,a,16x,i10//5x,a//(5x,3d15.7))') &
39+
"FINAL L2 NORM OF THE RESIDUALS", fnorm, &
40+
"NUMBER OF FUNCTION EVALUATIONS", nfev, &
41+
"EXIT PARAMETER", info, &
42+
"FINAL APPROXIMATE SOLUTION", x
5443

5544
!> Results obtained with different compilers or machines
5645
!> may be slightly different.
@@ -75,28 +64,29 @@ subroutine fcn(n, x, fvec, iflag)
7564
implicit none
7665
integer, intent(in) :: n
7766
integer, intent(inout) :: iflag
78-
double precision, intent(in) :: x(n)
79-
double precision, intent(out) :: fvec(n)
80-
81-
integer k
82-
double precision one, temp, temp1, temp2, three, two, zero
83-
data zero, one, two, three/0.0d0, 1.0d0, 2.0d0, 3.0d0/
84-
85-
if (iflag /= 0) go to 5
86-
87-
!! Insert print statements here when nprint is positive.
88-
89-
return
90-
5 continue
91-
do k = 1, n
92-
temp = (three - two*x(k))*x(k)
93-
temp1 = zero
94-
if (k /= 1) temp1 = x(k - 1)
95-
temp2 = zero
96-
if (k /= n) temp2 = x(k + 1)
97-
fvec(k) = temp - temp1 - two*temp2 + one
98-
end do
99-
return
67+
real(wp), intent(in) :: x(n)
68+
real(wp), intent(out) :: fvec(n)
69+
70+
integer :: k !! counter
71+
real(wp) :: temp, temp1, temp2
72+
73+
real(wp),parameter :: zero = 0.0_wp
74+
real(wp),parameter :: one = 1.0_wp
75+
real(wp),parameter :: two = 2.0_wp
76+
real(wp),parameter :: three = 3.0_wp
77+
78+
if (iflag == 0) then
79+
!! Insert print statements here when nprint is positive.
80+
else
81+
do k = 1, n
82+
temp = (three - two*x(k))*x(k)
83+
temp1 = zero
84+
if (k /= 1) temp1 = x(k - 1)
85+
temp2 = zero
86+
if (k /= n) temp2 = x(k + 1)
87+
fvec(k) = temp - temp1 - two*temp2 + one
88+
end do
89+
end if
10090

10191
end subroutine fcn
10292

examples/example_hybrd1.f90

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
!> -x(8) + (3-2*x(9))*x(9) = -1
77
program example_hybrd1
88

9-
use minpack_module, only: hybrd1, dpmpar, enorm
9+
use minpack_module, only: wp, hybrd1, dpmpar, enorm
1010
implicit none
1111
integer j, n, info, lwa, nwrite
12-
double precision tol, fnorm
13-
double precision x(9), fvec(9), wa(180)
12+
real(wp) tol, fnorm
13+
real(wp) x(9), fvec(9), wa(180)
1414

1515
!> Logical output unit is assumed to be number 6.
1616
data nwrite/6/
@@ -59,11 +59,11 @@ subroutine fcn(n, x, fvec, iflag)
5959
implicit none
6060
integer, intent(in) :: n
6161
integer, intent(inout) :: iflag
62-
double precision, intent(in) :: x(n)
63-
double precision, intent(out) :: fvec(n)
62+
real(wp), intent(in) :: x(n)
63+
real(wp), intent(out) :: fvec(n)
6464

6565
integer k
66-
double precision one, temp, temp1, temp2, three, two, zero
66+
real(wp) one, temp, temp1, temp2, three, two, zero
6767
data zero, one, two, three/0.0d0, 1.0d0, 2.0d0, 3.0d0/
6868

6969
do k = 1, n

0 commit comments

Comments
 (0)