Lecture III Start programming in Fortran Yi Lin Jan 11, 2007

Preview:

Citation preview

Lecture III Start programming in

Fortran

Yi Lin

Jan 11, 2007

A simple example: Roots finding

Finding roots for quadratic 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, cREAL x, y, z, w, r1, r2

a=1b=-5c=6

! To calculate b*b – 4*a*cx = b*by = a*cz = 4*yw = x-z

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

WRITE(*, *) r1, r2END PROGRAM

Declare variables,Must at the head of block

Statements:initialize variables with values

statements: calculation

statements:Output results

Roots finding (cont.)

PROGRAM ROOTSFINDINGINTEGER a, b, cREAL x, y, z, w, r1, r2

a=1b=-5c=6

! To calculate b*b – 4*a*cx = b*by = a*cz = 4*yw = x-z

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

WRITE(*, *) r1, r2END PROGRAM

variables

constants

operators

Keywords in Fortran90

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

DeclarationsPROGRAM 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

REAL x, y, z, w

A=1

B=-5

C=6

INTEGER:: a=1, b=-5, c=6REAL:: x, y, z, w

same

Variable precision INTEGER(KIND=2)::a OR

INTEGER(2)::a

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

11111111

1 byte = 8 bits

Example

program test

implicit none

integer(1)::a=127

integer(2)::b=128

write(*,*) "a=", a, “b=“,b

End PROGRAM

Outputa=127 b=-128

Variable precision (cont.)

REAL(KIND=4)

CHARACTER KIND=1 only

KIND BITs

4(default) 32

8(equivalent to DOUBLE PRECISION)

64

16 8*16

Arithmetic Expressions

An arithmetic expression is formed using the operations:+ (addition), - (subtraction)* (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 valueIf 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

2+3*4 = ?

= 5 * 4 = 20

Or

= 2+12 = 14

(2+3)*4 = 20

operators Precedence

() 1

** 2

*, / 3

+, - 4

Arithmetic operators: associativityoperators associativity

() Left to right

** Right to left

*, / Left to right

+, - Left to right

associativity resolves the order of operations when two operators of the same precedence compete for three operands:

2**3**4 = 2**(3**4) = 2**81, 72/12/ 3 = (72/12)/3 = 6/3 = 2 30/5*3 = (30/5)*3 = 18 if *,/ associativity is from right to left30/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

Examples1.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

Semantics1. Evaluate the expression2. If the type of result is the same as the type of the variable store

the result in the variable3. 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 decimal4. 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 revisitedPROGRAM

ROOTSFINDINGINTEGER::a=1, b=-5, c=6REAL x, r1, r2

! To calculate b*b – 4*a*cx = b**2- 4*a*c

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

WRITE(*, *) r1, r2END PROGRAM

Declare variables,Must at the head of block

statements: calculation

statements:Output results

Example in a previous finalPROGRAM Q1

IMPLICIT NONEINTEGER :: I, J, KREAL :: A, B, CA = 3.2 + 4B = 22/7C = 22.0/7.0I = 3.7 + MOD(78,5)J = 45/3*3+1-3*4K = CWRITE(*,*) I,J,K,A,B,C

END PROGRAM Q1a) 6 34 3 7.200 3.000 3.143b) 7.200 3.000 3.143 6 34 3c) 6.700 34.000 3.143 7.200 3.000 3.143d) 6 34 3 7.200 3.143 3.143

Example in midterm05What does the following program

output?PROGRAM midterm

REAL A,B,CINTEGER I,J,K

A = 3.5I = AJ = 5.25K = I*2B = A*IC = J/3

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

END PROGRAM midterm

A. 3.5 10.5 1. 3 5 7B. 3.5 12.25 1. 3 5 6C. 3.5 10.5 1. 3 5 6D. 3.5 10.5 1.75 3 5 6E. None of the above

Recommended