Anyone familiar with the fortran language?

In summary: So what you do is just calculate the cofactors (sign is the pattern of + and -), transpose and divide by the determinant.In summary, the conversation revolved around a user who is new to a programming language called Fortran and was seeking help in creating a program that can perform various tasks such as reading a 3x3 array of real numbers, calculating the inverse of a 3x3 matrix, multiplying two 3x3 matrices, and writing out the answer and original matrix. The user provided a code but was encountering errors. Other users suggested simplifying the code and fixing errors in the format statement. The conversation also touched on the mathematical formula for calculating the inverse of a matrix.
  • #1
Jauhar
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 I am desperate for it :) .. PLease HELP
 
Computer science news on Phys.org
  • #2
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
Yes the errror is with the cname but i can't figure it out I've 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 a lot :approve:
 
  • #5
bumpy bump
 
  • #6
Where do you specify the variable type of cname?
 
  • #7
this is the error i get

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

Ive tried everything forgive me if i can't answer all the questions u see I am extremely new to fortran
 
  • #8
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 going to do the invert, make sure to prove the above (maybe i blew a subscript).

Best
 
Last edited:
  • #9
I think I'm going to move this to 'Software'

[pushes button]
 
  • #10
[pushes button] : confused everyone.
 
  • #11
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
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
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
"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
ohh i think i might be getting it but slowly.

Thanks

Keep going is helpful
 
Last edited:
  • #16
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 don't think by the inverse just simply two matrices A and B to get C. Cant it be done on same program?

also I am getting a run time error now :shy: have u tried compiling it this is beginning to give me a headache i don't think programming is my thing :redface:
 
  • #17
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. Let's 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 got to 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
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 sense to me now slowly :). This fortran is quite kool i may need it in my calculations for aero later on.
 
  • #19
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
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
flexten said:
Also i assume you got rid of those pass by value calls ?
Best

ya i got rid of those calls...
 
  • #22
hi hope ur all well...

OK I've 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 doesn't 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. I've tried but it doesn't execute and comeup with run time errors.

Thanks for you help
 
  • #23
: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?
 

1. What is the history of the Fortran language?

The Fortran language was developed in the 1950s by a team at IBM led by John Backus. It was originally designed for scientific and engineering applications, and has since undergone several revisions to improve its capabilities and compatibility with newer computing systems.

2. What are the main features of Fortran?

Fortran is a high-level programming language that is optimized for mathematical and scientific computations. It includes features such as built-in support for arrays, mathematical functions, and complex numbers. It also has a concise syntax and strong support for parallel programming.

3. Is Fortran still widely used today?

Yes, Fortran is still widely used in scientific computing and engineering, particularly in fields such as weather forecasting, computational fluid dynamics, and high-performance computing. Many legacy codes are also written in Fortran, making it a valuable skill for scientists to have.

4. Are there any significant differences between different versions of Fortran?

Yes, there have been several revisions of the Fortran language, with the latest version being Fortran 2018. Each version has introduced new features and improvements, but there are also some differences in syntax and compatibility. It is important for Fortran programmers to be familiar with the specific version they are using.

5. Can Fortran be used for other types of programming besides scientific applications?

While Fortran is primarily used for scientific and engineering applications, it can also be used for other types of programming, such as data analysis and machine learning. Additionally, with the latest versions of Fortran, it is becoming more versatile and can be used for a wider range of applications.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
20
Views
4K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
17
Views
10K
Replies
20
Views
2K
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
3K
  • General Math
Replies
5
Views
2K
Back
Top