Make your own free website on Tripod.com

Fortran notes-2

 

OPEN/WRITE/READ STATEMENTS

 

Open(unit=integer expression,file=filename,status=literal)

Close(unit=integer expression)

Read(unit number,*) variable list

Write(unit number,*) expression list

Write(unit number,k) expression list

Where k is the statement number of the corresponding FORMAT statement.

 

Status can be either "old" or "new" or "unknown".

 

EXAMPLES:

Open(unit=10,file="results.dat",status="unknown")

Write(6,*) "hello"

Write(10,*)"Hello"

Read(10,*) time, temp

 

ARRAYS

DIMENSION array1(size), array2(size),....

 

Dimension J(5),DIST(4,4)

Read(9,*) (R(I),I=1,10)

 

An array can be defined like this:

Real b(10), c(3,5)

Integer j(7,4)

 

THE IDENTITY MATRIX

REAL IDEN(5,5)

DO 10 I=1,5

DO 5 J=1,5

IF (I.EQ.J) THEN

IDEN(I,J)=1.0

ELSE

IDEN(I,J)=0.0

END IF

5 CONTINUE

10 CONTINUE

 

FUNCTIONS:

 

CALL subroutine name(argument list)

SUBROUTINE name(argument list)


PROGRAM EXAMPLE

.....

CALL SORT(X,Y,N)

....

END

SUBROUTINE SORT(X,Y,N)

....

RETURN

END


COMMON BLOCKS

COMMON variable list

COMMON /name/ variable list

 

Examples:

Common a,b,c

Common /label1/ a,b,c


 

Program test1

Integer j

Real a,b,x,y

common a,j,b

....

call answer(x,y)

....

end

 

subroutine answer(x,y)

integer knot

common temp,knot,sum

....

return

end

 

COMPLEX NUMBERS

COMPLEX CX,CY,CZ

...

CX=(1.5,4.0)

CY=(0.0,2,4)

CZ=CMPLX(CX,CY)

COMPLEX FUNTIONS

REAL(X)

The real part of the complex number x

CONJG(X)

The conjugate number of the complex number x

AIMAG(X)

The imaginary part of the complex number x

CMPLX(X1,X2)

The real numbers x1 and x2 are converted into a complex number

 

 FUNCTIONS

FUNCTION name(argument list)

 

Example:

PROGRAM TEST

* This program finds the average value of three numbers

REAL TEST1,TEST2,TEST3,AVE

READ*, TEST1,TEST2,TEST3

PRINT 5 AVE(TEST1,TEST2,TEST3)

5 FORMAT(1X,AVERAGE =, F5.2)

END

*---------------------------------

REAL FUNCTION AVE(X,Y,Z)

REAL X,Y,Z

AVE = (X+Y+Z)/3.0

RETURN

END

  

 Back to the physics homepage

Back to my homepage