Title: COMPUTER PROGRAMMING
1COMPUTER PROGRAMMING
- Course code CE10082
- Recommended text-books any book about
Fortran77/90 programming. - Course length (contact hours) 15 hours
- Method of assessment Computing project
Course outline In this course you will learn
basic programming by Fortran 77 and Fortran 90.
The course will be run on an informal basis, and
will essentially be a teach yourself process,
under guidance. You will be given a series of
worksheets and a project to be completed during
the course, which must be handed in on-time.
2Course Content
- Section 1
- Introduction of the programming concept, learning
about the computing resources and getting
familiar with logging into the system in order to
use Fortran 77 and Fortran 90, and compiling the
Fortran routines. - Section 2
- Learning how to write algorithms, preparing
flowcharts, and learning basic features and rules
of Fortran. - Section 3
- Learning how to use the Fortran language by
writing programs and running them. - Section 4
- Learning some of the numerical computing methods
to solve polynomial functions, integral and
differential equations, and writing Fortran
programs to do them.
3Why do we need to know how to program?
Not every problem a chemical engineer wishes to
solve has a ready-made PC package available that
can be bought off a shelf. E.g. computer
simulation of the growth of a silica film at an
air-water interface. Experimental observations-
time
Silica particles precipitate out at the water-air
interface
Individual particles aggregate into a fractal
cluster structure
4Computer Simulation
If we are able to write a computer program that
models the growth of the silica film we,
therefore, understand the system and can
experiment in-silico-
Image of real film
Simulated film
5The Programming Concept
- Computer programming is a bit like learning a
foreign language - you have to learn the
computers vocabulary and grammar (in the jargon
it is called syntax). - Unfortunately, unlike a human, a computer cannot
guess what you are trying to say and only goes on
just what you tell it. - If your vocabulary or grammar has even the
slightest mistake, it will not understand you. - The computer will follow the instructions that
you give it to the letter, even if you really
meant it to do something else ! - Therefore you will need to develop a way of
thinking that breaks down a task into smaller
components that you are able to write down very
precisely.
6Flow Diagrams and Programs
A program is a set of instructions telling the
computer to perform a set of operations in a
particular order. In order to convert a specific
task into a computer program it is often helpful
to construct a flow diagram for procedures
involved in the task. A flow diagram consists of
operations such as
Terminator
Yes
No
Decision e.g. xgt1 ?
Process e.g. x x1
7The Compilation Process The first step in testing
a program after it is written. The compiler
checks if the program is written correctly (the
syntax is correct). It wont spot mistakes that
are grammatically correct.
Compiler
Object code
Source code
Compilation of the code
Source code listing
8Writing Fortran Programs In principle you have
several options for where to write your FORTRAN
programs. You can use- 1) fbench.exe in the f77
folder on the G-drive 2) the vi editor, or
similar, on any UNIX computer, e.g. mary 3) some
PCs have other FORTRAN compilers (e.g. Force)
installed 4) UNIX workstations, such as SUNs and
SILICON GRAPHICS We will use option (1) or
(3). Your files will be stored in your home
directory. You can also keep copies on floppy.
9Basic UNIX commands
ls -list contents of current directory pwd -pr
esent working directory mkdir name -make new
directory (folder) rm name -remove file called
name rmdir name -remove directory called name cd
path/name -change directory to path/name cd
.. -go back to previous directory man
command -brings up on-line manual entry for
a particular command Program files must have a
name followed immediately by .f To compile a
program in file name.f type f77 -o name
name.f and to run the program type name
10Basic Programming
- Start program commands on at least column 7 (e.g.
use tab) of whatever text editor is used. - Do not exceed column 72 for each line of text.
- Start program with the name of the program
- PROGRAM NAME
- Next disengage any defaults for variable classes
- IMPLICIT NONE
- Text which is not a Fortran command must be
indicated by either a C or ! character in the
first column. - ! comments
- It is helpful to use comments to both remind
yourself, and tell any new reader of your
program, what each bit of the program does.
11Data Types and Integer Constants
- Next you must declare the names and types of all
the variables you intend to use in the program. - Integer values are those that represent whole
numbers only. The range of values possible
depends on the computer but is typically from
-232-1 to 232-1-1 for a typical machine. - The declaration statement is as follows
- INTEGER NAME
- The second main type of numerical variable is
called Real. Real numbers are stored in the
computer as two components a mantissa ranging
between 0.1 and 1.0, and an exponent that
indicates the appropriate power of 10. - E.g 0.5x105 0.5E5
12Data Types and Integer Constants
- Real numbers may be positive or negative, and
always possess a decimal point. - The limit of accuracy for Real variables is 7
digits, with magnitudes in the range from 10-39
to 1039. - Declaration statements are of the form
- REAL NAME
- If more than 7 digits accuracy is required you
can use a Double Precision constant, which is
accurate up to 14-16 decimal places, depending on
the machine. - Instead of using E to denote the exponent you
must use the letter D, e.g. 0.5D5 - Double precision numbers require longer
computational time to do calculations. - Declaration statements are of the form
- DOUBLE PRECISION NAME
13Simple Input/Output
- To input a value to a variable you use the Read
statement. - For example, to get the user to enter a value for
the variable VARIABLE1 use - READ, VARIABLE1
- When the computer reaches this command in a
program it will sit and wait for the user to
enter a value. - To display the value of a variable, or to print
some text we use the Print command - For example
- PRINT, VARIABLE 1
- will print the value of VARIABLE1 on the screen,
and - PRINT,text
- will print the text between the inverted commas
on the screen.
14Mathematical Operations
Note Using Reals 3.0/2.0 1.5 Using
Integers 3/2 1 Integer answer obtained
by truncation
15IFTHEN...ELSEENDIF
IF statements can be used to make
decisions e.g. IF(X.EQ.1) THEN -Note full
stops PRINT,HELLO ELSE
PRINT,GOODBYE ENDIF If you are asking more
than one question, or two questions about more
than one variable, you can use .AND. and .OR.
statements e.g. IF((X.EQ.1).AND.(Y.EQ.1))
THEN IF((X.EQ.1).OR.(Y.EQ.1)) THEN For
the logical parts of the statements EQ
equals NE not equal to GT greater
than GE greater than or equal to LT less
than LE less than or equal to
16DOENDDO
The DO statement is for carrying out loops. The
format of the statement is DO variablestart,
stop, step e.g. DO I1,10,1 The loop must be
finished with an ENDDO statement e.g. DO
I1,10,1 YYI ENDDO A slight variant is
the DO WHILE loop e.g. X1 Y1 DO WHILE
(X.LT.10) YYX XX1 ENDDO The DO
WHILE loop uses the same codes for less than
etc. as IF.
17FILE MANAGEMENT
Computer programs may be run in your absence.
You can get the computer to run a program
overnight and then output the data to a file
that you can pick up later, at your convenience.
You may also wish to use other peoples data in
your programs. To create a file separate to your
source code file use the following command OPEN
(UNIT1, FILEname,STATUSNEW) To add
something to the file use the WRITE
command e.g. WRITE(1,) NUMBER
18FILE MANAGEMENT
When you have finished using the file, close it
down using CLOSE(1) When you want to
retrieve data from an existing file, re-open
it OPEN(UNIT1, FILEname,STATUSOLD) and
read in the data contained therein
using READ(1,) NUMBER and dont forget to
close the file again, afterwards.
19PROGRAM VALIDATION
- When you have written a program it is important
to check, not only that it compiles and runs, but
also that it does the job it was designed to do!! - This process is known as program validation.
- If the program is designed to solve a class of
numerical problems, then use it to solve a
particular special case where the solution is
known analytically or can easily be done on a
spreadsheet, and then check the answer from your
program against the alternative method. - If the program works in the simple case, we can
have more confidence that it will work for a
complex case we cant tackle by simpler means.
20Trapezium Rule
y
y0
y1
y3
y(x)
h
h
x
x1
x2
x3
(I) Divide integration region into strips of
width h (II) Approximate curve over each strip by
a straight line (III) Sum areas of resulting
trapezia - n strips means (n1)
function evaluations
21Simpsons Rule
y
A
B
C
yi-1
yi
yi1
y(x)
h
h
x
xi-1
xi
xi1
(I) Divide integral region into an even number of
strips, each of width h (II) Approximate curve
over two adjacent strips by a quadratic- interpola
tion through A, B, C
(III) Find area underneath interpolating curve
22Simpsons Rule
(IV) Sum
Note (1) Simpsons rule is exact for a
quadratic, by construction (2) Simpsons rule is
exact for a cubic too !
23Solving Differential Equations using Eulers
Method
y
Slope f(x0,y0)
Slope f(x1,y1)
y(x)
Start here
y0
y1
y3
h
h
x
x1
x2
x3
Approximate solution by a set of straight lines,
where the slopes are given by the differential
equation
In general
Equivalent to a Taylor expansion truncated after
the 1st derivative
24Modified Euler Method
yn
h
xn
xn1
Over the step xn,xn1 Euler uses yn/ f(xn,yn)
i.e. the slope at the start of the step. To
improve this, estimate the derivative at the end
of the step too, and take the average of the two
estimates. Estimate of derivative at xn is k1
f(xn,yn) Estimate of derivative at xn1 is
yn1 yn h/2(k1 k2)
25Example - Solving a Quadratic Equation by
Iteration
- Problem Write a program to solve a general
quadratic equation by iteration. - The general quadratic equation is given by
- For an iterative solution we must re-arrange the
equation such that x alone occurs on one side of
the equation
26Example - Solving a Quadratic Equation by
Iteration
- To solve by iteration we first select our initial
guess for x. - Then we insert this value of x into the rhs of
the re-arranged quadratic to obtain our next
value of x - We repeat this process until (hopefully) x
converges to one of the solutions of the
quadratic.