The Elk Code
 
Loading...
Searching...
No Matches
minf_nm.f90
Go to the documentation of this file.
1
2! Copyright (C) 2007 J. K. Dewhurst and D. W. H. Rankin.
3! This file is distributed under the terms of the GNU General Public License.
4! See the file COPYING for license details.
5
6subroutine minf_nm(id,rd,n,x,maxit,iter,eps)
7implicit none
8! arguments
9integer, intent(in) :: id(*)
10real(8), intent(in) :: rd(*)
11integer, intent(in) :: n
12real(8), intent(inout) :: x(n,n+1)
13integer, intent(in) :: maxit
14integer, intent(out) :: iter
15real(8), intent(in) :: eps
16! local variables
17integer i,j,il,iu
18! Nelder-Mead parmeters
19real(8), parameter :: alpha=1.d0,gamma=2.d0
20real(8), parameter :: beta=0.5d0,sigma=0.5d0
21real(8) fr,fe,fc,sm,t1
22! automatic arrays
23real(8) f(n+1),xm(n),xr(n),xe(n),xc(n)
24! external functions
25real(8), external :: fmin_nm
26if (n < 1) then
27 write(*,*)
28 write(*,'("Error(minf_nm): n < 1 : ",I8)') n
29 write(*,*)
30 stop
31end if
32! evaluate the function at each vertex
33do i=1,n+1
34 f(i)=fmin_nm(id,rd,x(:,i))
35end do
36iter=0
3710 continue
38iter=iter+1
39if (iter >= maxit) return
40! find the lowest and highest vertex
41il=1
42iu=1
43do i=2,n+1
44 if (f(i) < f(il)) il=i
45 if (f(i) > f(iu)) iu=i
46end do
47! check for convergence
48if ((f(iu)-f(il)) < eps) return
49! compute the mean of the n lowest vertices
50t1=1.d0/dble(n)
51do i=1,n
52 sm=0.d0
53 do j=1,iu-1
54 sm=sm+x(i,j)
55 end do
56 do j=iu+1,n+1
57 sm=sm+x(i,j)
58 end do
59 xm(i)=t1*sm
60end do
61xr(:)=xm(:)+alpha*(xm(:)-x(:,iu))
62fr=fmin_nm(id,rd,xr)
63if (f(il) > fr) goto 30
64if ((f(il) <= fr).and.(fr < f(iu))) then
65! reflection
66 x(:,iu)=xr(:)
67 f(iu)=fr
68 goto 10
69else
70 goto 40
71end if
7230 continue
73xe(:)=xm(:)+gamma*(xr(:)-xm(:))
74fe=fmin_nm(id,rd,xe)
75if (fr > fe) then
76! expansion
77 x(:,iu)=xe(:)
78 f(iu)=fe
79else
80! reflection
81 x(:,iu)=xr(:)
82 f(iu)=fr
83end if
84goto 10
8540 continue
86xc(:)=xm(:)+beta*(x(:,iu)-xm(:))
87fc=fmin_nm(id,rd,xc)
88if (fc < f(iu)) then
89! contraction
90 x(:,iu)=xc(:)
91 f(iu)=fc
92 goto 10
93end if
94! shrinkage
95do j=1,il-1
96 x(:,j)=x(:,il)+sigma*(x(:,j)-x(:,il))
97 f(j)=fmin_nm(id,rd,x(1,j))
98end do
99do j=il+1,n+1
100 x(:,j)=x(:,il)+sigma*(x(:,j)-x(:,il))
101 f(j)=fmin_nm(id,rd,x(1,j))
102end do
103goto 10
104end subroutine
105
real(8) function fmin_nm(id, rd, x)
Definition fmin_nm.f90:7
subroutine minf_nm(id, rd, n, x, maxit, iter, eps)
Definition minf_nm.f90:7