Skip to content

Commit 17fc00d

Browse files
authored
Merge pull request #4 from fortran-fans/add_hybrd_interface
Add hybrd1 interface and example.
2 parents 14ed8c2 + be736da commit 17fc00d

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

README.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ of the Jacobian matrix with the functions.
2121

2222
Jorge Moré, Burt Garbow, and Ken Hillstrom at Argonne National Laboratory.
2323

24+
Build with `fortran-lang/fpm <https://github.com/fortran-lang/fpm>`_
25+
--------------------------------------------------------------------
26+
27+
Fortran Package Manager (fpm) is a great package manager and build system for Fortran.
28+
29+
You can build using provided ``fpm.toml``:
30+
31+
.. code-block:: bash
32+
33+
fpm build
34+
fpm run --example <example_name, see ``fpm.toml``>
35+
36+
37+
To use ``minpack`` within your fpm project, add the following to your ``fpm.toml`` file:
38+
39+
.. code-block:: toml
40+
41+
[dependencies]
42+
minpack = { git="https://github.com/certik/minpack.git" }
43+
2444
Documentation
2545
-------------
2646

examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include_directories(${PROJECT_BINARY_DIR}/src)
22

3+
add_executable(example_hybrd1 example_hybrd1.f90)
4+
target_link_libraries(example_hybrd1 minpack)
5+
36
add_executable(example_lmder1 example_lmder1.f90)
47
target_link_libraries(example_lmder1 minpack)
58

examples/example_hybrd1.f90

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
!> the problem is to determine the values of x(1), x(2), ..., x(9),
2+
!> which solve the system of tridiagonal equations.
3+
!>
4+
!> (3-2*x(1))*x(1) -2*x(2) = -1
5+
!> -x(i-1) + (3-2*x(i))*x(i) -2*x(i+1) = -1, i=2-8
6+
!> -x(8) + (3-2*x(9))*x(9) = -1
7+
program example_hybrd1
8+
9+
use minpack, only: hybrd1
10+
implicit none
11+
integer j, n, info, lwa, nwrite
12+
double precision tol, fnorm
13+
double precision x(9), fvec(9), wa(180)
14+
double precision enorm, dpmpar
15+
16+
data nwrite/6/
17+
18+
n = 9
19+
20+
!> The following starting values provide a rough solution.
21+
do j = 1, 9
22+
x(j) = -1.d0
23+
end do
24+
25+
lwa = 180
26+
tol = dsqrt(dpmpar(1))
27+
28+
call hybrd1(fcn, n, x, fvec, tol, info, wa, lwa)
29+
fnorm = enorm(n, fvec)
30+
write (nwrite, 1000) fnorm, info, (x(j), j=1, n)
31+
32+
1000 format(5x, "FINAL L2 NORM OF THE RESIDUALS", d15.7// &
33+
5x, "EXIT PARAMETER", 16x, i10// &
34+
5x, "FINAL APPROXIMATE SOLUTION"// &
35+
(5x, 3d15.7))
36+
37+
!> Results obtained with different compilers or machines
38+
!> may be slightly different.
39+
!>
40+
!>> FINAL L2 NORM OF THE RESIDUALS 0.1192636D-07
41+
!>>
42+
!>> EXIT PARAMETER 1
43+
!>>
44+
!>> FINAL APPROXIMATE SOLUTION
45+
!>>
46+
!>> -0.5706545D+00 -0.6816283D+00 -0.7017325D+00
47+
!>> -0.7042129D+00 -0.7013690D+00 -0.6918656D+00
48+
!>> -0.6657920D+00 -0.5960342D+00 -0.4164121D+00
49+
50+
contains
51+
52+
!> subroutine fcn for hybrd1 example.
53+
subroutine fcn(n, x, fvec, iflag)
54+
55+
implicit none
56+
integer n, iflag
57+
double precision x(n), fvec(n)
58+
59+
integer k
60+
double precision one, temp, temp1, temp2, three, two, zero
61+
data zero, one, two, three/0.0d0, 1.0d0, 2.0d0, 3.0d0/
62+
63+
do k = 1, n
64+
temp = (three - two*x(k))*x(k)
65+
temp1 = zero
66+
if (k /= 1) temp1 = x(k - 1)
67+
temp2 = zero
68+
if (k /= n) temp2 = x(k + 1)
69+
fvec(k) = temp - temp1 - two*temp2 + one
70+
end do
71+
72+
end subroutine fcn
73+
74+
end program example_hybrd1

fpm.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ auto-examples = false
1717
[install]
1818
library = false
1919

20+
[[ example ]]
21+
name = "example_hybrd1"
22+
source-dir = "examples"
23+
main = "example_hybrd1.f90"
24+
2025
[[ example ]]
2126
name = "example_lmder1"
2227
source-dir = "examples"

src/minpack.f90

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ double precision function enorm(n,x)
1212
double precision x(n)
1313
end function
1414

15+
!> The purpose of `hybrd1` is to find a zero of a system of
16+
!> n nonlinear functions in n variables by a modification
17+
!> of the powell hybrid method.
18+
subroutine hybrd1(fcn,n,x,fvec,tol,info,wa,lwa)
19+
integer n,info,lwa
20+
double precision tol
21+
double precision x(n),fvec(n),wa(lwa)
22+
interface
23+
subroutine fcn(n,x,fvec,iflag)
24+
integer n,iflag
25+
double precision x(n),fvec(n)
26+
end subroutine fcn
27+
end interface
28+
end subroutine hybrd1
29+
1530
subroutine lmder1(fcn,m,n,x,fvec,fjac,ldfjac,tol,info,ipvt,wa,lwa)
1631
integer m,n,ldfjac,info,lwa
1732
integer ipvt(n)

0 commit comments

Comments
 (0)