Debian Codegraph @

mex/sources/block_trust_region/test/trust_region_test.f08

raw
1 program trust_region_test
2 use trust_region
3 use iso_fortran_env
4 implicit none (type, external)
5
6 real(real64), dimension(2), parameter :: rosenbrock_guess = [ -10**4, 1 ]
7 real(real64), dimension(2), parameter :: rosenbrock_solution = [ 1, 1 ]
8 real(real64), dimension(2) :: rosenbrock_x
9
10 real(real64), dimension(4), parameter :: powell1_guess = [ 3, -1, 0, 1 ]
11 real(real64), dimension(4), parameter :: powell1_solution = 0
12 real(real64), dimension(4) :: powell1_x
13
14 integer :: info
15
16 rosenbrock_x = rosenbrock_guess
17 call trust_region_solve(rosenbrock_x, rosenbrock, info)
18 if (info /= 1 .or. maxval(abs(rosenbrock_x - rosenbrock_solution)) > 4e-9_real64) &
19 error stop "Failed to solve rosenbrock"
20
21 powell1_x = powell1_guess
22 call trust_region_solve(powell1_x, powell1, info, tolf = 1e-8_real64)
23 if (info /= 1 .or. maxval(abs(powell1_x - powell1_solution)) > 4e-5_real64) &
24 error stop "Failed to solve powell1"
25 contains
26 subroutine rosenbrock(x, fvec, fjac)
27 real(real64), dimension(:), intent(in) :: x
28 real(real64), dimension(size(x)), intent(out) :: fvec
29 real(real64), dimension(size(x),size(x)), intent(out), optional :: fjac
30
31 fvec(1) = 1 - x(1)
32 fvec(2) = 10*(x(2)-x(1)**2)
33
34 if (present(fjac)) then
35 fjac(1,1) = -1
36 fjac(1,2) = 0
37 fjac(2,1) = -20*x(1)
38 fjac(2,2) = 10
39 end if
40 end subroutine rosenbrock
41
42 subroutine powell1(x, fvec, fjac)
43 real(real64), dimension(:), intent(in) :: x
44 real(real64), dimension(size(x)), intent(out) :: fvec
45 real(real64), dimension(size(x),size(x)), intent(out), optional :: fjac
46
47 fvec(1) = x(1)+10*x(2)
48 fvec(2) = sqrt(5._real64)*(x(3)-x(4))
49 fvec(3) = (x(2)-2*x(3))**2
50 fvec(4) = sqrt(10._real64)*(x(1)-x(4))**2
51
52 if (present(fjac)) then
53 fjac = 0
54 fjac(1,1) = 1
55 fjac(1,2) = 10
56 fjac(2,3) = sqrt(5._real64)
57 fjac(2,4) = -fjac(2,3)
58 fjac(3,2) = 2*(x(2)-2*x(3))
59 fjac(3,3) = -2*fjac(3,2)
60 fjac(4,1) = 2*sqrt(10._real64)*(x(1)-x(4))
61 fjac(4,4) = -fjac(4,1)
62 end if
63 end subroutine powell1
64 end program trust_region_test