Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental of Fortran Part 2 Dr.Entesar Ganash. 1. Program layout The general structure of a simple Fortran program is given as the following: ------------------------------------

Similar presentations


Presentation on theme: "Fundamental of Fortran Part 2 Dr.Entesar Ganash. 1. Program layout The general structure of a simple Fortran program is given as the following: ------------------------------------"— Presentation transcript:

1 Fundamental of Fortran Part 2 Dr.Entesar Ganash

2 1. Program layout The general structure of a simple Fortran program is given as the following: ------------------------------------ [PROGRAM program name] [declaration statements] [executable statements] End [PROGRAM [program name]] ------------------------------------ it should be pointed out the items in brackets are optinal.

3 3 1 Some useful intrinsic functions 2 4 Function name in Fortran Meaning Example ABS(x)Absolute value ABS (-4.0)= 4.0000 SQRT(x)Square root SQRT(4.0)=2.0000 EXP(x)Exponential function EXP(4.0)=54.59 LOG (x) Natural logarithm LOG (4.0)=1.38629 LOG10 (x) Common (base 10) logarithm ? SIN(x)sine SIN(30.0)=-0.988032 Why not.5? COS(x) cosine ? TAN(x)tangent ? ASIN(x)Inverse sine ( arc sine) ? ACOS(x) Inverse cosine ATAN(x)Inverse tangent SINH(x)Hyperbolic sine

4 3 1 Some useful intrinsic functions 2 4 Function name in Fortran Meaning Example INT(A)Convert to integer type INT(3.9)=3 NINT(A)Integer nearest to A NINT(3.9)=4 REAL(A)Convert to real REAL (2/4)=0.0 but REAL (2)/4=0.5 as 2.0/4=0.5 MAX(A1,A2,A3) All same type Maximum of argument MAX(2,3,1)= 3 MIN(A1,A2,A3)Minimum of argument MIN(2,3,1)= 1 CONJG (Z)Conjugate of complex z CONJG(.5+i)= (0.5,-1.0) where i=(0.0,1.0) REAL(Z) Give a real part of complex number z REAL (.5+i)= 0.500000 AIMAG (Z)Give a imaginary part of complex number z AIMAG (.5+2.0*i)=2.00000

5 3 1 Numeric Expressions A formula combining constants, variables and function using arithmetic operations Let us see this E.g. E.g. Translate the following expression into Fortran z + c / a**3 +b Wrong (z + c) / (a**(3 +b) ) Right (z + c) / (a**(3 +b) ) 2 4

6 3 1 Numeric Assignment To calculate the value of numeric expression and assign it to a variable Variable =Expression, It should be pointed out the equal sign does not have the same meaning as the equal sign in mathematics and should read as “ becomes’ E.g. x= a + b N=N+1 the assignment is clear but the in the form of mathematical equation is not clear E.g. 2 4

7 Simple Input and Output The important form to transfer data is READ and PRINT statements. Input: READ*, A, B, C Read(*,*) is the same as Read*, Or if you need to read data from an external file that is called for example (data) OPEN ( 1, File =‘ data’) READ (1,*) A, B, C --------------------------------------------------------------------------------------------- Output: PRINT*, A, B, C the result is 2 4 5 on the black screen If you want the result will be displayed as A=2 B=4 C=5 (HOW ?) a quotes (‘) or apostrophes (“) will be used for that PRINT*, ‘A=’, A, ‘B=’, B, ‘C=’, C PRINT*, ‘ Welcome to phys 393’ on the black screen Write (*,*) is the same as PRINT*, Sending output to the printer OPEN ( 2, File =‘ outdata’) WRITE(2, *) ‘ Welcome to phys 393’ on the file, which is called outdata Unit file File name This might be discussed in details later

8 Exercise Write a program to read a Celsius temperature and convert it to Fahrenheit, using the formula PROGRAM TEMP ! A program to convert a Celsius temperature to Fahrenheit ! Implicit none Real :: TC, TF !a Celsius & a Fahrenheit temperature Print*,' What is the Celsius temperature?' ! Read*,TC ! TF= 9.0*TC/5.0+32.0 Print*, 'TC=',TC, 'TF=',TF END PROGRAM TEMP

9 PROGRAM TEMP ! A program to convert a Celsius temperature to Fahrenheit ! Implicit none Real :: TC, TF !a Celsius & a Fahrenheit temperature Print*,' What is the Celsius temperature?' ! Read*,TC ! TF= 9.0*TC/5.0+32.0 Print*, 'TC=',TC, 'TF=',TF END PROGRAM TEMP Example Write a program to read a Celsius temperature and convert it to Fahrenheit, using the formula [PROGRAM program name] [declaration statement] [executable statements] End [PROGRAM [program name]]

10 Write a program to read a Celsius temperature and convert it to Fahrenheit, using the formula Exercise

11 PROGRAM TEMP ! A program to convert a Celsius temperature to Fahrenheit ! Implicit none Real TC, TF !a Celsius & a Fahrenheit temperature Print*,' What is the Celsius temperature?' ! Read*,TC ! TF= 9.0*TC/5.0+32.0 Print*, 'TC=',TC, 'TF=',TF END PROGRAM TEMP Exercise Write a program to read a Celsius temperature and convert it to Fahrenheit, using the formula What is the Celsius temperature?.5 TC= 0.500000 TF= 32.9000 Press RETURN to close window... Just for amplification

12 Some useful notes Statements form is the basis of any Fortran program and could contain from 0-132 characters. Comments : you can make any comment by using an exclamation mark (!) before any expressions. This will help you to remember things you did before E.g. ! this is a problem to solve the 2 roots of equation. Continuation lines: some time you can not write a long statement on one line and you need other lines. In this case, you must put ampersand (&) E.g. A=4.0+ B-10 +……………………………………………………………….+ & 3.0+ C- 6.0 or A=4.0+ B-10 +……………………………………………………………….+ & 3.0+ C- 6.0 An & at the end of the comment line will not continue the comment E.g. ! A=4.0+ B-10 +……………………………………………………………….+ & C- 6.0

13 Fortran is insensitive from a capital or small letter E.g. A or a represents the same thing. apostrophes(‘) or quotes (“) are used to display some words E.g. "Welcome to physics 393 “ At the start of all programs, the following statement: IMPLICIT NON is strongly recommended (why?).

14 References Hahn, B.D., 1994, Fortran 90 For Scientists and Engineers, Elsevier CH2 Ellis, T.M.R., 1990, Fortran 77 Programming with an introduction to Fortran 90 standard (2nd Ed.), Addison.Wesley Publishing Company http://www2.ph.ed.ac.uk/~playfer/f77_tut.html#intro السيد, ابوبكر احمد,1992, برمجة الحاسب بلغة الفوتران, جامعة الكويت, دار القلم للنشر والتوزيع


Download ppt "Fundamental of Fortran Part 2 Dr.Entesar Ganash. 1. Program layout The general structure of a simple Fortran program is given as the following: ------------------------------------"

Similar presentations


Ads by Google