# FORTRAN 90: Eigenvalues and Eigenvectors of a Matrix

Page 1 of 1

## 3 Replies - 19920 Views - Last Post: 18 April 2010 - 09:20 AM

### #1 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

# FORTRAN 90: Eigenvalues and Eigenvectors of a Matrix

Posted 28 September 2007 - 08:16 AM

Description: !Example Program PROGRAM EIGEN_MAT IMPLICIT NONE REAL, ALLOCATABLE, DIMENSION(:,:) :: Matrix REAL, ALLOCATABLE, DIMENSION(:) :: x REAL :: EigenVal INTEGER :: n, i, j, steps PRINT*,"Enter order of matrix" READ*,n ALLOCATE(Matrix(n,n)) ALLOCATE(x(n)) PRINT*,"Enter elements row-wise" DO i = 1,n READ*,(Matrix(i,j),j=1,n) END DO PRINT*,"Enter number of iterations" READ*,steps CALL FINDEIGEN(Matrix, n, x, EigenVal, steps) PRINT*,"The largest eigenvalue of the matrix is ",EigenVal PRINT*,"The largest eigenvector of the matrix is:" DO i = 1,n PRINT*,x(i) END DO END PROGRAM EIGEN_MAT !Subroutines HereThese set of subroutines find the largest eigenvalue and the corressponding eigenvector
```!Author : Louisda16th a.k.a Ashwith J. Rego
!These set of subroutines find the largest eigenvalue and eigenmatrix of the matrix.
!The algorithm is based on Rayleigh's power method
!Please note that the subroutine used to multiply the two matrices is not general.
!Also note that the number of iterations must be specified
SUBROUTINE FINDEIGEN(Matrix, n, x, EigenVal, steps)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n, steps  !n = order of matrix, steps = number of iterations
REAL, INTENT(IN), DIMENSION(n,n) :: Matrix(n,n)  !Input Matrix
REAL, INTENT(INOUT), DIMENSION(n) :: x !Eigenvector
REAL, INTENT(INOUT) :: EigenVal !Eigenvalue
INTEGER :: i, j

x  = 1 !Initialize eigen vector to any value.

DO i = 1, steps
CALL MULMATRIX(Matrix, x, n)       !Multiply input matrix by eigenvector
CALL FINDLARGEST(x, n, EigenVal)   !Find eigenvalue
IF(EigenVal == 0) EXIT
DO j = 1, n                        !Find eigenvector
x(j) = x(j)/EigenVal
END DO
END DO

END SUBROUTINE FINDEIGEN

SUBROUTINE MULMATRIX(a, b, n)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n !matrix size
REAL, INTENT(IN), DIMENSION(n,n) :: a  !Matrix of order > 1
REAL, INTENT(INOUT), DIMENSION(n) :: b !1x1 matrix

INTEGER i, j
REAL, DIMENSION(n) :: temp !temporary matrix

temp = 0

!These two loops to the multiplication
DO i = 1, n
DO j = 1, n
temp(i) = temp(i) + a(i,j)*b(j)
END DO
END DO
b = temp

END SUBROUTINE MULMATRIX

SUBROUTINE FINDLARGEST(x, n, l)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n
REAL, INTENT(IN), DIMENSION(n) :: x
REAL, INTENT(INOUT) :: l !Largest value

INTEGER :: i
!Algorithm is easy
!Let the largest number be the first one.
!If you find a number larger than it, store this number and then continue
l = ABS(x(1))
DO i = 2, n
IF (ABS(x(i)) > l) l = ABS(x(i))
END DO

END SUBROUTINE FINDLARGEST

```

Is This A Good Question/Topic? 0

## Replies To: FORTRAN 90: Eigenvalues and Eigenvectors of a Matrix

### #2 atash

Reputation: 0
• Posts: 0
• Joined: 10-January 10

## Re: FORTRAN 90: Eigenvalues and Eigenvectors of a Matrix

Posted 13 January 2010 - 03:14 AM

thanks

Reputation: 0
• Posts: 0
• Joined: 09-April 10

## Re: FORTRAN 90: Eigenvalues and Eigenvectors of a Matrix

Posted 18 April 2010 - 09:10 AM

could you let a subroutine that find all eigenvalues of a general matrix. thank you

### #4 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: FORTRAN 90: Eigenvalues and Eigenvectors of a Matrix

Posted 18 April 2010 - 09:20 AM

Sadly, I'm having trouble understanding the algorithm myself since I don't have time. You could go through this. Its in Fortran 77 but may still be useful. http://www.nrbook.co...df/chap11f9.pdf