|
| 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 |
0 commit comments