You are on page 1of 16

‫‪Numerical Analysis‬‬ ‫‪Ch I: Fundamentals of FORTRAN Programming‬‬

‫التحليل العددي‬
‫‪Numerical Analysis‬‬

‫إعداد‬
‫دكتور مهندس‪ /‬ابراهيم عمر محمد رحومة‬
‫رئيس قسم الهندسة الميكانيكية ‪ +‬مدير مكتب البحث والتطوير‬
‫كلية الهندسة تاجوراء‬
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Chapter I

Fundamentals of FORTRAN Programming

Errors in Computation
Digital computers are highly accurate devices that are capable of handling numbers
ranging from extremely small to the extremely large (for example, numbers ranging from
10-38 to 1038). Why then are we concern with Errors in computation. There are many
reasons:

1- The given set of data contain errors (in observation, measurement, recording,
transmission, conversion and in preprocessing)
2- Errors due to mathematical model used (approximate methods of solution)
3- Errors produced during computations (round-off errors, truncation errors)

Round-off errors
This is caused by the fact that computers work with only a finite number of digits. (not
causing much of a problem with modern computers).

Truncation errors
Truncation error is the error caused by truncating an infinite series after a given number
of terms.

Absolute and relative errors

The absolute error Eabs is given by:


Eabs= (Calculated value) – (True value) (1.1)

The relative error Erel is given by:


Erel= (Calculated value) – (True value) / (True value) (1.2)

The percentage error Epct is given by:


Epct= Erel * 100 (1.3)

Example:
suppose the true value for a calculation is 25.0 but the calculated value is 24.0:
then,
Eabs = 24.0 - 25.0 = -1.0
Erel = ( 24.0 - 25.0 ) / 25.0 = - 0.04
Epct = - 0.04 * 100 = - 4 %
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

FORTRAN Programming
FORTRAN (FORmula TRANslation) is a procedure-oriented computer language that is
especially suited for scientific computations.

Algorithm
The algorithm is a step-by-step computational procedure, which organizes the numerical
method in an order of sequence of computational steps, with decision instructions
including all possibilities.
Computer programs can be broken down into three major steps:

(1) Reading the input data.


(2) Processing the input data to obtain the desired output information.
(3) Writing out the results.

Read Process Write


Start input data input data output Stop
data

Example1: Area and Circumference of a circle

Suppose that we want to calculate the area and circumference of


a circle whose radius is known. The outline of computation is as
following:
1. Read the radius, R
2. Calculate area ( A= π R2 )
3. Calculate circumference ( C = 2πR )
4. Write R, A, C
5. Stop

A flow chart for this example can be drawn as following.

Read Compute Write


Start R A, C R, A, C Stop
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Example2: Roots of quadratic equation

Set up an algorithm of computing the roots of the following quadratic equation:


A x2 + b x + c = 0

Solution:

Step 1.Read the constants A, B, and C

Step 2.Compute the discriminate (d)


d = b2 – 4 a c

Step 3.Test the discriminate (d) i.e. ( compare algebraically with 0 )


If d < 0, go to step 3a
If d = 0, go to step 3b
If d > 0, go to step 3c

3a. Roots are complex and conjugates.


XR1 = - b / 2 a XI1 =

XR2 = - b / 2 a XI2 =

3b. Roots are real and equal.


X1 = - b / 2 a X2 = - b / 2 a

3c. Roots are real and unequal.

Step4. Output the computed roots.


Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Flowchart

Data input symbol

Counter symbol (incrementing index)

computaion symbol

Direction of flow symbol

Connector symbol

Decision symbol

Output symbol

Subroutine symbol

Arithmetic statements

Arithmetic statement consists of two parts, separated by equals ( = ) sign: a variable on


the left and an expression on the right

Variable = Expression

Constants and Variables


Integer constant is written as a string of digits without a decimal point
Example: 3 0 0002 -525 1234567890
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Real constants are written as numbers with a decimal point (decimal point must not be
omitted from a real constant even if its value is an exact number)
Example: 2.0 3.13159 -0.00165 175.3E6 -8.32E-4

A variable is a name given to a numerical quantity. Each variable name consists of


characters (letters or numbers), the first of which must be a letter.
Variable names begin with letters I, J, K, L, M or N represent integer quantity. While real
variable begins with any other letter.

Example:

The following variables are integer type:


I IX MASSI15 L3 LAST

And the following variables are real type:


A X TEMP F5 RESULT ALPHA

Arithmetic Expressions

The FORTRAN five arithmetic operators, together with the symbols that present them,
are in the following table:

Table 1.1 Arithmetic operators


Operation symbol

Addition +
Subtraction -
Multiplication *
Division /
Exponentiation **

The operations within any expression are performed in the following order of precedence:
First ( )
Second **
Third *, /
Forth +, -
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Example: The operation order for the formation of expression is indicated by the
numbers under the operation symbols.

A * X ** 2 + B * X + C
2 1 4 3 5

The following are examples of mathematical expressions and their FORTRAN


expressions

Mathematical Expression FORTRAN Expression

a x2 + b x + c A * X **2 + B * X + C
x2 + y2 + Z2 X ** 2 + Y ** 2 + Z ** 2
1 – sin2 x 1.0 – SIN(X) ** 2

Problems:

(1) Write a FORTRAN expression, which corresponds to each algebraic


equation:

(1) z = (x/y) + 3 (2) f = A sin t


(3) x2 + y2 (4) ( x + y )2
(5) (4 t ) 1/6 (6) A = 2 π r

(2) Each of the following statements contains at least one error. Identify the errors.

1- 4*T
2- X = 2 (P + Q )
3- ( A + B ) ** -5
4- K1 + K2 = 0.5
5- X1 = (( A+B )/C)**2 – (A-B)/C)**2)
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

INPUT/ OUTPUT statements


Most of programs involve the reading of data and the printing of results

READ Statement

20 READ ( 5,100 ) I, A, B, C

List of variables to be read.


FORMAT statement number
Input device number
Statement number

Write Statement

30 WRITE ( 5,100 ) I, A, B, C

List of variables to be written


FORMAT statement number
Output device number
Statement number

Format Statement
FORMAT statement must accompany each formatted READ or WRITE statement in a
FORTRAN program, its purpose is:
1. To specify the number of quantities to be read or written.
2. To specify the maximum number of characters in each quantity.
3. To specify whether each quantity is an integer, real or real with exponent constant.

Example:
100 FORMAT (I4, 3F6.3, 2E12.4)

Simple (Unformatted) Input/Output Statement


The simplest form of the input and output statements are called list directed free form
input/output.

The free form input statement has the form: READ *, List
And the free form output statement has the form: PRINT *, List

Example:
READ *, X, Y, X1, Y1
PRINT *, X, Y, (X+Y) /2.0, 1, X1, 2, Y1
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Control Statement

FORTRAN language has several control statements, such as GO TO, IF, DO. These
statements, which can be combined with one another, and with arithmetic and I/O
statements, allow the programmer to make logical decisions, to alter the sequence of
computation.

GO TO Statement

The GO TO statement allows us to jump from one portion of program to another. It is written in
the form:
GO TO Statement number

Example: GO TO 20
where 20 is the number of statement, which will be executed next. This
statement should appear some where in the program and it must be
numbered.

IF statement

IF statement allows the programmer to test the sign of an arithmetic expression and then
jumps to some other part of the program.

There are three types of IF statements exist in Fortran:


1. The Arithmetic IF statement
The arithmetic IF statement transfers control to up to three target statements. An
example of arithmetic IF statement is:

Example:
IF (n-10) 10,20,30

Go to this statement number if expression is POSITIVE


Go to this statement number if expression is ZERO
Go to this statement number if expression is NEGATIVE
Arithmetic expression to be tested

2. The logical IF statement


This type consists of only one statement that is executed if a condition tests true

Example:
IF (A .EQ. B) GO TO 100

Go to this statement number if the logical expression is true.


Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Logical expression
Logical Expression
Logical expression is some combination of constants, variables or arithmetic
expressions, which connected by means of logical operators (relational operators) to
represent a condition which either true or false.
The logical (relational) operator is one of the following:

Table 1.2 Logical operators


Equivalent
Symbol Notation meaning
(FORTRAN 90)

.EQ. == = Equals
.NE. /= ≠ Not equal
.LT. < < Less than
.LE. <= ≤ Less than or equal
.GT. > > Greater than
.GE. >= ≥ Greater than or equal

3. The IF - THEN - ELSE statement


This statement is composed of blocks of code that are executed if a condition tests
true

The form of the IF construct is:


IF [condition] THEN
block
ELSE
block
END IF

Example:
IF (A > 0) THEN
B = C/A
ELSE
B = ABS (C)
D=0
END IF
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

This type of IF statement can contain several ELSE IF statements, but only one ELSE
statement. IF blocks can be nested within one another.

DO statement

The DO statement allows us to repeat a sequence of statements within a FORTRAN


program a specific number of times.

Example:
DO 50 I = 1, 19, 2

Increment value, indicates the amount by which I increased each time


(constant or variable assumed equals 1 if not shown)
Final value of I (constant or variable)
Initial value of I (constant or variable)
Counter or index
Statement number of the last statement in the sequence.

In this example the sequence of statements beginning with the DO statement and ending
with the statement number 50 (arithmetic, I/O statement or a CONTINUE statement) will
be executed 10 times; first for I=1, then for I=3, and so on, until I=19 the last time
through.

Examples of DO statements:

1. DO 60 J = 1, N
…….
…….
60 CONTINUE

2. DO K = I,500,4
……
……
END DO

DIMENSION

DIMENSION statement consists of the keyword DIMENSION followed by a list of array


names separated by commas each array name is immediately followed by a pair of
parentheses containing integer constants, which specify the largest allowable subscript
values.
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Example:

DIMENSION A(50), X(100,100)


FUNCTIONS
FORTRAN has three types of functions:

1. Predefined functions: FORTRAN language contains a list of library functions for


carrying out simple tasks like taking sins and cosines of angles. Some of the more
common functions are listed in Table 1.3

Table 1.3 mathematical library functions


Operation Function name Application

Absolute value ABS Y = ABS(X)


Square root SQRT Y = SQRT(X)
Exponential EXP Y = EXP(X)
Natural logarithm LOG Y = LOG(X)
Sine SIN Y = SIN(X)
Cosine COS Y = COS(X)
Tangent TAN Y = TAN(X)
Smallest value MIN Y = MIN(X1,X2,…)
Largest value MAX Y = MAX(X1,X2,…)

2. Statement function, which defined by a single statement that appears within the
main program.
Example:
F(x) = x**2-1
X=3.0
Y=2.0
Z=Y+2*F(X)

3. External function, which is defined by an independent block of statement


prepared by the programmer and appended to the main program.

Example:
x=81
z=root(x)
print*,z
stop
end

function root(x)
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

root=x**0.5
return
end
General Examples on FORTRAN

1. The following program computes the circumference, C and the area, A of any
circle after reading its radius, R.

READ *, R
PI = 3.14
C = 2 * PI * R
A = PI * R**2
PRINT *,C,A
STOP
END

2. The next example reads values of 5 elements and find out the largest element. The
predefined function MAX is used.

dimension A(10)
do 5 i=1,5,1
read*,A(i)
5 continue
MN=max(A(1),A(2),A(3),A(4),A(5))
print*,MN
stop
end

3. The following program calculates the following expression:

Where
and A, B, C are constants

F(x)=x**5+2*x**3-x+15
A=1.0
B=1.0
C=1.0
V=(F(A)+F(B))/F(C)
print*,V
stop
end
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

Taylor series

The well-known Taylor series representation of a function f(x) can be easily derived by
the method of undetermined coefficients, as follows:
Suppose that we have a function f(x) which has derivatives of all orders up to and
including n at the same point x = x0 and we want to obtain the function for any x in the
neighborhood of x0

Assume that f(x) can be expressed as a convergent finite series of the following form:

(1.4)

Calculating the derivatives of the above series, we get

(1.5)

…………
…………
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

The unknown coefficients a0, a1, a2, …. an, …… can be determined by evaluating the
series and its derivatives at x = x0, as follows:

(1.6)

Substituting the values of ak, (where k=0,1,2,3,…n,….) determined by equation (1.6),


into equation (1.4) we obtain the following series, which known as the classical form of
the Taylor Series of f(x) in the neighborhood of point x = x0.

(1.7)

Taylor series (1.7) may be expressed in one of the following forms:

(1.8)
or

(1.9)
where

Example: the series for the sin of an angle x. ( i.e. f(x) = sin x ) can be expressed in
terms of Taylor series at x0 = 0 as following :

The series become:


Numerical Analysis Ch I: Fundamentals of FORTRAN Programming

sin(x) = 0 + x + 0 - +0+ +…

sin(x) = x - + - + -…

Problem:
Express the following functions in terms of Taylor series about x0 = 0
1- Cosine of an angle x ( cos x )
2- Series of ex
3- Natural logarithm of x ( ln x )

You might also like