1 / 29

Lecture III Start programming in Fortran

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.

shaunte
Download Presentation

Lecture III Start programming in Fortran

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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)

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

  4. Declare variables, Must at the head of block Statements: initialize variables with values statements: calculation statements:Output results Roots finding (cont.) PROGRAM ROOTSFINDING INTEGER a, b, c REAL x, y, z, w, r1, r2 a=1 b=-5 c=6 ! To calculate b*b – 4*a*c x = b*b 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

  5. Keywords in Fortran90 variables constants operators Roots finding (cont.) PROGRAM ROOTSFINDING INTEGER a, b, c REAL x, y, z, w, r1, r2 a=1 b=-5 c=6 ! To calculate b*b – 4*a*c x = b*b 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

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

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

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

  9. INTEGER a, b, c REAL x, y, z, w A=1 B=-5 C=6 INTEGER:: a=1, b=-5, c=6 REAL:: x, y, z, w Declare variables same

  10. Variable precision 1 byte = 8 bits • INTEGER(KIND=2)::a OR INTEGER(2)::a 11111111

  11. program test implicit none integer(1)::a=127 integer(2)::b=128 write(*,*) "a=", a, “b=“,b End PROGRAM Output a=127 b=-128 Example

  12. Variable precision (cont.) • REAL(KIND=4) • CHARACTER • KIND=1 only

  13. 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 value If the operands are of different types, the expression is called mixed mode.

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

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

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

  17. Arithmetic operators: precedence 2+3*4 = ? = 5 * 4 = 20 Or = 2+12 = 14 (2+3)*4 = 20

  18. Arithmetic operators: associativity 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 left 30/5*3 = 30/(5*3) = 2

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

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

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

  22. 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)

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

  24. 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!’

  25. Assignment Statement The assignment statement has syntax: variable = expression Semantics • Evaluate the expression • If the type of result is the same as the type of the variable store the result in the variable • 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 • The original value of the variable is destroyed

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

  27. Declare variables, Must at the head of block statements: calculation statements:Output results Roots finding revisited PROGRAM ROOTSFINDING INTEGER::a=1, b=-5, c=6 REAL x, r1, r2 ! To calculate b*b – 4*a*c x = b**2- 4*a*c r1 = (-b +SQRT(x))/(2*a) r2 = (-b – SQRT(x))/(2*a) WRITE(*, *) r1, r2 END PROGRAM

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

  29. What does the following program output? PROGRAM midterm REAL A,B,C INTEGER I,J,K 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 A. 3.5 10.5 1. 3 5 7 B. 3.5 12.25 1. 3 5 6 C. 3.5 10.5 1. 3 5 6 D. 3.5 10.5 1.75 3 5 6 E. None of the above Example in midterm05

More Related