Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. x-2y-z=2
  2. 5x+2y+2z=9
  3. -3x+5y-z=1
  4.  
  5. 1 2 -1
  6.  
  7. 5 2 2
  8.  
  9. -3 5 -1
  10.  
  11. 2
  12. 9
  13. 1
  14.  
  15. Option Explicit
  16.  
  17. Sub GaussElim()
  18. Dim n As Integer, er As Integer, i As Integer
  19. Dim a(10, 10) As Double, b(10) As Double, x(10) As Double
  20. n = 3
  21. a(1, 1) = 1: a(1, 2) = 2: a(1, 3) = -1
  22. a(2, 1) = 5: a(2, 2) = 2: a(2, 3) = 2
  23. a(3, 1) = -3: a(3, 2) = 5: a(3, 3) = -1
  24. b(1) = 2: b(2) = 9: b(3) = 1
  25. Call Gauss(a, b, n, x, er)
  26. If er = 0 Then
  27. For i = 1 To n
  28. MsgBox "x(" & i & ") = " & x(i)
  29. Next i
  30. Else
  31. MsgBox "ill-conditioned system"
  32. End If
  33. End Sub
  34.  
  35. Sub Gauss(a, b, n, x, er)
  36. Dim i As Integer, j As Integer
  37. Dim s(10) As Double
  38. Const tol As Double = 0.000001
  39. er = 0
  40. For i = 1 To n
  41. s(i) = Abs(a(i, 1))
  42. For j = 2 To n
  43. If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j))
  44. Next j
  45. Next i
  46. Call Eliminate(a, s, n, b, tol, er)
  47. If er <> -1 Then
  48. Call Substitute(a, n, b, x)
  49. End If
  50. End Sub
  51.  
  52. Sub Pivot(a, b, s, n, k)
  53. Dim p As Integer, ii As Integer, jj As Integer
  54. Dim factor As Double, big As Double, dummy As Double
  55. p = k
  56. big = Abs(a(k, k) / s(k))
  57. For ii = k + 1 To n
  58. dummy = Abs(a(ii, k) / s(ii))
  59. If dummy > big Then
  60. big = dummy
  61. p = ii
  62. End If
  63. Next ii
  64. If p <> k Then
  65. For jj = k To n
  66. dummy = a(p, jj)
  67. a(p, jj) = a(k, jj)
  68. a(k, jj) = dummy
  69. Next jj
  70. dummy = b(p)
  71. b(p) = b(k)
  72. b(k) = dummy
  73. dummy = s(p)
  74. s(p) = s(k)
  75. s(k) = dummy
  76. End If
  77. End Sub
  78.  
  79. Sub Substitute(a, n, b, x)
  80. Dim i As Integer, j As Integer
  81. Dim sum As Double
  82. x(n) = b(n) / a(n, n)
  83. For i = n - 1 To 1 Step -1
  84. sum = 0
  85. For j = i + 1 To n
  86. sum = sum + a(i, j) * x(j)
  87. Next j
  88. x(i) = (b(i) - sum) / a(i, i)
  89. Next i
  90. End Sub
  91.  
  92. Sub Eliminate(a, s, n, b, tol, er)
  93. Dim i As Integer, j As Integer, k As Integer
  94. Dim factor As Double
  95. For k = 1 To n - 1
  96. Call Pivot(a, b, s, n, k)
  97. If Abs(a(k, k) / s(k)) < tol Then
  98. er = -1
  99. Exit For
  100. End If
  101. For i = k + 1 To n
  102. factor = a(i, k) / a(k, k)
  103. For j = k + 1 To n
  104. a(i, j) = a(i, j) - factor * a(k, j)
  105. Next j
  106. b(i) = b(i) - factor * b(k)
  107. Next i
  108. Next k
  109. If Abs(a(k, k) / s(k)) < tol Then er = -1
  110. End Sub
  111.  
  112. program Gauss_Emlimination !with partial pivoting
  113. implicit none
  114. INTEGER n, i, j
  115. REAL A(3,3), B(3), X(3), ER, tol
  116. tol = 0.000001
  117. n = 3
  118. OPEN(UNIT=2, FILE='INPUT1.DAT')
  119. OPEN(UNIT=3, FILE='INPUT2.DAT')
  120. OPEN(UNIT=4, FILE='RESULT.DAT')
  121. READ(2,*) ((A(I,J),J=1,3),I=1,3)
  122. READ(3,*) (B(I), I=1,3)
  123. CALL Gauss(a, b, n, x, er)
  124. IF (er .EQ. 0) THEN
  125. DO i =1, N
  126. WRITE(4,*) X(i)
  127. END DO
  128. ELSE
  129. WRITE(4,*) "ill-conditioned system"
  130. END IF
  131. contains
  132.  
  133.  
  134. SUBROUTINE Gauss(a, b, n, x, er)
  135. real, intent(inout) :: a(3,3)
  136. real, intent(inout) :: b(3)
  137. integer, intent(in) :: n
  138. real, intent(out) :: x(N)
  139. REAL, intent(out) :: er
  140. real, dimension(10) :: S(10)
  141. INTEGER I, J
  142. ER=0
  143. DO I= 1, N
  144. s(i) = ABS(A(i,1))
  145. DO j = 2, n
  146. IF (ABS(A(i,j)) .GT. s(i)) THEN
  147. s(i) = ABS(A(i,j))
  148. END IF
  149. END DO
  150. END DO
  151. CALL Eliminate(a, s, n, b, tol, er)
  152. IF (er .ne. -1) THEN
  153. CALL Substitute(a, n, b, x)
  154. END IF
  155. END SUBROUTINE Gauss
  156.  
  157. SUBROUTINE Pivot(a, b, s, n, k)
  158. INTEGER ii, jj
  159. real, intent(inout) :: a(3,3)
  160. real, intent(inout) :: b(3)
  161. integer, intent(in) :: n, K
  162. integer p
  163. real, dimension(10) :: S(10)
  164. DOUBLE PRECISION big, dummy, factor
  165. p = k
  166. big = ABS(A(k,k)/S(k))
  167. DO ii = k+1, n
  168. dummy = ABS(A(ii, k)/S(ii))
  169. IF (dummy .GT. big) THEN
  170. big = dummy
  171. p = ii
  172. END IF
  173. END DO
  174. IF (p .ne. k) THEN
  175. DO jj = k, n
  176. dummy = A(p, jj)
  177. A(p, jj) = A(k, jj)
  178. A(k, jj) = dummy
  179. END DO
  180. dummy = B(p)
  181. B(p) = B(k)
  182. B(k) = dummy
  183. dummy = S(p)
  184. S(p) = S(k)
  185. S(k) = dummy
  186. END IF
  187. END SUBROUTINE Pivot
  188.  
  189. SUBROUTINE Substitute(a, n, b, x)
  190. INTEGER i, j
  191. real, intent(inout) :: a(3,3)
  192. real, intent(inout) :: b(3)
  193. integer, intent(in) :: n
  194. real, intent(out) :: x(N)
  195. DOUBLE PRECISION sum
  196. X(n) = B(n)/A(n, n)
  197. DO i = n-1, 1, -1
  198. sum = 0
  199. DO j = i+1, n
  200. sum = sum +A(i, j)*X(j)
  201. END DO
  202. X(n) = (B(n)-sum)/A(n,n)
  203. END DO
  204. END SUBROUTINE Substitute
  205.  
  206. SUBROUTINE Eliminate(a, s, n, b, tol, er)
  207. real, intent(in) :: tol
  208. real, intent(inout) :: a(3,3)
  209. real, intent(inout) :: b(3)
  210. integer, intent(in) :: n
  211. real, dimension(10) :: S(10)
  212. real, intent(INout) :: er
  213. INTEGER i, j, k
  214. DOUBLE PRECISION factor
  215. DO K = 1, N-1
  216. CALL Pivot (a, b, s, n, k)
  217. IF (ABS(A(K,K)/S(K)) .LT. tol) THEN
  218. er=-1
  219. EXIT
  220. END IF
  221. DO i = k+1, n
  222. factor = A(i,k)/A(k,k)
  223. DO j= k+1, n
  224. A(i,j) = A(i,j) - factor*B(k)
  225. END DO
  226. B(i) = B(i) - factor * B(k)
  227. END DO
  228. END DO
  229. IF (ABS(A(n,n)/S(n)) .LT. tol) THEN
  230. er= -1
  231. END IF
  232. END SUBROUTINE Eliminate
  233.  
  234. end program Gauss_Emlimination
  235.  
  236. module gauss_mod
  237.  
  238. implicit none
  239.  
  240. contains
  241.  
  242. ! your code here
  243.  
  244. end module gauss_mod
  245.  
  246. s(i) = ABS(A(i,1))
  247.  
  248. double precision, dimension(:,:) :: A
  249.  
  250. module gauss_mod
  251.  
  252. implicit none
  253.  
  254. integer, parameter :: dp = selected_real_kind(15)
  255.  
  256. contains
  257.  
  258. ! your code here
  259.  
  260. ! As an example:
  261. subroutine gauss(a, b, n, x, er)
  262. ! dummy arguments
  263. real(kind=dp), dimension(:,:), intent(in) :: a, b
  264. integer, intent(in) :: n
  265. real(kind=dp), dimension(:), intent(out) :: x
  266. real(kind=dp), intent(out) :: er
  267.  
  268. real(kind=dp), dimension(10) :: S(10)
  269. real(kind=dp), parameter :: tol = 0.000001
  270.  
  271. ! rest of subroutine
  272. end subroutine gauss
  273.  
  274. end module gauss_mod
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement