Eigenvalues and eigenvectors of a real symmetric matrix in Fortran

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Fortran program designed to compute the eigenvalues and eigenvectors of a real symmetric matrix using the Jacobi method. Participants are addressing specific compilation errors and offering suggestions for code formatting and structure.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports compilation errors related to the FORMAT statement in their Fortran code, specifically mentioning an unexpected element '&' and an invalid character in the name.
  • Another participant suggests using CODE tags to preserve spacing in the program source code, which is important for Fortran.
  • Some participants question the version of Fortran being used, asking for clarification on whether it is Fortran 77, Fortran 90/95, or another version.
  • A participant proposes eliminating the FORMAT statement altogether and suggests a simpler write statement for output, arguing that the continuation character may be causing issues.
  • There are repeated suggestions to address the FORMAT statement labeled 200 by moving the continuation character '&' to the correct column in the next line.

Areas of Agreement / Disagreement

Participants generally agree that the FORMAT statement needs modification, but there are differing opinions on whether to retain it or simplify the output method. The discussion remains unresolved regarding the best approach to fix the code.

Contextual Notes

Participants have not reached a consensus on the specific version of Fortran being used, which may affect the applicability of their suggestions. There are also unresolved aspects regarding the handling of continuation lines in Fortran.

alui
Messages
4
Reaction score
0

Homework Statement



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

Homework Equations

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
! comments ...
!===========================================================
        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:
Physics news on Phys.org
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.
 
  • Like
Likes   Reactions: alui
Code tags inserted... :-)
 
Instead of this:
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
 
  • Like
Likes   Reactions: 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:
Instead of this:
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 :)
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K