Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.       program LUfactor
  2.      
  3.       implicit none
  4.       !declare variables
  5.       integer, parameter :: dkind = selected_real_kind(15,307)
  6.       integer :: i,j,n,k
  7.       real (kind=dkind), allocatable, dimension(:,:) :: A, L, U
  8.       real (kind=dkind), allocatable, dimension(:) :: x, b, y
  9.      
  10.       !Prompt user for the order of the matrix
  11.       print*, "Enter order of matrix"
  12.       read*, n
  13.      
  14.       !allocate matrices
  15.       allocate(A(n,n))
  16.       allocate(b(n))
  17.       allocate(x(n))
  18.       allocate(L(n,n))
  19.       allocate(U(n,n))
  20.       allocate(y(n))
  21.      
  22.       !Enter in entries of the given A matrix
  23.       do i = 1,n
  24.         do j = 1, n
  25.         print*,"enter elements of A ", i,j
  26.         read*,A(i,j)
  27.         enddo
  28.       enddo
  29.      
  30.       !Enter in entries of the column vector b
  31.       do i = 1,n
  32.         print*, "enter column vector b elements ", i
  33.         read*,b(i)
  34.       enddo
  35.      
  36.       !stop program if one of the diagonal elements = 0
  37.       do k=1,n
  38.         if (A(k,k) == 0.0) then
  39.         print*,'A has no LU factorization'
  40.         stop
  41.         end if
  42.       end do
  43.      
  44.       !Factor A into L*U
  45.       do k=1,n-1
  46.         A(k+1:n,k) = A(k+1:n,k)/A(k,k)
  47.         forall (i=k+1:n,j=k+1:n)
  48.             A(i,j) = A(i,j) - A(i,k)*A(k,j)
  49.         end forall
  50.       end do
  51.  
  52.      
  53.       !Extract L and U from the new A matrix
  54.       L=0
  55.       U=0
  56.       do i=1,n
  57.         L(i,i) = 1
  58.       end do
  59.       do j=1,n
  60.         do i=1,n
  61.             if (i .gt. j) then
  62.             L(i,j) = A(i,j)
  63.             else
  64.             U(i,j) = A(i,j)
  65.             end if
  66.         end do
  67.       end do
  68.      
  69.       print *, 'L:'
  70.       do i = 1,n
  71.         print *, (L(i,j), j=1,n)
  72.       enddo
  73.       print *, 'U:'
  74.       do i = 1,n
  75.         print *, (U(i,j), j=1,n)
  76.       enddo
  77.  
  78.  
  79.       !Forward substitution Ly=b
  80.       do k=1,n
  81.         do i=1,k-1
  82.             b(k) = b(k)-L(k,i)*b(i)
  83.         end do
  84.         y(k) = b(k)/L(k,k)
  85.       end do
  86.       print *, 'y:', (y(k),k=1,n)
  87.  
  88.       !Back substitution Ux=y
  89.       do k=n,1,-1
  90.         do i=k+1,n
  91.           y(k) = y(k) - U(k,i)*x(i)
  92.         end do
  93.         x(k) = y(k)/U(k,k)
  94.       end do
  95.       print *, 'x:', (x(k),k=1,n)
  96.      
  97.       deallocate(A)
  98.       deallocate(b)
  99.       deallocate(x)
  100.       deallocate(L)
  101.       deallocate(U)
  102.       deallocate(y)
  103.      
  104.       end program LUfactor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement