# Lecture III Start programming in Fortran

Document Sample

```					 Lecture III Start
programming in
Fortran
Yi Lin
Jan 11, 2007
A simple example: Roots
finding
aX^2+bX+c=0
   Input a, b, c
   Output root(s):
(-b + SQRT(b*b - 4*a*c))/(2*a)
(-b - SQRT(b*b - 4*a*c))/(2*a)
Roots finding (cont.)
start

Initialize a=1, b=-5, c=6

w = (b*b – 4*a*c)

R1 = (-b + sqrt(w))/(2*a)
R2 = (-b - sqrt(w))/(2*a)

End
Roots finding (cont.)
PROGRAM ROOTSFINDING
INTEGER a, b, c
REAL x, y, z, w, r1, r2              Declare variables,
a=1
Must at the head of block
b=-5                         Statements:
c=6
initialize variables with values
! To calculate b*b – 4*a*c
x = b*b
y = a*c
z = 4*y
w = x-z
statements: calculation
r1 = (-b +SQRT(w))/(2*a)
r2 = (-b – SQRT(w))/(2*a)

WRITE(*, *) r1, r2                statements:Output results
END PROGRAM
Roots finding (cont.)
Keywords in Fortran90
PROGRAM ROOTSFINDING
INTEGER a, b, c               variables
REAL x, y, z, w, r1, r2

a=1
b=-5                         constants
c=6

! To calculate b*b – 4*a*c
x = b*b                          operators
y = a*c
z = 4*y
w = x-z

r1 = (-b +SQRT(w))/(2*a)
r2 = (-b –SQRT(w))/(2*a)

WRITE(*, *) r1, r2
END PROGRAM
Variables and constants
   Constants (e.g., 1, -5, 6, 4, 2)
   A variable (e.g., a, b, c, x, y, z, w) is a unique
name which a FORTRAN program applies to
a word of memory and uses to refer to it.
   Naming convention
   alphanumeric characters (letters, numerals and the
underscore character)
   The first character must not be a letter.
   No case sensitivity
   Don‟t use keywords in Fortran as variables‟ names
Variable data types
   INTEGER
   E.g., a, b, c
   REAL
   E.g., x, y, z, w
   LOGICAL
   COMPLEX
   CHARACTER
   Examples:
   Data type is important
   INTEGER::a=1,b=-5,c=6
   REAL::r1, r2
   IMPLICIT NONE needed to prevent errors.
   Comment out “IMPLICIT NONE”
   write(*, *) r1, r3 ! You want to print r1 and r2, but by mistakes you type r3
Declarations
PROGRAM RootFinding
IMPLICIT NONE

INTEGER :: a, b, c
REAL :: x,y,z,w
REAL :: r1, r2
. . .
END PROGRAM RootFinding

Declarations tell the compiler
   To allocate space in memory for a variable
   What “shape” the memory cell should be (i.e. what type of value
is to be placed there)
   What name we will use to refer to that cell
Declare variables
INTEGER a, b, c   INTEGER:: a=1, b=-5, c=6
REAL x, y, z, w   REAL:: x, y, z, w
A=1
B=-5
C=6

same
Variable precision
1 byte = 8 bits
   INTEGER(KIND=2)::a OR
INTEGER(2)::a
11111111

KIND      BITs Value range

1         8     -2**7 <= a < +2**7 (i.e., [-128,127])
2         16    -2**15 <= a < +2**15
(i.e., [-32768,-32767])
4(default) 32   -2**31 <= a < +2**31
8         64    -2**63 <= a < +2**63
Example
program test                      Output
implicit none                    a=127 b=-128
integer(1)::a=127
integer(2)::b=128

write(*,*) "a=", a, “b=“,b
End PROGRAM
Variable precision (cont.)
   REAL(KIND=4)      KIND           BITs

4(default)     32

   CHARACTER         8(equivalent to 64
   KIND=1 only   DOUBLE
PRECISION)
16             8*16
Arithmetic Expressions
An arithmetic expression is formed using the operations:
* (multiplication), / (division)
** (exponentiation)
If the operands are integers, the result will be an integer
value
If the operands are real, the result will be a real value
If the operands are of different types, the expression is
called mixed mode.
Arithmetic expressions
   Variable operator variable operator ….
   E.g., 5 + 2 *3
   E.g., b**2 – 4*a*c
   It has a value by itself
   E.g. The value of expression 5 + 2*3 is 11
Simple Expressions
   1 + 3 --> 4
   1.23 - 0.45 --> 0.78
   3 * 8 --> 24
   6.5/1.25 --> 5.2
   8.4/4.2 --> 2.0 rather than 2, since the result
must be of REAL type.
   -5**2 --> -25
   12/4 --> 3
   13/4 --> 3 rather than 3.25. Since the operands
are of INTEGER type, so is the result. The
computer will truncate the mathematical result
to make it an integer.
   3/5 --> 0 rather than 0.6.
Evaluating Complex Expressions
Evaluate operators in order of precedence.
   First evaluate operators of higher precedence
3 * 4 – 5  7
3 + 4 * 5  23
   For operators of the same precedence, use
associativity. Exponentiation is right associative, all
others are left associative
   Expressions within parentheses are evaluated first
Arithmetic operators: precedence

operators   Precedence   2+3*4 = ?
()          1
= 5 * 4 = 20
**          2
Or
*, /        3            = 2+12 = 14
+, -        4
(2+3)*4 = 20
Arithmetic operators: associativity
operators   associativity   associativity resolves the order of
operations when two operators of
()          Left to right   the same precedence compete for
three operands:
**          Right to left
2**3**4 = 2**(3**4) = 2**81,
72/12/ 3 = (72/12)/3 = 6/3 = 2
*, /        Left to right

30/5*3 = (30/5)*3 = 18
+, -        Left to right   if *,/ associativity is from right to left
30/5*3 = 30/(5*3) = 2
Examples
2 * 4 *   5 / 3 ** 2
-->   2 * 4 * 5 / [3 ** 2]
-->   2 * 4 * 5 / 9
-->   [2 * 4] * 5 / 9
-->   8 * 5 / 9
-->   [8 * 5] / 9
-->   40 / 9
-->   4

The result is 4 rather than 4.444444 since the operands are
all integers.
Examples
100 + (1 + 250 / 100) ** 3
--> 100 + (1 + [250 / 100]) ** 3
--> 100 + (1 + 2) ** 3
--> 100 + ([1 + 2]) ** 3
--> 100 + 3 ** 3
--> 100 + [3 ** 3]
--> 100 + 27
--> 127
Parentheses are evaluated first
Examples
1.0 + 2.0 *   3.0 / ( 6.0*6.0 + 5.0*44.0) ** 0.25
--> 1.0   + [2.0 * 3.0] / (6.0*6.0 + 5.0*44.0) ** 0.25
--> 1.0   + 6.0 / (6.0*6.0 + 5.0*55.0) ** 0.25
--> 1.0   + 6.0 / ([6.0*6.0] + 5.0*44.0) ** 0.25
--> 1.0   + 6.0 / (36.0 + 5.0*44.0) ** 0.25
--> 1.0   + 6.0 / (36.0 + [5.0*44.0]) ** 0.25
--> 1.0   + 6.0 / (36.0 + 220.0) ** 0.25
--> 1.0   + 6.0 / ([36.0 + 220.0]) ** 0.25
--> 1.0   + 6.0 / 256.0 ** 0.25
--> 1.0   + 6.0 / [256.0 ** 0.25]
--> 1.0   + 6.0 / 4.0
--> 1.0   + [6.0 / 4.0]
--> 1.0   + 1.5
--> 2.5
Mixed Mode Expressions
If one operand of an arithmetic operator is INTEGER and
the other is REAL
 the INTEGER value is converted to REAL
 the operation is performed

 the result is REAL
1 + 2.5  3.5
1/2.0  0.5
2.0/8  0.25
-3**2.0  -9.0
4.0**(1/2)  1.0 (since 1/2  0)
Example of Mixed Mode
25.0   ** 1 / 2 * 3.5 ** (1 / 3)
    25.0 ** 1 / 2 * 3.5 ** (1 / 3)
    25.0 ** 1 / 2 * 3.5 ** (1 / 3)
    25.0 ** 1 / 2 * 3.5 ** ([1 / 3])
    [25.0 ** 1] / 2 * 3.5 ** 0
    25.0 / 2 * [3.5 ** 0]
    25.0 / 2 * 1.0
    [25.0 / 2] * 1.0
    12.5 * 1.0
    12.5
Statements
   Assignment statement:
Variable = expression
e.g., b = -5
(never the other way around, show an example)
   General statement:
INTEGER a, b, c
write (*,*) „Hello world!‟
Assignment Statement
The assignment statement has syntax:
variable = expression
Semantics
1.    Evaluate the expression
2.    If the type of result is the same as the type of the variable store
the result in the variable
3.    Otherwise convert the value to the type of the variable and
then store it
–    If the value is REAL and the variable is INTEGER remove
the decimal part (truncate)
–    If the value is INTEGER and the variable is REAL convert
the value to a decimal
4.    The original value of the variable is destroyed
Assignment statements
examples
REAL::X=3.5
X=2 * 2.4 ! X=4.8

INTEGER X=3
X=2 * 2.4 ! X=4
X=2 * 2.6 ! X=5
Roots finding revisited
PROGRAM
ROOTSFINDING                 Declare variables,
INTEGER::a=1, b=-5,
c=6                          Must at the head of block
REAL x, r1, r2

! To calculate b*b – 4*a*c
x = b**2- 4*a*c
statements: calculation

r1 = (-b +SQRT(x))/(2*a)     statements:Output results
r2 = (-b – SQRT(x))/(2*a)

WRITE(*, *) r1, r2
END PROGRAM
Example in a previous final
PROGRAM Q1
IMPLICIT NONE
INTEGER :: I, J, K
REAL :: A, B, C
A = 3.2 + 4
B = 22/7
C = 22.0/7.0
I = 3.7 + MOD(78,5)
J = 45/3*3+1-3*4
K=C
WRITE(*,*) I,J,K,A,B,C
END PROGRAM Q1
a) 6            34     3       7.200   3.000   3.143
b) 7.200        3.000 3.143    6       34      3
c) 6.700        34.000 3.143   7.200   3.000   3.143
d) 6            34     3       7.200   3.143   3.143
Example in midterm05
What does the following program   A. 3.5 10.5     1. 3 5     7
output?
B. 3.5 12.25 1. 3 5        6
PROGRAM midterm
REAL A,B,C                      C. 3.5 10.5     1. 3 5     6
INTEGER I,J,K                   D. 3.5 10.5     1.75 3 5   6
E. None of the above
A = 3.5
I=A
J = 5.25
K = I*2
B = A*I
C = J/3

WRITE (*,*) A,B,C,I,J,K

END PROGRAM midterm

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 8 posted: 5/1/2010 language: English pages: 29
How are you planning on using Docstoc?