0% found this document useful (0 votes)
12 views70 pages

Computational PhysicsCourse Pages 1

The document provides an introduction to FORTRAN 90, a high-level programming language primarily used for mathematical computations in scientific applications. It covers the history of FORTRAN, its various versions, and fundamental programming concepts such as variables, data types, and program structure. Additionally, it includes guidelines for good programming practices and an overview of derived data types and arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views70 pages

Computational PhysicsCourse Pages 1

The document provides an introduction to FORTRAN 90, a high-level programming language primarily used for mathematical computations in scientific applications. It covers the history of FORTRAN, its various versions, and fundamental programming concepts such as variables, data types, and program structure. Additionally, it includes guidelines for good programming practices and an overview of derived data types and arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Jimma University

College of Natural sciences


Department of Physics

Phys 602, Semester II, 2017

Computational Physics

Solomon Negash

1
Introduction to Computational Physics – Solomon N.
1. Introduction to FORTRAN 90
Introduction
FORTRAN is a general purpose programming language, mainly intended for
mathematical computations in science applications (eg. physics and engineering).

FORTRAN is an acronym for FORmula TRANslation, invented 1954 by John


Backus and his team at IBM.

In 1957 the the first FORTRAN compiler was released for the IBM 704.

Based on user feedback it was improved and called FORTRAN II in 1958.

American Standards Association (ASA) later called American National Standards


Institute (ANSI) standardized FORTRAN in 1966 which is called FORTRAN IV
(FORTRAN 66)

2
Introduction to Computational Physics – Solomon N.
FORTRAN 66 (ISO Standard 1972)

FORTRAN 77 (ANSI Standard 1978) – very popular and widely used

FORTRAN 90 (ISO Standard 1991, ANSI Standard 1992)

– Has many new features compared to FORTRAN 77

FORTRAN 95 (ISO Standard 1996) – only a few minor changes made

FORTRAN 2003 (ISO Standard 2004)

FORTRAN 2008 (ISO Standard 2010)

FORTRAN 2018 (ISO Standard 2018) – latest FORTRAN version

Hardware and software


A computer system is built from hardware and software.

The hardware is the physical medium, e.g. CPU, memory, keyboard, display etc.
3
Introduction to Computational Physics – Solomon N.
A piece of software is a set of computer program, e.g. operating system,
compilers, editors, Fortran 90 programs.

The software allows the hardware to be used, programs vary enormously in


size and complexity.

FORTRAN was the first high-level programming language.

High-level languages allow a simpler notation English-like words and math-like


expressions:

Fortran 90 is a high-level language, which is called “third-generation” or 3GL

Compilers translate into machine instructions – a linker then creates an


executable program – the operating system runs the executable.

4
Introduction to Computational Physics – Solomon N.
Variables and Statements
A FORTRAN program consists of a series of statements – which is made up of
alphanumeric, underscore (_) and special characters (see Table 1, p. 6)
Example: x = (-y + root_of_discriminant)/2.0*a)
The alphanumeric characters are the 26 letters, the 10 digits
Statements may contain from 0 to 132 characters per line (a statement may be
blank)
Two basic types of statements are
– Executable: describes the action a program takes when it executes.
– Non-executable: provides an information necessary for a proper operation
of the program.
All statements, except the assignment statement (e.g. BALANCE = 1000), start
with a keyword – END, PRINT, PROGRAM, and REAL.
Generally, there will be one statement per line.
However, multiple statements may appear on a line if they are separated by
semi-colons. eg. A = 2; B = 2; C = 1 5
Introduction to Computational Physics – Solomon N.
Variables and Statements

6
Introduction to Computational Physics – Solomon N.
Variables and Statements

Any characters following an exclamation mark ! are a comment and are


ignored by the compiler. Example: x = y/a - b ! Solve the linear equation
If a statement is too long to fit on a line, it will be continued on the next line by
& up to 39 additional continuation lines are allowed.
Example: x= &
(-y + root_of_discriminant) &
/2.0*a)
Names and Variables
A name (helps to reference many different entities in FORTRAN) must consist
of b/n 1 and 31 alphanumeric characters, and must start with a letter.
The alphanumeric characters are the 26 letters, the 10 digits, and the
underscore (_)
Except in the case of character strings, FORTRAN 90 is case insensitive, i.e. the
names MYNAME and MyName represent the same thing.
7
Introduction to Computational Physics – Solomon N.
Some valid and invalid names.
valid names invalid names (why?)
x x+y
r2d2 shadow fax
pay_day 2a
endofthemonth obi-wan
q123 $sign
A name in a program must be unique, Example: If a program is named MONEY,
a variable of the same name should not be used.
A variable is a memory location whose value may be changed during execution
of a program.
A variable's name is constructed according to the above rules.
A variable has a type which determines the type of constant it may hold.
It is given a type in a type declaration, e.g.
Integer :: x
Real :: interest
Character:: letter
Real, parameter :: a = 1 8
Introduction to Computational Physics – Solomon N.
Structure of FORTRAN program
A FORTRAN program is divided into three sections
a) The declaration
b) The execution section
c) The termination section

PROGRAM program name PROGRAM suma


Implicit none !This program sum two numbers and print
!the result
IMPLICIT NONE
[declaration statements] REAL:: a, b, c
a = 2.0
[executable statements] b = 4.0
c = a + b !sum
PRINT*, “sum = ”, c
END PROGRAM program name END PROGRAM suma
9
Introduction to Computational Physics – Solomon N.
Data types
A data type consists of a set of data values (e.g. the whole numbers), a means of
denoting those values and a set of operations that are allowed on them.
Intrinsic data type
Numeric
Integer
Numerical Constant
Real calculations
Complex
Non-numeric
Variable
Logical Text processing
Character and control

CONSTANT PARAMETER
Derived data type
The ability to define data type of interest to the programmer
Example: matrices, geometrical shapes, lists, interval numbers
10
Introduction to Computational Physics – Solomon N.
Integer, real, complex
Integer: positive, negative and zero

n bits Smallest integer Largest integer


64
32

Real = Mantissa * Exponent Example:

Mantissa –› precision and Exponent –› range


n bits Precision Smallest real Largest real
64 15 – 18 ~0.5E-324 ~ 1.8E+308
32 6–9 ~1.4E-45 ~ 1.7E+38

Complex = (real or integer, imaginary)


Overflow/Underflow –› integer, Roundoff –› real
11
Introduction to Computational Physics – Solomon N.
Intrinsic data types are further specified by their kind parameter, which is an
integer.
The kind attribute is used for selecting the precision of a numerical constant or
variable.

SELECTED_INT_KIND ( ) – to specify the range of integer

SELECTED_REAL_KIND ( , ) – to specify the precision and range of real

INTEGER, PARAMETER :: k2 = SELECTED_INT_KIND (2)

INTEGER (k2) :: i, j

The KIND specifies that INTEGER variables i and j can hold 2-digit integers.

SELECTED_INT_KIND(10) means an integer KIND of no more than 10 digits.

If SELECTED_INT_KIND() returns -1, this means the hardware does not


support the requested KIND. 12
Introduction to Computational Physics – Solomon N.
KIND type parameter

INTEGER, PARAMETER :: LONG = SELECTED_REAL_KIND (15,308)

REAL (LONG) :: area

SELECTED_REAL_KIND(15, 308) selects a REAL KIND of 15 significant digits


and 308 digits for the exponent.

INTRINSIC FUNCTIONS KIND TYPES

KIND(X) – Returns the kind type


HUGE(X) – Returns the largest number
PRECISION(X) – Returns the decimal precision
EPSILON(X) – Smallest difference between two reals

13
Introduction to Computational Physics – Solomon N.
Arithmetic expressions
Arithmetic operator symbol
Operator symbol Arithmetic operation
+ Addition
- Subtraction or unary minus
* Multiplication
/ Division
** Exponentiation

Precedence of arithmetic operators

Operator symbol Precedence


Unariy - First
** Second
*, / Third
+, - Last
14
Introduction to Computational Physics – Solomon N.
FORTRAN intrinsic functions
Mathematical intrinsic functions

Numeric Intrinsic Functions

15
Introduction to Computational Physics – Solomon N.
Good programming guidelines
Use comments to clarify the purpose of both sections of the
program and the whole program

Choose meaningful names in your programs.

Use indentation to highlight the structure of the program.

Remember that the program has to be read and understood by


both humans and a computer.

Use IMPLICINT NONE to minimize errors

All variables starting with i, j, k, l, m and n, if not declared, are of


the INTEGER type by default.
IMPLICIT NONE means all names must be declared 16
Introduction to Computational Physics – Solomon N.
Logical statements
Containing a value of logical data type

Constant

.TRUE.
.FALSE.
Declaration of logical constant

LOGICAL, PARAMETER :: vara1, vara2


Logical variable

LOGICAL :: vara1, vara2

17
Introduction to Computational Physics – Solomon N.
Character

Is a string of character enclosed in single ‘ and double “ quotes


CHARACTER :: ALPHA ! length of 1
CHARACTER (15) :: Name ! length of 15
CHARACTER :: Word*5 ! length of 5

Assignment is done as follows:

Name = "Asfaw, N"

Constant character

Used to print descriptive information


CHARACTER, PARAMETER :: error_message = “unknown error”
Character (len = <len>) :: vara1 or
Character ( ) :: vara1
18
Introduction to Computational Physics – Solomon N.
Character
The characters are not restricted to the Fortran set.
Any graphic character supported by the processor is permitted.
The apostrophes and quotation marks serve as delimiters, and are
not part of the value of the constant.
Example: the value of the constant ‘STRING’ is STRING
In character constants the blank character is significant, for instance
‘a string’ is same as a string.
Delimiter characters of one sort may be embedded in a string
delimited by the other,
Example: ‘He said “Hello” ’ ›››› He said “Hello”
“This contains an’ ” ›››› This contains an’
19
Introduction to Computational Physics – Solomon N.
Character
Doubled delimiter without intervening blanks is regarded as a
single character of the constant.
Example: ‘Isn’’t it a nice day’ ›››› Isn’t it a nice day
The number of characters in a string is called its length, and may be
zero.
Example: ‘’ and “” are character constants of length zero
Character constants that are written on more than one line
Example: long_string = &
‘The quick brown fox &
& jumps over the &
& lazy dog.’ 20
Introduction to Computational Physics – Solomon N.
Derived data type
Used to manipulate objects that are more sophisticated than those of the
intrinsic types.
Let's consider the problem of maintaining student records.
TYPE First_year
CHARACTER (20) :: Name ! includes initials, etc.
CHARACTER (20), DIMENSION(4) :: Address ! 4 lines for address Definition of
CHARACTER (10) :: Telephone the derived
CHARACTER (9) :: RegNo type
LOGICAL :: gender ! .TRUE. for female, .FALSE. for male First_year
INTEGER :: BirthDate ! e.g. 462012
REAL, DIMESION(20) :: Marks ! Marks
END TYPE

A variable of this type is declared as follows


type (First_year) :: Student
The components are referenced with the component selector (%). e.g.

Student % Birthdate = 462012 ›››› helps to access birth date


21
Introduction to Computational Physics – Solomon N.
Derived data type
TYPE Student_Type
Derived types have literal constants, e.g. CHARACTER (20) NAME
REAL Mark
Student_Type ( "Smith, JR", 49 ) END TYPE
The order of components must follow their type (Student_Type) :: you
order in the definition.
It can be assigned to a variable of the same type:
you = Student_Type ( "Smith, JR", 49 )
Derived data type may have a component that is a previously defined derived
data type
type point
REAL :: x,y,z
End type point

type triangle
type (point) :: a,b,c t has components: t%a, t%b, t%c all of type
point, and
End type triangle t%a has components: t%a%x, t%a%y, t%a%z
type (triangle) :: t of type real 22
Introduction to Computational Physics – Solomon N.
Array
Consists of a rectangular set of elements, all of the same type and type
parameters – used to refer a set of group item by a single name
Fixed and dynamical

Fixed array Example: To declare an array named a of 10


Real, dimension (size) :: vara real elements: Real,dimension(10):: a
– Elements of the array are: a(1),a(2)...a(10)
Integer, dimension (size) :: vara

It is possible in Fortran to declare a lower as well as an upper bound:

Real,dimension(-10:5):: vector

This is a vector of 16 elements, vector(-10), vector(-9), ..., vector(5).

We always need to specify the upper bound, the lower bound is optional, and
by default has the value 1.
Fortran allows up to seven dimensions to be specified. 23
Introduction to Computational Physics – Solomon N.
Array
Ex 1. real, dimension(5,4) :: b ››› declares an array with two dimensions,
2. real, dimension(-10:5, -20:-l, 0:1, -1:0, 2, 2, 2)::grid ››› declares seven
dimensions
The size of this second array is:
Arrays of many dimensions can thus place large demands on the memory of
a computer.
The number of dimensions of an array is called rank. Thus grid has a rank of
seven.
A derived type may contain an array component.
Type name
Real, dimension (size) :: vara
End type name
24
Introduction to Computational Physics – Solomon N.
Array
Some statements treat the elements of an array one-by-one in a special
order which we call the array element order.
It is obtained by counting most rapidly in the early dimensions.
The elements of grid in array element order are
grid(-10, -20, 0, -1, 1, 1, 1)
...
grid( -9, -20, 0, -1, 1, 1, 1) The ordering of elements in the array b(5,4).
grid( 5, -1, 1, 0, 2, 2, 2).
This is illustrated for an array of
two dimensions (see fig.)
We reference the element of an
array by specifying its subscript
values.

25
Introduction to Computational Physics – Solomon N.
Array
In general, a subscript may be formed of a scalar integer expression
Example: a(1)
a(i*j) ! i and j are of type integer
a(nint(x+3.)) ! x is of type real

In addition subarrays, called sections, may be referenced by specifying a


range for one or more subscripts.
Example: Rank and size of an array, examples of array sections
a(i:j) ! rank one and array of size j – i + 1
b(1:n) ! rank one of array size n
c(l:i, l:j) ! rank-two array with extents i and j

In Fortran, a rank-one array may be constructed as a list of elements


enclosed between the tokens (/ and /). Example: (/1, 2, 3, 5, 10 /) ››› an
array of rank one and size five, (/1, 2, 3, 4, 5/) is same as (/ (i, i=1, 5) /).
26
Introduction to Computational Physics – Solomon N.
Array
Character – It is possible to build arrays of characters,
Character, dimension (80) :: line
Declares an array, called line, of 80 elements, each one character in
length.
To reference a character a more appropriate declaration is
Character (len=80) :: line
These can be referenced individually or in groups using a substring
notation
line(i:j) ! references all the characters from i to j in 1ine.
Referencing a single character requires 1ine(i:i).
If the lower one is omitted, the value 1 is assumed; if the upper one is
omitted, a value corresponding to the character length is assumed.
line(:i) is equivalent to line(l:i)
line(i:) is equivalent to line(i:80)
line(:) is equivalent to line or line(l:80)
27
Introduction to Computational Physics – Solomon N.
Array
If i is greater than j in line(i: j), the value is a zero-sized string.
Now we can combine the length declaration with the array declaration
to build arrays of character objects of specified length:
Character (len=80), dimension (60) :: page
which might be used to store the characters of a whole page, with 60
elements of an array, each of length 80.
Page(j)(i:i) ! to reference line j and character i in that line
Dynamical array (allocatable)
PROGRAM program_name
REAL, ALLOCATABLE, DIMENSION(:) :: mass
ALLOCATE (mass(size))

DEALLOCATE (mass)
END PROGRAM 28
Introduction to Computational Physics – Solomon N.
Input/Output statement
Input
Read*, list
Read (unit, format) list
Output
Print*, list
Write (unit, format) list Example: 10 format(i10, fl0.3, al0)
Format – To print the scalar variables j, b, and c, of
types integer, real, and character, respectively.
NX skip N-character
print '(i10, f10.3, a10)', j, b, c or
AN N-strings print 10, j,b,c
NI N-integer 10 format(i10, fl0.3, al0)
FN.M N-character with M-decimal points
EN.M N-characters and M-decimal place/s
Print/Read 10, data
10 FORMAT( )
29
Introduction to Computational Physics – Solomon N.
Processing file in FORTRAN
Open(unit =”number”, Form=”formated”, file = “file_name”, status=”new”,
iostat =”ios”)
UNIT – integer constatnt, variable or expression (MUST!!)
FORM – formatted or unformatted (optional!!)
FILE – file name
STATUS – old, new, replace, scratch or unknown (default unknown)
IOSTAT – integer variable name (optional! 0 if currently opened)

PROGRAM program_name
Implicit none
[declaration statements]
open(unit =”number”, Form=”formatted”, file = “file_name”, status=”new”, &
iostat =”ios”)

CLOSE (unit = number)

END PROGRAM program_name


30
Introduction to Computational Physics – Solomon N.
Logical operator
FORTRAN has the following relational and logical operators

Operator Meaning Type


== Equal Relational
/= Not equal Relational
>= Greater than or equal Relational
<= Less than or equal Relational
< Less than Relational
> Greater than Relational
.AND. And Logical
.OR. or Logical
.NOT. NOT Logical

31
Introduction to Computational Physics – Solomon N.
Control structure
BLOCK IF statements
if (a .lt. b) then
a) IF (logical expression) statement
sum = sum + a
if (sum > 0.) print*, sum
b) IF (logical expression) THEN end if
STATEMENT
ENDIF

c) IF (logical expression1) THEN


if (hrs .le. 40.0) then
STATEMENT1 a = hrs * 150.0
ELSEIF (logical expression1) THEN elseif (hrs .le. 50.) then
a = (hrs - 40.0) * 150.0 * 1.5
STATEMENT2 else
ELSE a = (hrs - 50.0) * 150.0 * 2.0
STATEMENT3 end if

ENDIF
32
Introduction to Computational Physics – Solomon N.
CASE statement
The CASE statement provides a very clear and expressive selection mechanism
b/n two or more courses of action.
It could be programmed entirely with IF statement, but with controllable loss of
clarity.
A general form of CASE is
SELECT CASE (expr) select case (ch) ! ch of type character
CASE (selector1) case (‘c’, ‘d’, ‘r’:)
ch_type = .true.
block1 case (‘i’:’n’)
CASE (selector2) int_type = .true.
block2 case default
real_type = .true.
[CASE DEFAULT end select
blockD]
END SELECT
where (expr) must be integer, character or logical
33
Introduction to Computational Physics – Solomon N.
DO statements
DO variable = start, end, increment
Block of statements
END DO

Example
Program iteration_do
Integer:: i, sum, n
...
! assignment of n
sum = 0
do i = 1, n, 2
sum = sum + i
end do
...
end program iteration_do
34
Introduction to Computational Physics – Solomon N.
DO statements
DO WHILE (logical expression)
Block of statements
END DO

Example: Summation of the series until the


general term is less than ε times the current partial times the current partial sum:
Program iteration_while
Integer :: n
double precision :: term, sum
double precision, parameter :: epsilon = 1.d-3
Logical :: done
! Initialization
n = 0; sum = 0.d0; done = .false.
do while (.not.done)
n=n+1
term = 1.d0/n**2
sum = sum + term
done = (term .lt. epsilon * sum)
end do
print*, “Iteration number :” , n
print*, “Sum =”, sum
end program iteration_while 35
Introduction to Computational Physics – Solomon N.
DO statements
DO
Block of statements
IF (logical expression)EXIT
END DO

Example:
Program iteration_exit
Real :: value
Real :: x, xlast
real, parameter :: tolerance = 1.0e-6
Value = 50.
x = 1.0 ! initial value (diff. 0)
do
xlast = x
x = 0.5 * (xlast + value/xlast)
if (abs(x – xlast)/x < tolerance) exit
end do
end program iteration_exit 36
Introduction to Computational Physics – Solomon N.
FUNCTION
Advanced programs can be structured by means of procedures or subprograms
FORTRAN 90 enables us to implement subprograms as functions and
subroutines
Subprograms may be internal or external.
We use function:
– To break a problem down into parts, giving us the opportunity to structure
our problem solution
– To avoid the replication of the same or very similar sections of the code
– To build up a library of functions or modules for solving particular sub-
problems
Intrinsic or user defined
It take parameter or argument
Parameters can be an expression
It will normally return a value
It can sometimes take arguments of variety of types
37
Introduction to Computational Physics – Solomon N.
Function
Structure of a function in a program

PROGRAM program_name
IMPLICIT NONE
Real/Integer :: function_name
Real/Integer :: vara1
,
Vara1 = function_name
,
END PROGRAM program_name

Real/Integer FUNCTION :: function_name ()


IMPLICIT NONE
,
function_name = …
,
END FUNCTION function_name
38
Introduction to Computational Physics – Solomon N.
SUBROUTINE
Break problems down into simpler more easily solvable sub-problems
Avoids duplication of code
Hiding away messy code so that a main program is a sequence of calls to
procedures
Allowing us to call procedures from libraries written, tested and documented by
experts
A subroutine is defined as
SUBROUTINE subroutine_name(optional list of dummy arguments)
IMPLICIT NONE
Dummy argument type definitions with INTENT

END SUBROUTINE subroutine_name
Example:
SUBROUTINE interact(A,B,C,OK)
IMPLICIT NONE
Real, INTENT(out) :: A,B,C
Logical, INTENT(out) :: OK
END SUBROUTINE interact 39
Introduction to Computational Physics – Solomon N.
Subroutine
Referencing a subroutine
– To reference subroutine you use the CALL statement
– CALL subroutine_name(optional list of actual arguments)
– Example the call to subroutine interact was of the form:

CALL interact(P, Q, R, OK)

Dummy arguments or parameters and actual arguments


Intent

– INTENT(IN), where the parameter already has a value and cannot be


altered in the called routine
– INTENT(OUT), where the parameter does not have a value, and is given
one in the called routine
– INTENT(INOUT), where the parameter already has a value and this is
changed in the called routine
40
Introduction to Computational Physics – Solomon N.
Subroutine
Local variables

– Usually created when a procedure is called and their value lost when
execution returns to the calling program unit

Fortran allows the computer system being used to ‘forget’ a new value, the
unless it has the save attribute.

SAVE attribute
– for the local variable to retain its values between calls to a subprogram the
SAVE attribute can be used on a type statement

Example:

INTEGER, SAVE :: I

Functions and subroutines are very similar except a function returns a single
value, whereas a subroutine returns several results through its arguments.
41
Introduction to Computational Physics – Solomon N.
MODULES
Use of module
– Help programmers to split their codes into small modules
– Consistent definition of precision throughout a program and subprograms
– Use global data, Sharing arrays of data
– For derived data types
The form of a module is
Program main
MODULE module_name USE module_name
… Implicit none
Real :: vara1, vara2
END MODULE module_name
To access module …

USE module_name End program main

The USE statement must be the first statement after the PROGRAM or
SUBROUTINE or FUNCTION statement.
42
Introduction to Computational Physics – Solomon N.
MODULES
module constants
Example: implicit none
real, parameter :: pi = 3.1415926536
real, parameter :: e = 2.7182818285
contains
subroutine show_consts()
print*, "Pi = ", pi
print*, "e = ", e
end subroutine show_consts
end module constants

program module_example
use constants
implicit none
real :: x, ePowerx, area, radius
X = 2.0; radius = 7.0
ePowerx = e ** x
area = pi * radius**2
call show_consts()
print*, "e raised to the power of 2 = ", ePowerx
print*, "Area of a circle with radius 7= ", area
end program module_example 43
Introduction to Computational Physics – Solomon N.
2. Errors and Uncertainties in Computations

Errors and uncertainties are a part of computation.

Sources of errors are:

– Humans

– Computers

Arise from limited precision with
computers to store numbers or

B/c algorithms or models can fail
Types of Errors
On June 4, 1996, an Ariane 5 rocket launched by the European Space
Four general types of errors exist to Agency exploded just forty seconds after its lift-off from Kourou, French
Guiana. The rocket was on its first voyage, after a decade of development

plague your computations: costing $7 billion. The problem was a software error (overflow) in the
inertial reference system. Specifically a 64 bit floating point number
relating to the horizontal velocity of the rocket with respect to the platform
was converted to a 16 bit signed integer. Because the floating point value
I) Blunders or bad theory: was too large to be represented by a 16-bit signed integer.

– typographical errors entered with your program or data


44
Introduction to Computational Physics – Solomon N.
– running the wrong program or having a fault in your reasoning (theory)

– using the wrong data file, and so on.

II) Random errors

– Imprecision caused by events such as fluctuations in electronics, cosmic


rays, or someone pulling a plug

III) Approximation errors

– Imprecision arising from simplifying the mathematics so that a problem


can be solved on the computer

They include the replacement of:



Infinite series by finite sums

Infinitesimal intervals by finite ones

Variable by constants
45
Introduction to Computational Physics – Solomon N.
Example: (exact)

(algorithm)

where is approximation error from to

It is also called algorithm error


The approximation error decreases as N increases and vanish as

In the above Eq. the scale for N is set by the value of x

A small approximation error requires

So if x and N are close in value, the approximation error will be large.

IV) Round-off errors


– Imprecision arising from the finite number of digits used to store
floating point numbers
46
Introduction to Computational Physics – Solomon N.
– Analogous to the uncertainty in the measurement of a physical quantity
encountered in physics laboratory

– Round-off error accumulates as the computer handles more numbers

– As the number of steps in a computation increases ›››› some algorithms


become unstable ›››› rapid increase in error.

Example: if your computer kept four decimal places, then

stored as 0.3333 and as 0.6667

If we ask the computer to calculate

Imagine repeating this type of calculation millions of times !!

47
Introduction to Computational Physics – Solomon N.
Subtractive Cancellation
A calculation using numbers that are stored only approximately on the
computer can be expected to yield only an approximate answer.

To show this, we model the computer representation of the exact


numbers as:

Here is the relative error in , we expect (machine precision).

Applying this to

= 0 or /= 0

The resulting error – a weighted average of the errors in b and c

48
Introduction to Computational Physics – Solomon N.
Of special importance here the error in the answer increases when we
subtract two nearly equal numbers

B/c we are subtracting off the most significant parts of both numbers

B/c we cannot assume any sign for the errors, we must assume the worst (the
“max” in the above Eq.)

Example: solution of

If , so , due to finite machine precision, computer gives


instead of

49
Introduction to Computational Physics – Solomon N.
Round-off Error in a Single Step
Let’s start by seeing how error arises from a single division of the computer
representations of two numbers:

(Ignoring very
small)
We can generalize this error estimation in the evaluation a function

For

50
Introduction to Computational Physics – Solomon N.
Round-off Error Accumulation After Many Steps
There is a useful model for approximating how round-off error accumulates in a
calculation involving a large number of steps.

In a random walk – a walk for which each step is in a random direction.


R N
Total distance covered in N steps of length r, is on average
1 2
3
4
By analogy, the total relative error arising after N steps each with machine
precision error is:

In a particular algorithm round-off errors may need detailed analysis to predict


the dependence of the error on N-steps.
In some cases there may be no cancellation, and the error may increase as
Even worse, in some recursive algorithms (Bessel functions), error increase as
51
Introduction to Computational Physics – Solomon N.
Errors in Algorithms
Algorithms play a vital role in computational physics.

In a given problem we take a general algorithm and decide

1. Does it converge?

2. How precise are the converged results?

3. How expensive (time-consuming) is it?

On first thought we may think, all algorithms converge if enough terms are used,
and if you want more precision, then use more terms.

But, some algorithms may be asymptotic expansions that converge only up to a


point.

Even for uniformly convergent power series, including more terms:

– will decrease the algorithmic error

– increase the round-off errors 52


Introduction to Computational Physics – Solomon N.
B/c round-off errors eventually diverge to infinity, the best we can hope for is a
“best” approximation.

Good algorithms–are fast & round-off error does not have much time to grow.

Let us assume that an algorithm takes N steps to find a good answer.

As a rule of thumb, the approximation (algorithmic) error decreases rapidly


often as:

Here α and β are empirical constants – may change for different algorithms, may
be approximately constant as

As N increases algorithm error decrease and round-off error tends to grow


slowly and somewhat randomly.

If we model the accumulation of error as a random walk the round-off error is :

53
Introduction to Computational Physics – Solomon N.
The total error in a computation would be the sum of the two

For small N the first term is larger but ultimately to be overcome by the slowly
growing round-off error.

Example: Fig. a log-log plot of the relative error in numerical integration using
the Simpson rule

Let us assume A is the exact answer &


and A(N) the computed answer, if

The error show a rapid decrease for small N


tells us the number of decimal
places of precision obtained
As N is increased, the error becomes erratic

The smallest total error will be obtained if we can stop the calculation at the
minimum near 54
Introduction to Computational Physics – Solomon N.
Minimizing the Error
In order to see more clearly how different kinds of errors enter into a
computation, let us examine a case and are known

The total error is then a minimum when

For a single-precision calculation , the minimum total error occurs


40 times machine
precision

55
Introduction to Computational Physics – Solomon N.
Most of the error is due to round-off, the double precision results are better.
We can decrease the total error by decrease round-off error using a smaller
number of steps N.
Let us assume another algorithm that converges more rapidly with N

The total error is now

The total error is then a minimum when

The error is now smaller by a factor of 4 with only 1/16 as many steps needed
In this case the better algorithm is quicker and, by using fewer steps, it produces
less round-off error

Error Assessment (to be discussed at the end of chapter 5)


56
Introduction to Computational Physics – Solomon N.
3. Roots of an Equation

In physics, we often need to find the value of x that makes,

– can be an explicit or an implicit function of x.

If such a value exists, we call it a root or zero of the equation.

Bisection Method
If a root exists in the region for , we can use the bisection
method – simple method to code
y
B/c there is a root in the region, f (x )
We divide the region into two equal parts

x

Then we have either or a b


a b
xo 
2
57
Introduction to Computational Physics – Solomon N.
If , the solution lies in the region:

and

We calculate the new value

If , the solution lies in the region:

and

We calculate new value

We continue to halve the interval till we reach a value which fulfills


to a given numerical precision or tolerance

58
Introduction to Computational Physics – Solomon N.
Newton-Raphson’s Method
This method is based on linear approximation of a smooth function around its
root.

To appreciate the power of Taylor's series, we'll use it to develop Newton's


method to find the zeros of a function.

We can expand the function in the neighborhood of the root


through the Taylor expansion.

Here x can be viewed as a trial value for the root at the ith step

The approximate value of the next step can be derived from

, with
59
Introduction to Computational Physics – Solomon N.
Here we have used the notation

The idea of using one value to generate a better value is called iteration

This iterative scheme is called the Newton-Raphson method or Newton method.

The above Eq. is equivalent to approximating the root by drawing a tangent to


the curve at the point and taking as the tangent’s intercept on the x-
axis.

The step is repeated toward the


root (see Fig), and converges fast
to the desired result.
Example: for

60
Introduction to Computational Physics – Solomon N.
So we might guess that: , somewhere around
We then calculate the zero to be at:

Much closer to the correct answer of 0.739085133 than the initial guess

This result can be used as a new guess to calculate another approximation to


the location of the zero.

a result accurate to 7 significant digits.


Typically we want a result that is accurate to about eight significant digits, e.g., a
relative error of
61
Introduction to Computational Physics – Solomon N.
Secant Method
When has an implicit dependence on x, an analytic expression for the first
derivative in Newton method may not exist or may be very difficult to obtain.

In this case we have to find an alternative method


To do this we replace in Newton method with a two-point formula for the
1st – order derivative:

Using the iterative expression of Newton-Raphson’s

secant method, or
discrete Newton method

The disadvantage of the method is that we need two points in order to start the
search process. 62
Introduction to Computational Physics – Solomon N.
We are drawing a straight line from the point to

Where it crosses the we have the new point

The secant method is more efficient than the bisection method but less

efficient than the Newton-Raphson method


63
Introduction to Computational Physics – Solomon N.
4. Numerical Differentiation

Numerical integration and differentiation are some of the most frequently

needed methods in computational physics.

Need to evaluate either or an integral

This chapter deals essentially with numerical differentiation.

There are three methods for numerical differentiation

– Forward difference

– Central difference

– Extrapolated difference
64
Introduction to Computational Physics – Solomon N.
Forward Difference
The most direct method for numerical differentiation of a function starts by
expanding it in a Taylor series.
This series advances the function one small step forward:

where is the step size

Solving for we obtain the forward-difference


derivative algorithm
Forward (1)
difference

An approximation for the error follows from substituting the Taylor series:

We can think of this approximation as using two points to represent the


function by a straight line in the interval from and . (see Fig. above) 65
Introduction to Computational Physics – Solomon N.
The approximation (1) has an error proportional to h (unless vanishes)

We can make the approximation error smaller by making h smaller

Yet precision will be lost through the subtractive cancellation on the left-hand
side (LHS) of (1) for too small an h.

For example: consider a case where is a simple, analytic polynomial:

The exact derivative is , while the computed derivative is

This clearly becomes a good approximation only for small

66
Introduction to Computational Physics – Solomon N.
Central Difference
An improved approximation to the derivative starts with the basic definition

Rather than making a single step of h forward, we form a central difference


by stepping forward by and backward by (see fig. below)

(2)

Central difference

We estimate the error in the central-difference


algorithm by substituting the Taylor series for
into (2)

67
Introduction to Computational Physics – Solomon N.
The central-difference algorithm accurate to order ( before division by
h), while the forward difference is accurate only to order h.

If the is smooth, that is, if

We can expect the central-difference error to be smaller.

For , the central difference gives the exact derivative independent


of h:

68
Introduction to Computational Physics – Solomon N.
Extrapolated Difference
We can reduce the theoretical error further by combining algorithms whose
sums extrapolate to zero

One algorithm is the central difference using a half step back and a half step
forward
The second algorithm is another central-difference approximation using
quarter-steps:

The combination of the two eliminates both the quadratic and the linear error
terms:
For half step:

Introduction to Computational Physics – Solomon N.


* 69
For quarter step:

**

(3)

A good way of computing (3) is

Extrapolated difference 70
Introduction to Computational Physics – Solomon N.

You might also like