Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!emory!hubcap!molotov.eng.clemson.edu!vanand From: vanand@molotov.eng.clemson.edu (Anand Lakshmikumaran) Newsgroups: comp.lang.fortran Subject: AN ODD ERROR Message-ID: <9206@hubcap.clemson.edu> Date: 4 Jun 90 20:50:40 GMT Sender: news@hubcap.clemson.edu Reply-To: vanand@molotov.eng.clemson.edu (Anand Lakshmikumaran) Distribution: na Organization: Clemson University Engineering Department Lines: 177 Hi Folks ! I encountered a very odd error in a couple of my FORTRAN programs. I have debugged the programs to the point that i know where the error occurs but can't understand why that error should occur. I have simplified one of the programs and am attaching it below so that if anyone have faced a similar problem before or know how to avoid this error could kindly reply to me by email. This program is just a part of a bigger program. The problem is: I am trying to invert a matrix 'b' (which in this example is of order 4 but could be much bigger actually) using a subroutine. 'write statement' no. 1 gives the correct value but 'write statement' no. 2 which SHOULD give the same value doesn't. I have attached the output also. The odd part of it is that if i change the dimension of 'b' in my first statement to 'b(4,4)' (i.e. which is the actual array size for this particular example) then the output is accurate. The correct values of 'b inverse' is also attached. P.S. I have tried to run it on both SUN Fortran and VAX Fortran. Both of them give the same error. My email address is vanand@eng.clemson.edu Thanks for reading through this. -ANAND V. L. vanand@eng.clemson.edu dimension b(50,50) n = 4 do i = 1,n,1 b(i,i) = 4.0 if(i.ne.(n))then b(i,i+1) = 1. b(i+1,i) = 1. endif enddo c*** WRITE STATEMENT NO.1 ******************************************* do i = 1,n,1 write(*,*)(b(i,j),j=1,n,1) enddo C************************************************************************** call minv(n,b) C*** WRITE STATEMENT NO. 4 ******************************************* do i = 1,n,1 write(*,*)(b(i,j),j=1,n,1) enddo C************************************************************************** end C ******************************************************** C MINV IS MATRIX INVERSE. C ******************************************************** SUBROUTINE MINV(N,A) dimension a(n,n) C********************************************************************** C modified from p. 197 of James,Smith,& Wolford C matrix inversion using Gauss-Jordan reduction C inverted matrix overlays original matrix in memory C C input: matrix a to be inverted, N=order of matrix [A] C C output: a is replaced by its inverse (original destroyed) C C calculate elements of new matrix C********************************************************************** C*** WRITE STATEMENT NO. 2 **************************************** do 100 i = 1,n,1 write(*,*)(a(i,j),j=1,n,1) 100 continue C************************************************************************ DO 600 K=1,N C calculate new elements of pivot row do 20 J=1,N IF (J.ne.K) THEN A(K,J)=A(K,J)/A(K,K) endif 20 continue C calculate element replacing pivot element A(K,K)=1.d0/A(K,K) C calculate new elements not in pivot row or pivot column do 40 I=1,N do 30 J=1,N if ((I.ne.K).and.(J.ne.K)) then A(i,j)=A(i,j)-A(k,j)*A(i,k) endif 30 continue 40 CONTINUE C calculate replacement elements for pivot column C (except pivot element) do 50 i=1,N if(i.ne.K) a(i,K)=-a(i,K)*a(K,K) 50 CONTINUE 600 continue C*** WRITE STATEMENT NO. 3 ******************************************** do 200 i = 1,n,1 write(*,*)(a(i,j),j=1,n,1) 200 continue C**************************************************************************** return end THE OUTPUT ON SUN FORTRAN WAS AS FOLLOWS WRITE STATEMENT NO. 1 4.00000 1.00000 0. 0. 1.00000 4.00000 1.00000 0. 0. 1.00000 4.00000 1.00000 0. 0. 1.00000 4.00000 WRITE STATEMENT NO. 2 4.00000 0. 0. 0. 1.00000 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. WRITE STATEMENT NO. 3 -\NA\N \NA\N -\NA\N -\NA\N \NA\N \NA\N \NA\N \NA\N -\NA\N -\NA\N -\NA\N -\NA\N -\NA\N -\NA\N -\NA\N -\NA\N WRITE STATEMENT NO. 4 -\NA\N 1.00000 0. 0. \NA\N 4.00000 1.00000 0. -\NA\N 1.00000 4.00000 1.00000 -\NA\N 0. 1.00000 4.00000 THE CORRECT VALUES OF THE MATRIX B AND ITS INVERSE ARE MATRIX B 4.0 1.0 0.0 0.0 1.0 4.0 1.0 0.0 0.0 1.0 4.0 1.0 0.0 0.0 1.0 4.0 B INVERSE 0.267943 -7.17703E-02 1.91388E-02 -4.78469E-03 -7.17703E-02 0.287081 -7.65550E-02 1.91388E-02 1.91388E-02 -7.65550E-02 0.287081 -7.17703E-02 -4.78469E-03 1.91388E-02 -7.17703E-02 0.267943