# Anyone familiar with the fortran language?

1. Aug 16, 2004

### Jauhar

hello all

Im new here but is anyone familiar with the fortran language? I need to make a program to do the following:

a) read a 3x3 array of REAL NUMBERS
b) calculate the inverse of the 3x3 matrix
c) write out the answer and the original Matrix
d) multiply two 3x3 matrices.

this is what i have so far but i keep gettin an error with it.

DIMENSION A(3,3)
DIMENSION B(3,3)
DIMENSION C(3,3)
CALL INPUTS(A)
CALL OUTPUTS(A)
CALL IN(B)
CALL OUT(B)
STOP
END

SUBROUTINE INPUTS(A)
DIMENSION A(3,3)
DO 100 IROW=1,3
DO 100 ICOL=1,3
WRITE(*,200) IROW, ICOL
100 CONTINUE
200 FORMAT('Enter elements of Matrix A (',I1,',',I1,') = ')
RETURN
END

SUBROUTINE OUTPUTS(A)
DIMENSION A(3,3)
WRITE(*,*) 'Matrix A will be'
DO 300 IROW=1,3
WRITE(*,400) (A(IROW,ICOL),ICOL=1,3)
300 CONTINUE
400 FORMAT(3F7.1)
RETURN
END

SUBROUTINE IN(B)
DIMENSION B(3,3)
DO 500 IROW=1,3
DO 500 ICOL=1,3
WRITE(*,600) IROW, ICOL
500 CONTINUE
600 FORMAT('Enter elements of Matrix B (',I1,',',I1,') = ')
RETURN
END

SUBROUTINE OUT(B)
DIMENSION B(3,3)
WRITE(*,*) 'Matrix B will be'
DO 700 IROW=1,3
WRITE(*,800) (B(IROW,ICOL),ICOL=1,3)
700 CONTINUE
800 FORMAT(3F7.1)
RETURN
END

DIMENSION a(n,n),b(n,n),c(n,n)
C(1,1)= A(1,1)*B(1,1)+A(1,2)*B(2,1)+A(1,3)*B(3,1)
C(1,2)= A(1,1)*B(1,2)+A(1,2)*B(2,2)+A(1,3)*B(3,2)
C(1,3)= A(1,1)*B(1,3)+A(1,2)*B(2,3)+A(1,3)*B(3,3)
C(2,1)= A(2,1)*B(1,1)+A(2,2)*B(2,1)+A(2,3)*B(3,1)
C(2,2)= A(2,1)*B(1,2)+A(2,2)*B(2,2)+A(2,3)*B(3,2)
C(2,3)= A(2,1)*B(1,3)+A(2,2)*B(2,3)+A(2,3)*B(3,3)
C(3,1)= A(3,1)*B(1,1)+A(3,2)*B(2,1)+A(3,3)*B(3,1)
C(3,2)= A(3,1)*B(1,2)+A(3,2)*B(2,2)+A(3,3)*B(3,2)
C(3,3)= A(3,1)*B(1,3)+A(3,2)*B(2,3)+A(3,3)*B(3,3)

WRITE(*,*) 'Matrix C is, the multiplication of matrices A & B'
DO 1000 IROW=1,3
WRITE(*,1100) (C(IROW,ICOL),ICOL=1,3)
1000 CONTINUE
1100 FORMAT(3F7.1)
Stop
END

I have been told it could cut down to this but i also get an error with this:( .

DIMENSION B(3,3)
CALL INPUT1(A,'A')
CALL OUTPUT1(A,'A')
CALL INPUT1(B,'B') ! same subroutine, different arguments
CALL OUTPUT1(B,'B') ! same subroutine, different arguments
STOP
END

SUBROUTINE INPUT1(x,cname)
DIMENSION x(3,3)
DO 100 IROW=1,3
DO 100 ICOL=1,3
WRITE(*,200) IROW, ICOL
100 CONTINUE
200 FORMAT ('Enter elements of Matrix ',cname,' (',I1,',',I1,') = ')
RETURN
END

SUBROUTINE OUTPUT1(x,cname)
DIMENSION x(3,3)
WRITE(*,*) 'Matrix ',cname,' will be'
DO 700 IROW=1,3
WRITE(*,800) (x(IROW,ICOL),ICOL=1,3)
700 CONTINUE
800 FORMAT(3F7.1)
STOP
END

2. Aug 16, 2004

### Integral

Staff Emeritus
What is the error you get?

3. Aug 16, 2004

maybe something with the "cname"? I (quickly) copied and pasted and executed in emacs and mine came up with an error about cname.. I'd mess with it more but I have some sleep to catch up on.

4. Aug 17, 2004

### Jauhar

Yes the errror is with the cname but i cant figure it out ive kept trying i spent basically the whold day on it

Anyways and a newb to it tht could be why what i appreciate the help.

Thanks alot

5. Aug 17, 2004

### Jauhar

bumpy bump

6. Aug 17, 2004

### Integral

Staff Emeritus
Where do you specify the variable type of cname?

7. Aug 18, 2004

### Jauhar

this is the error i get

error 274 - Unknown edit descriptor 'C', or missing comma

Ive tried everything forgive me if i cant answer all the questions u see im extremely new to fortran

8. Aug 18, 2004

### flexten

I think it is an error in format statement.
First off ; get rid of all STOP statements ... those are used only for fatal error traps.
Next : get rid of all RETURN statements .. they are redundant : END is the same as RETURN.

When you compiler gives error report it will tell you line # of error ... use it.
First output for write statement is 1X in the format statement.
This is required for proper page control (first out is page control).
Example:

WRITE(*,'(1X,1P5E10.3)') FOM0,FOM1,FOM2,FOM3,FOM4

Don't use the literal passing, no reason to go this complicated method and it's all wrong anyway, just pass the arguments like in first method.
Invert not too much more complicated than multiply :

$$\,[Z] = \frac{1}{{Y_{11} (Y_{22} Y_{33} - Y_{23} Y_{32} ) + Y_{12} (Y_{31} Y_{23} - Y_{21} Y_{33} ) + Y_{13} (Y_{21} Y_{32} - Y_{31} Y_{22} )}} \cdot \left[ {\begin{array}{*{20}c} {Y_{22} Y_{33} - Y_{23} Y_{32} } \hfill & {Y_{13} Y_{32} - Y_{12} Y_{33} } \hfill & {Y_{12} Y_{23} - Y_{13} Y_{22} } \hfill \\ {Y_{31} Y_{23} - Y_{21} Y_{33} } \hfill & {Y_{11} Y_{33} - Y_{13} Y_{31} } \hfill & {Y_{13} Y_{21} - Y_{23} Y_{11} } \hfill \\ {Y_{21} Y_{32} - Y_{31} Y_{22} } \hfill & {Y_{31} Y_{12} - Y_{32} Y_{11} } \hfill & {Y_{11} Y_{22} - Y_{12} Y_{21} } \hfill \\ \end{array}} \right]$$

If you are gonna do the invert, make sure to prove the above (maybe i blew a subscript).

Best

Last edited: Aug 18, 2004
9. Aug 18, 2004

### enigma

Staff Emeritus
I think I'm going to move this to 'Software'

[pushes button]

10. Aug 18, 2004

### flexten

[pushes button] : confused everyone.

11. Aug 18, 2004

### Jauhar

thanks for the help The error is with the Format statement but i still can tsort it out i removed the stop and return statements but it didnt help. :(

Thanks big time for the help. I have no idea what more to do but i will keep messin with it.

12. Aug 18, 2004

### flexten

200 FORMAT(1X,'Enter elements of Matrix A (',I1,',',I1,') = ',\)

Does that work ? Put the entire line here that has the syntax error. Or here is a quick and dirty way :

PRINT*,'INPUT 9 NUMBERS'

FORTRAN manuals really have a derth of examples. In the above method ; separate inputs with commas or spaces.

Best

13. Aug 18, 2004

### Jauhar

hello OK that just got rid of the error thanks. Now i got this for the Subrotines Also for the main program the first part what would be the correct way to write it in Because initially i have the variables A and B but now i dont. Sorry about being such a newb :).

SUBROUTINE INPUT1(x)
DIMENSION x(3,3)
DO 100 IROW=1,3
DO 100 ICOL=1,3
WRITE(*,200) IROW, ICOL
100 CONTINUE
200 FORMAT(1X,'Enter elements of Matrix A (',I1,',',I1,') = ',\)
END

SUBROUTINE OUTPUT1(x,cname)
DIMENSION x(3,3)
WRITE(*,*) 'Matrix ',cname,' will be'
DO 700 IROW=1,3
WRITE(*,800) (x(IROW,ICOL),ICOL=1,3)
700 CONTINUE
800 FORMAT(3F7.1)
END

Thanks big time for tht

14. Aug 18, 2004

### flexten

"I have been told it could cut down to this but i also get an error with this:( ."
"Don't use the literal passing, no reason to go this complicated method and it's all wrong anyway, just pass the arguments like in first method."
If you insist on passing by value, you need to declare ! Why are you passing cname ?
And i told you before : the first byte output is page control ... use a 1x first.

SUBROUTINE OUTPUT1(x)
DIMENSION x(3,3)
WRITE(*,*) 'Matrix will be'
DO 700 IROW=1,3
WRITE(*,800) (x(IROW,ICOL),ICOL=1,3)
700 CONTINUE
800 FORMAT(1x,3F7.1)
END

a) read a 3x3 array of REAL NUMBERS
b) calculate the inverse of the 3x3 matrix
c) write out the answer and the original Matrix
d) multiply two 3x3 matrices.

First 3 steps look like one program to me, step d another program ? Or maybe it implies you multiply the origional matrix by the invert and also print out the result of that (course it is I).

Best

Last edited: Aug 18, 2004
15. Aug 18, 2004

### Jauhar

ohh i think i might be getting it but slowly.

Thanks

Last edited: Aug 18, 2004
16. Aug 18, 2004

### Jauhar

d) is to multiply 2 3x3 Matrices i dont think by the inverse just simply two matrices A and B to get C. Cant it be done on same program?

also im getting a run time error now :shy: have u tried compiling it this is begining to give me a headache i dont think programming is my thing

17. Aug 18, 2004

### flexten

Hard for me to compile it as i don't know what you source looks like now, besides, my compiler's on other machine. Don't give up so easy. Lets look at your main program :

CALL INPUTS(A)
CALL OUTPUTS(A)
CALL IN(B)
CALL OUT(B)

Say we want to do the first 3 steps, then :

PRINT*,'Input Matrix to be Inverted'
CALL INPUT(A) ...... gets the array data
CALL INVERT(A,B) .... inverts the 3x3 matrix and returns result in B
PRINT*,'Input Matrix is :'
CALL OUTPUT(A) ...... outputs the origional (input) matrix
PRINT*,'Inverse is :'
CALL OUTPUT(B) .......outputs the inverted matrix ... done ! !

if you want to also multiply two matricies together ... you gotta get the matricies from someplace ... so you can input them both like :

PRINT*,'Input first matrix for multiplication'
CALL INPUT(A) .... we can use the same storage to input another array
PRINT*,'Input second matrix'
CALL INPUT(B) ..... get another matrix from the operator
CALL MULT(A,B,C) .... multiply em together and return result in C
PRINT*,'First Input Matrix is :'
CALL OUTPUT(A)
PRINT*,'Second Input Matrix is :'
CALL OUTPUT(B)
PRINT*,'Product Matrix is :'
CALL OUTPUT(C)
END

So need subroutines INPUT,OUTPUT,INVERT and MULT only.

Best

18. Aug 18, 2004

### Jauhar

hi ok I just tried again

and am using ur advice thanks again for ur time.

But what does this mean "MAIN# - in file aaa.f95 at line 1 [+0044] [recur= 1]"

Thts the runtime error i got. I put the subrotiines and the main prog in there were no errors in compiling.

Its actually being to make sence to me now slowly :). This fortran is quite kool i may need it in my calculations for aero later on.

19. Aug 19, 2004

### flexten

Don't know what that error is. Do you use a PROGRAM statement ?
First line of your program should be PROGRAM MYPROG or what you want for a name.
Also i assume you got rid of those pass by value calls ?

Best

Last edited: Aug 19, 2004
20. Aug 19, 2004

### Jauhar

hi

Yeah i had a program statement its PROGRAM MATRIX. The program strts and gives the first statement "Input Matrix to be Inverted" The the error comes up.