Professional Documents
Culture Documents
CH1 Ibrahim
CH1 Ibrahim
التحليل العددي
Numerical Analysis
إعداد
دكتور مهندس /ابراهيم عمر محمد رحومة
رئيس قسم الهندسة الميكانيكية +مدير مكتب البحث والتطوير
كلية الهندسة تاجوراء
Numerical Analysis Ch I: Fundamentals of FORTRAN Programming
Chapter I
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.
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:
Solution:
XR2 = - b / 2 a XI2 =
Flowchart
computaion symbol
Connector symbol
Decision symbol
Output symbol
Subroutine symbol
Arithmetic statements
Variable = Expression
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
Example:
Arithmetic Expressions
The FORTRAN five arithmetic operators, together with the symbols that present them,
are in the following table:
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
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:
(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
READ Statement
20 READ ( 5,100 ) I, A, B, C
Write Statement
30 WRITE ( 5,100 ) I, A, B, C
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)
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.
Example:
IF (n-10) 10,20,30
Example:
IF (A .EQ. B) GO TO 100
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:
.EQ. == = Equals
.NE. /= ≠ Not equal
.LT. < < Less than
.LE. <= ≤ Less than or equal
.GT. > > Greater than
.GE. >= ≥ Greater than or equal
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
Example:
DO 50 I = 1, 19, 2
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
Example:
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)
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
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)
(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)
(1.7)
(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 :
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 )