So far, we have met integer and double precision variables which can store a single value. It is often useful to be able to store several values in one variable, for example, to store the three components of a vector, or one hundred data points from an experiment. Fortunately, in Fortran we are able to do this, by using arrays.
Arrays are sometimes referred to as subscripted variables. They may be declared as follows:
integer n (10) double precision e (25)
In the above, the integer variable n refers to 10 separate variables: n(1), n(2), n(3), ... , n(10). Similarly, the double precision variable e refers to 25 separate variables: e(1), e(2), ... , e(25). The number in brackets is referred to as the index or subscript of the array. The useful feature about such subscripts is that they may also be variables or expressions. For example, consider the following fragment of code:
integer i, j, k double precision a(10) c i = 1 j = 2 k = 3 c a(7) = 3.0d0 a(i) = 1.5d1 a(i*j+k) = 2.0d-3
In the above, three elements of the array a are initialised: a(7), a(1) and a(5).
The range of subscript values for a particular array may be chosen when the array is declared, by using a more flexible form of declaration. For example:
integer a(0:100) double precision b(-10:10)
Here, a takes subscripts from 0 to 100 (101 elements) whereas b takes subscripts from -10 to +10 (21 elements).
As well as simple one-dimensional arrays, multi-dimensional arrays can be used in Fortran. The most common example is a two-dimensional array, eg. a(n,m), which is useful for storing matrices. In this case, the element a(1,1) is the value for the first row, first column; a(1,2) is the value for the first row, second column; etc...
When writing programs to solve physical problems, there are often occasions when mathematical functions are required. For example, we might want to find the sine, cosine or logarithm of a number, or expression. We are fortunate that Fortran contains several built-in, or intrinsic, functions which allow us to do just that. The functions take one, or more, arguments, and return a value that may be assigned to a variable. The following example illustrates their use:
double precision x, y, z, biggest c x = 1.0d0 y = sin (x) ! This calculates the sine of x, and stores it in y z = sqrt (y) ! This finds the square root of y, storing it in z biggest = max (y, z) ! This finds the larger of y and z, and puts ! its value in biggest.
Here is a table of some of the more common functions that are available:
Function |
Description |
y = sqrt(x) |
Square root |
y = exp(x) |
Exponential |
y = log(x) |
Natural logarithm |
y = log10(x) |
Common logarithm |
y = sin(x) |
Sine, x in radians |
y = cos(x) |
Cosine, x in radians |
y = tan(x) |
Tangent, x in radian |
y = asin(x) |
Arcsine, -1 £ x £ 1 |
y = acos(x) |
Arccosine, -1 £ x £ 1 |
y = atan(x) |
Arctangent, tan-1(x) |
y = atan2(x1,x2) |
Arctangent, tan-1(x1/x2) |
y = sinh(x) |
Hyperbolic sine |
y = cosh(x) |
Hyperbolic cosine |
y = tanh(x) |
Hyperbolic tangent |
N.B. In the above, all variables are double precision.
Function |
Description |
y = abs(x) |
Absolute Value |
n = int(x) |
Truncation to integer part |
n = nint(x) |
Round to nearest integer |
y = dble(x) |
Convert x to double precision |
y = max(x1,x2) |
Obtain maximum of arguments x1,x2 |
y = min(x1,x2) |
Obtain minimum of arguments x1,x2 |
N.B. In this table, n is an integer, whereas x, x1, x2 and y can be either integer or double precision.
Write a program which will read in a data point in Cartesian coordinates and then convert
the data point into spherical polar coordinates. Store your Cartesian coordinates and spherical polar
coordinates as three element arrays. Output your answer to the screen.
Recall: Cartesian coordinates are (x,y,z) while spherical polar coordinates are
(r,q,f). The conversion is given by:
x = rcos(f)sin(q)
y = rsin(f)sin(q)
z = rcos(q)
What are the units off and q? Can you modify your program to give the results in more common units?
CHECK: Do your answers agree with the following:
(x,y,z) = (2,3,4)
after conversion gives
(r,q,f) = (5.385,42.031,56.310)
q and f are in degrees and answers have been rounded to three decimal places.
Previous: Getting Started
Return to Dr. Alex Brown's Teaching
This page maintained by alex.brown@ualberta.ca of the Department of Chemistry, University of Alberta
Last updated August 8, 2003.