Anyone familiar with the fortran language?

  • Thread starter Jauhar
  • Start date
  • #1
14
0
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)
CALL ANSWERS(A,B,N,C)
STOP
END

SUBROUTINE INPUTS(A)
DIMENSION A(3,3)
DO 100 IROW=1,3
DO 100 ICOL=1,3
WRITE(*,200) IROW, ICOL
READ(*,*) A(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
READ(*,*) B(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


SUBROUTINE ANSWERS(a,b,n,c)
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
READ(*,*) x(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

Hope someone can help im desperate for it :) .. PLease HELP
 

Answers and Replies

  • #2
Integral
Staff Emeritus
Science Advisor
Gold Member
7,201
56
What is the error you get?
 
  • #3
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
14
0
Yes the errror is with the cname but i cant figure it out ive kept trying i spent basically the whold day on it :eek: :redface:

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

Thanks alot :approve:
 
  • #5
14
0
bumpy bump
 
  • #6
Integral
Staff Emeritus
Science Advisor
Gold Member
7,201
56
Where do you specify the variable type of cname?
 
  • #7
14
0
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
51
0
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 :

[itex]
\[
\,[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]
\]
[/itex]


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

Best
 
Last edited:
  • #9
enigma
Staff Emeritus
Science Advisor
Gold Member
1,752
13
I think I'm going to move this to 'Software'

[pushes button]
 
  • #10
51
0
[pushes button] : confused everyone.
 
  • #11
14
0
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
51
0
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'
READ*,C(1,1),C(1,2),C(1,3),C(2,1),C(2,2),C(2,3),C(3,1),C(3,2),C(3,3)

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

Best
 
  • #13
14
0
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
READ(*,*) x(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
51
0
"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:
  • #15
14
0
ohh i think i might be getting it but slowly.

Thanks

Keep going is helpful
 
Last edited:
  • #16
14
0
flexten said:
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
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 :redface:
 
  • #17
51
0
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)
CALL ANSWERS(A,B,N,C)

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
14
0
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
51
0
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:
  • #20
14
0
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.
 
  • #21
14
0
flexten said:
Also i assume you got rid of those pass by value calls ?
Best
ya i got rid of those calls....
 
  • #22
14
0
hi hope ur all well...

OK ive tried this program. But i got probs again. when i execute the old proram which i had. It answered my first question which was to: read a 3x3 array of REAL NUMBERS. the program was.

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

SUBROUTINE INPUTS(A)
DIMENSION A(3,3)
DO 100 IROW=1,3
DO 100 ICOL=1,3
WRITE(*,200) IROW, ICOL
READ(*,*) A(IROW,ICOL)
100 CONTINUE
200 FORMAT('Enter elements of Matrix A (',I1,',',I1,') = ')
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)
end

SUBROUTINE IN(B)
DIMENSION B(3,3)
DO 500 IROW=1,3
DO 500 ICOL=1,3
WRITE(*,600) IROW, ICOL
READ(*,*) B(IROW,ICOL)
500 CONTINUE
600 FORMAT('Enter elements of Matrix B (',I1,',',I1,') = ')
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)
END

this program Asked me for the input and then Gave me an output in matrix form for 2 matrices A and B.

But when i try the new program which flexten kindly helped me with it doesnt get past the first input which is inverse.

so basically how can i get the program to store the matrix which i input in the above program and then make it inverse it. Ive tried but it doesnt execute and comeup with run time errors.

Thanks for ya help
 
  • #23
14
0
:eek: :rofl: :yuck: :surprise: :cry: :zzz: :blush: :bugeye: :rolleyes:
 
  • #24
I need help writing a fortran subroutine that given a set of N numbers and integer i, determine the ith smallest number. I need the subroutine to be recursive and use the median of medians method to sort the array of input numbers and output the ith smallest number. Can anyone help please?
 

Related Threads on Anyone familiar with the fortran language?

  • Last Post
Replies
5
Views
3K
Replies
18
Views
4K
  • Last Post
Replies
2
Views
3K
  • Last Post
2
Replies
33
Views
4K
Replies
40
Views
2K
  • Last Post
Replies
12
Views
12K
Replies
2
Views
2K
  • Last Post
Replies
2
Views
8K
  • Last Post
Replies
22
Views
4K
Replies
7
Views
3K
Top