You are on page 1of 322

# Computational Physics

2

3

4

5

6

7

1 INTRODUCTION

### The sources which I have consulted most frequently whilst developing course material are as follows:

C/C++ PROGRAMMING:

### Schaum’s outline: Programming with C++, 2nd edition, J.R. Hubbard (McGraw- Hill, New York NY, 2000).

NUMERICAL METHODS AND COMPUTATIONAL PHYSICS:

### Numerical methods for physics, 2nd edition, A.L. Garcia (Prentice-Hall, Upper Saddle River NJ, 2000).

8

1.3

Purpose of Course

1 INTRODUCTION

PHYSICS OF BASEBALL:

CHAOS:

### Basically, there are three possible methods by which we could perform the nu- merical calculations which we are going to encouter during this course.

9

1.5 Programming Methodologies

1 INTRODUCTION

### Thirdly, we could write our own programs—completely from scratch—in a high-level language. This is the approach used in this course. I have opted not to use pre-written subroutine libraries, simply because I want students to develop the ability to think for themselves about scientiﬁc programming and numerical techniques. Students should, however, realize that, in many cases, pre-written library routines offer solutions to numerical problems which are pretty hard to improve upon.

1 See http://www.wolfram.com 2 See http://www.maplesoft.com 3 See http://www.mathworks.com 4 See http://www.nag.com 5 See http://www.netlib.org ibid.

6

10

1.6

Scientiﬁc Programming Languages

1 INTRODUCTION

### C: This language was originally developed by computer scientists to write op- erating systems. Indeed, all UNIX operating systems are written in C. C is, consequently, an extremely ﬂexible and powerful language. Amongst its ma- jor advantages are its good control statements and excellent input/output facilities. C’s main disadvantage is that, since it was not speciﬁcally written to be a scientiﬁc language, some important scientiﬁc features (e.g., complex

11

1.6

Scientiﬁc Programming Languages

1 INTRODUCTION

### Of the above languages, we can immediately rule out C++, because object- orientation is an unnecessary complication (at least, for our purposes), and FOR- TRAN 90, because of the absence of an inexpensive compiler. The remaining options are FORTRAN 77 and C. I have chosen to use C (augmented by some of the useful, non-object-orientated features of C++) in this course, simply because I ﬁnd the archaic features of FORTRAN 77 too embarrassing to teach students in the 21st century.

12

2

SCIENTIFIC PROGRAMMING IN C

### Variable names in C can consist of letters and numbers in any order, except that the ﬁrst character must be a letter. Names are case sensitive, so upper- and lower- case letters are not interchangeable. The underscore character (_) can also be

13

2.2

Variables

2 SCIENTIFIC PROGRAMMING IN C

x

c14

area

electron_mass

TEMPERATURE

0

57

4567

128933

0.01 70.456

3e+5 .5067e-16

### Strings are mainly used in scientiﬁc programs for data input and output pur- poses. A string consists of any number of consecutive characters (including blanks) enclosed in double quotation marks: e.g.,

"red" "Austin TX, 78723" "512-926-1477"

### Line-feeds can be incorporated into strings via the escape sequence \n: e.g.,

"Line 1\nLine 2\nLine 3"

### The above string would be displayed on a computer terminal as

Line 1

Line 2

Line 3

14

2.3

Expressions and Statements

2 SCIENTIFIC PROGRAMMING IN C

### A declaration associates a group of variables with a speciﬁc data type. As mentioned previously, all variables must be declared before they can appear in executable statements. A declaration consists of a data type followed by one or more variable names, ending in a semicolon. For instance,

 int a, b, c; double acc, epsilon, t;

### A type declaration can also be used to assign initial values to variables. Some examples of how to do this are given below:

 int a = 3, b = 5; double factor = 1.2E-5;

### An expression represents a single data item—usually a number. The expression may consist of a single entity, such as a constant or variable, or it may consist of some combination of such entities, interconnected by one or more operators. Expressions can also represent logical conditions which are either true or false. However, in C, the conditions true and false are represented by the integer values 1 and 0, respectively. Several simple expressions are given below:

a + b x = y

15

2.3

Expressions and Statements

2 SCIENTIFIC PROGRAMMING IN C

t =

u + v

x

<= y

++j

j =

j

+ 1

a = 6;

c

= a + b;

++j;

### A compound statement consists of several individual statements enclosed within a pair of braces { }. The individual statements may themselves be expression

16

2.3

Expressions and Statements

2 SCIENTIFIC PROGRAMMING IN C

### statements, compound statements, or control statements. Unlike expression state- ments, compound statements do not end with semicolons. A typical compound statement is shown below:

 { pi = 3.141593; circumference = 2. * pi * radius; area = pi * radius * radius; }

#define

NAME

text

#define

PI 3.141593

### During the compilation process, the preprocessor replaces each occurrence of the symbolic constant PI by its corresponding text. Hence, the above statement becomes

17

2.4

Operators

2 SCIENTIFIC PROGRAMMING IN C

### There are four main arithmetic operators in C. These are:

 addition + subtraction - multiplication * division /

(double) j

### Finally, to avoid mixing data types when dividing a ﬂoating-point variable x by an integer variable i, we would write

18

2.4

Operators

2 SCIENTIFIC PROGRAMMING IN C

x / (double) i

a -

b / c

+ d

a -

(b / c) + d

### Note that there is a subtle distinction between the expressions a++ and ++a. In the former case, the value of the variable a is returned before it is incremented. In the latter case, the value of a is returned after incrementation. Thus,

b = a++;

19

2.4

Operators

2 SCIENTIFIC PROGRAMMING IN C

### is equivalent to

b = a; a = a + 1;

b = ++a;

### is equivalent to

a = a + 1; b = a;

### There are four relational operators in C. These are:

 less than < less than or equal to <= greater than > greater than or equal to >=

### Closely associated with the relational operators are the two equality operators:

 equal to == not equal to !=

### C also possess two logical operators. These are:

20

2.4

Operators

2 SCIENTIFIC PROGRAMMING IN C

 && and || or

i + j == 3 &&

i *

l >= 5

### and relying on the fact that arithmetic operators have precedence over relational and equality operators, which, in turn, have precedence over logical operators, it is better to write

((i + j) == 3) && (i * l >= 5)

### The most common assignment operator in C is =. For instance, the expression

f = 3.4

21

2.4

Operators

2 SCIENTIFIC PROGRAMMING IN C

i =

j

= k

= 4

• i += 6

• i -= 6

• i *= 6

• i /= 6

### Simple conditional operations can be carried out with the conditional opera- tor (? :). An expression that makes use of the conditional operator is called a conditional expression. Such an expression takes the general form

expression 1

?

expression 2

:

expression 3

22

2.4

Operators

2 SCIENTIFIC PROGRAMMING IN C

(j < 5) ?

12 : -6

k =

(i <

0) ?

n

:

m