# Eigenvalues and eigenvectors of a real symmetric matrix in Fortran

• Comp Sci
• alui
In summary: The program tries to solve the system of equations a*x = lambda*x. The system has three equations. The first
alui

## Homework Statement

I try to run this program, but there are still some errors, please help me to solve this problems

## The Attempt at a Solution

Code:
        Program Main
!====================================================================
!  eigenvalues and eigenvectors of a real symmetric matrix
!  Method: calls Jacobi
!====================================================================
implicit none
integer, parameter :: n=3
double precision :: a(n,n), x(n,n)
double precision, parameter:: abserr=1.0e-09
integer i, j

! matrix A
data (a(1,i), i=1,3) /   1.0,  2.0,  3.0 /
data (a(2,i), i=1,3) /   2.0,  2.0, -2.0 /
data (a(3,i), i=1,3) /   3.0, -2.0,  4.0 /

! print a header and the original matrix
write (*,200)
do i=1,n
write (*,201) (a(i,j),j=1,n)
end do

call Jacobi(a,x,abserr,n)

! print solutions
write (*,202)
write (*,201) (a(i,i),i=1,n)
write (*,203)
do i = 1,n
write (*,201)  (x(i,j),j=1,n)
end do

200     format (' Eigenvalues and eigenvectors (Jacobi method) ',/, &
' Matrix A')
201         format (6f12.6)
202         format (/,' Eigenvalues')
203         format (/,' Eigenvectors')
end program main

subroutine Jacobi(a,x,abserr,n)
!===========================================================
! Evaluate eigenvalues and eigenvectors
! of a real symmetric matrix a(n,n): a*x = lambda*x
! method: Jacoby method for symmetric matrices
! Alex G. (December 2009)
!-----------------------------------------------------------
! input ...
! a(n,n) - array of coefficients for matrix A
! n      - number of equations
! abserr - abs tolerance [sum of (off-diagonal elements)^2]
! output ...
! a(i,i) - eigenvalues
! x(i,j) - eigenvectors
!===========================================================
implicit none
integer i, j, k, n
double precision a(n,n),x(n,n)
double precision abserr, b2, bar
double precision beta, coeff, c, s, cs, sc

! initialize x(i,j)=0, x(i,i)=1
! *** the array operation x=0.0 is specific for Fortran 90/95
x = 0.0
do i=1,n
x(i,i) = 1.0
end do

! find the sum of all off-diagonal elements (squared)
b2 = 0.0
do i=1,n
do j=1,n
if (i.ne.j) b2 = b2 + a(i,j)**2
end do
end do

if (b2 <= abserr) return

! average for off-diagonal elements /2
bar = 0.5*b2/float(n*n)

do while (b2.gt.abserr)
do i=1,n-1
do j=i+1,n
if (a(j,i)**2 <= bar) cycle  ! do not touch small elements
b2 = b2 - 2.0*a(j,i)**2
bar = 0.5*b2/float(n*n)
! calculate coefficient c and s for Givens matrix
beta = (a(j,j)-a(i,i))/(2.0*a(j,i))
coeff = 0.5*beta/sqrt(1.0+beta**2)
s = sqrt(max(0.5+coeff,0.0))
c = sqrt(max(0.5-coeff,0.0))
! recalculate rows i and j
do k=1,n
cs =  c*a(i,k)+s*a(j,k)
sc = -s*a(i,k)+c*a(j,k)
a(i,k) = cs
a(j,k) = sc
end do
! new matrix a_{k+1} from a_{k}, and eigenvectors
do k=1,n
cs =  c*a(k,i)+s*a(k,j)
sc = -s*a(k,i)+c*a(k,j)
a(k,i) = cs
a(k,j) = sc
cs =  c*x(k,i)+s*x(k,j)
sc = -s*x(k,i)+c*x(k,j)
x(k,i) = cs
x(k,j) = sc
end do
end do
end do
end do
return
end

and the error in my compiler is

200 format (' Eigenvalues and eigenvectors (Jacobi method) ',/, &
1
Error: Unexpected element '&' in format string at (1)
C:\Users\TOSHIBA\AppData\Local\Temp\~Source7.f:34.13:

' Matrix A')
1
Error: Invalid character in name at (1)
C:\Users\TOSHIBA\AppData\Local\Temp\~Source7.f:18.20:

write (*,200)
1
Error: FORMAT label 200 at (1) not defined

Last edited by a moderator:
Please use CODE tags to enclose program source code. This preserves any spacing found in the original program, which is sometimes important for languages like Fortran.

It's also not clear which version of Fortran your compiler is using. Is it Fortran 77, Fortran 90/95, something else?

In any event, the FORMAT statement labeled 200 needs attention. Try eliminating the '&' at the end and put it instead in column 6 of the next line.

alui
Code tags inserted... :-)

Code:
write (*,200)
.
.
.
200     format (' Eigenvalues and eigenvectors (Jacobi method) ',/, &
' Matrix A')
I would dispense with the format statement altogether, and write something like this:
Code:
 write (*, *) ' Eigenvalues and eigenvectors (Jacobi method)  Matrix A'

The compiler is probably complaining about the line being broken up with an incorrect continuation character being used. If all you're doing is printing a string of characters, I don't see any advantage in using a format statement.

Here's a link to a page that discusses continuation lines in Fortran: http://fortranwiki.org/fortran/show/Continuation+lines

alui
SteamKing said:
Please use CODE tags to enclose program source code. This preserves any spacing found in the original program, which is sometimes important for languages like Fortran.

It's also not clear which version of Fortran your compiler is using. Is it Fortran 77, Fortran 90/95, something else?

In any event, the FORMAT statement labeled 200 needs attention. Try eliminating the '&' at the end and put it instead in column 6 of the next line.

SteamKing said:
Please use CODE tags to enclose program source code. This preserves any spacing found in the original program, which is sometimes important for languages like Fortran.

It's also not clear which version of Fortran your compiler is using. Is it Fortran 77, Fortran 90/95, something else?

In any event, the FORMAT statement labeled 200 needs attention. Try eliminating the '&' at the end and put it instead in column 6 of the next line.

Thank you very much :)

Mark44 said:
Code:
write (*,200)
.
.
.
200     format (' Eigenvalues and eigenvectors (Jacobi method) ',/, &
' Matrix A')
I would dispense with the format statement altogether, and write something like this:
Code:
 write (*, *) ' Eigenvalues and eigenvectors (Jacobi method)  Matrix A'

The compiler is probably complaining about the line being broken up with an incorrect continuation character being used. If all you're doing is printing a string of characters, I don't see any advantage in using a format statement.

Here's a link to a page that discusses continuation lines in Fortran: http://fortranwiki.org/fortran/show/Continuation lines

Thank you very much :)

## 1. What are eigenvalues and eigenvectors of a real symmetric matrix?

Eigenvalues and eigenvectors are mathematical concepts that are used to study linear transformations. In the context of a real symmetric matrix, eigenvalues are the scalars that represent the scale factor of the eigenvectors. In simpler terms, eigenvalues and eigenvectors help us understand how a matrix transforms a vector.

## 2. How are eigenvalues and eigenvectors calculated in Fortran?

In Fortran, the eigenvalues and eigenvectors of a real symmetric matrix can be calculated using the subroutine DSYEV from the LAPACK library. This subroutine uses the QR algorithm to compute the eigenvalues and eigenvectors of a matrix.

## 3. What is the significance of a real symmetric matrix in Fortran?

A real symmetric matrix is important in Fortran because it has a special property where its eigenvalues are always real numbers. This makes it easier to calculate and analyze the eigenvalues and eigenvectors using Fortran subroutines. Additionally, real symmetric matrices have many applications in physics, engineering, and computer science.

## 4. Can a real symmetric matrix have complex eigenvalues?

No, a real symmetric matrix can only have real eigenvalues. This is because the complex eigenvalues would have to come in complex conjugate pairs, which would violate the symmetry of the matrix. Therefore, a real symmetric matrix always has real eigenvalues and orthogonal eigenvectors.

## 5. How are eigenvalues and eigenvectors used in real-world applications?

The concept of eigenvalues and eigenvectors has various applications in real-world problems, such as image processing, data compression, and machine learning. They are also used in physics to study the behavior of systems, in engineering to analyze structures and vibrations, and in computer science to solve linear systems of equations.

Replies
7
Views
2K
Replies
2
Views
6K
Replies
1
Views
1K
Replies
18
Views
2K
Replies
7
Views
1K
Replies
5
Views
508
Replies
10
Views
2K
Replies
1
Views
2K
Replies
1
Views
819
Replies
5
Views
2K