Anda di halaman 1dari 25

Fortran -3

Arrays and Functions

Arrays (matrices)
Array elements are numbered 1, 2, 3,
and are stored in consecutive locations of memory. A(1)
A(2) A(3) A(4)

Declaration of Array
REAL, DIMENSION (10) :: A LOGICAL, DIMENSION(5) :: TRUTH Character(len=15) , DIMENSION(100) ::
NAMES Integer, dimension(10,10) :: B

Declaration (contd.)
One can use named constants in
declaration. This is useful in changing matrix sizes. Integer, Parameter :: dim=10 Real :: A(dim) Integer :: B(dim,dim) Real :: C(2*dim)
4

Initialization of Arrays
An array element behaves like an ordinary
variable which has the same type as the declared array type. Array can be initialized by defining every element : Integer, Dimension (5) :: A Do I=1,5 A(I) = 0 End Do

Initialization of Array
It is also possible to initialize array in the
declaration statement itself Real, Dimension(5) :: A=(/1.,2.,3.,4.,5./) Initialization may be done with READ statement from terminal or from a file Initialize through an implicit do integer, dimension(5):: A=(/(i, i=1,5)/) initializes A= 1,2,3,4,5
6

Changing array range


However, sometimes it is useful to change the range. For instance, if we want to store sales figure of years from 2001 to 2007, it may be better if we defined Real, Dimension(2001:2007) :: Sales Internally, computer will subtract 2000 from the argument to arrive at correct reference.

The default range of an array is from 1 to N.

Matrix operations
Fortran 90 allows whole matrix operations
between matrices of the same shape and operations like addition, subtraction and multiplication, division etc. will be done on element to element basis, i.e. an operation C= A+B implies C (I , J)= A(I,J) +B(I,J) or C= A*B implies C(I,J) = A(I,J)*B(I,J) and NOT usual matrix multiplication.
8

Bubble Sort
Read N Data as an array of N numbers For each I check if A(I) > A(J) for J= I+1
to N. If yes, swap.

Gauss Seidel Method


Read the coefficient matrix and the
r.h.s. of each equation
The set of equations AX = Y can be solved iteratively by writing the i-th equation as A ii X i + Aij X j = Yi
j i

The solution of the i-th variable is X i = ( Aij X j + Y )i /A ii


j i

10

Gauss-Seidel
The method converges if written in
diagonally dominant form Give trial values of Xi Solve for each Xi iteratively.

X ( I ) = (Y ( I ) sum(I)) / A( I , I ), where sum(I)= A ij


j i
11

Subroutines
Compiled as separate program independent of
main program Codes can be used repeatedly in different parts of the same or different program Communication with main program is only through passing of arguments. All variables and data except those in the argument list are hidden from main or subroutine so that mistake in one program is not replicated in the other.
12

Subroutine structure
Similar to main program except for the
argument list
Subroutine sub (argument list) Declaration section Statements Return End subroutine sub

The variables in the argument list match the

type and order in the Declaration in Subroutine


13

How argument list is used


Subroutine to calculate range of a projectile Subroutine Range (init_vel, angle, time, range1)
Implicit None Real INTENT(IN) :: init_vel, angle Real INTENT(OUT) :: range1, time Real, parameter :: g=9.81 Real, parameter :: pi=3.14159 Real :: radian_angle radian_angle = angle*pi/180. Range1= (init_vel)**2*sin(2*radian_angle)/g Time =2.* init_vel*sin(radian_angle)/g Return end subroutine Range
14

V 2 sin 2 R= g

How a main program calls this subroutine

Program rangefinder
real :: u, t, theta, range2 Write(*,*) Enter initial velocity u , angle of projection in degrees and time t Read(*,*) u,t,theta Call Range(u, theta, t, range2) Write(*,*) Range=, range, time-, t End program rangefinder
15

INTENT Variables
Dummy arguments in a subroutine can
have INTENT attribute INTENT(IN), INTENT(OUT), INTENT(INOUT) Intent attribute of each argument should be declared in the subroutine so that accidental tampering such arguments do not take place.
16

Passing arrays as argument of a subroutine

Calling argument passes a pointer to the

memory location of the array argument. There are several ways to pass on array arguments. In this example we use dummy arguments where the arrays have the same shape as in the calling program. The main program calls a subroutine matmult to do the matrix multiplication.

17

Matrix Multiplication subroutine


Program main
Real, dimension (20,20) :: A,B,C ! 20X20 is the maximum size that the multiplication is expected to be handled. Integer :: al, au, bl, bu !these are actual dimensions of the matrices A,B and that of product C, which will be passed on. For multiplication au=bl
18

Multiplication (Contd.)
Enter the matrix elements of A and B
Call Matmult (A, B, al, au, bl, bu, C) Write C Subroutine Structure : Subroutine Matmult (A1,B1,al,au,bl,bu,C) ! The names of dummy arguments may or may not be the same as the real argument but their number and the order in which they appear must be identical integer, intent (in) :: al, au, bl, bu real, intent(in), dimension(20,20) :: A1,B1 real, intent(out), dimension(20,20) :: C
19

Matrix multiplication (contd.)


IF (au/=bl) THEN Write(*,*) The matrix is not conformable stop END IF Carry out multiplication of matrices A1 and B1 and store in result in C Return
20

Function
Result is a single number, logical value a
string or an array Intrinsic functions are built in sin(x), abs(x) Here we talk about user defined functions

21

Functions
Function definition should define the return type.
This can be done in two ways : Integer function myfunction (I, J) OR Function myfunction (I,J) integer :: myfunction

22

Program to calculate volume of a sphere, given radius.


Main program Implicit none Real :: radius, volume Read(*,*) radius Write(*,*) volume=, volume(radius) End Main The function program for volume would look as follows

23

Subroutine Volume
Real function volume(R) ! R is a dummy argument corresponding to radius in main. implicit none Real, intent(in) :: R Real, parameter :: pi=3.14159 If (r<=0.0) then volume=0 Return ! Return control to the calling program End If Volume=4*p1*R**3/3 end function volume
24

Recursion
A function can call itself recursively For instance n! = n*(n-1)!
Recursive function factorial (n) Result(fac) ! Note the return value is in Result, fac is the result returned Integer, intent(in) :: n Integer :: fac If (n==0) then Fac=1 Else Fac= n*factorial(n-1) End if End Function Factorial

25

Anda mungkin juga menyukai