1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Eigenvalues and eigenvectors of a real symmetric matrix in Fortran

Tags:
  1. Nov 29, 2014 #1
    1. The problem statement, all variables and given/known data

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

    2. Relevant equations


    3. The attempt at a solution
    Code (Text):
            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: Nov 29, 2014
  2. jcsd
  3. Nov 29, 2014 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  4. Nov 29, 2014 #3

    berkeman

    User Avatar

    Staff: Mentor

    Code tags inserted... :-)
     
  5. Nov 30, 2014 #4

    Mark44

    Staff: Mentor

    Instead of this:
    Code (Text):
    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 (Text):
     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
     
  6. Dec 1, 2014 #5
    Thank you very much :)
     
  7. Dec 1, 2014 #6
    Thank you very much :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Eigenvalues and eigenvectors of a real symmetric matrix in Fortran
Loading...