Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Recipes The Art of Scientific Computing (with applications in computational physics) A level 5000 course based on “Numerical Recipes in C, the.

Similar presentations


Presentation on theme: "Numerical Recipes The Art of Scientific Computing (with applications in computational physics) A level 5000 course based on “Numerical Recipes in C, the."— Presentation transcript:

1 Numerical Recipes The Art of Scientific Computing (with applications in computational physics) A level 5000 course based on “Numerical Recipes in C, the art of scientific computing”, 2nd edition, by W H Press, S A Teukolsky, W T Vetterling, and B P Flannery. A soft copies of the slides are available at

2 Computer Architecture
CPU Memory For a more extensive discussion, see standard textbooks, e.g., D A Patterson & J L Hennessy, “Computer Organization & Design”. von Neumann architecture: both data and instructions are stored in memory. External Storage

3 Program Organization int main() { … } double func(double x) {
There are many books on C, e.g., B W Kernighan and D M Ritchie, “the C Programming Language”, 2nd. edition. Or S. P. Harbison and G. L. Steele, Jr. “C: A Reference Manual” 5th ed.

4 First Example #include <stdio.h> main() {
printf(”hello, world\n”); } gcc hello.c (to get a.out) [Or other way depending on your OS] There is really only one type of quote “ in C.

5 Data Types #include <stdio.h> main() { int i,j,k; double a,b,f; char c, str[100]; j = 3; a = 1.05; c = ’a’; str[0] = ’p’; str[1] = ’c’; str[2] = ’\0’; printf(”j=%d, a=%10.6f, c=%c, str=%s\n”, j, a, c, str); }

6 Equal is not equal x = 10 is not 10 = x
x = x + 1 made no sense if it is math x = a + b OK, but a+b = x is not C. In general, left side of ‘=’ refers to memory location, right side expression can be evaluated to numerical values

7 Expressions Expressions can be formed with +, -, *, / with the usual meaning Use parentheses ( …) if meaning is not clear, e.g., (a+b)*c Be careful 2/3 is 0, not 0.666…. Large number of operators exists in C, such as ++i, --j, a+=b, &, !, &&, ||, ^, ?a:b, etc

8 Use a Clear Style (A) k=(2-j)*(1+3*j)/2; k=j+1; if(k == 3) k=0;
switch(j) { case 0: k=1; break; case 1: k=2; break; case 2: k=0; break; default: { fprintf(stderr, ”unexpected value for j”); exit(1); } (D) k=(j+1)%3; (B) (C) The four pieces of code (A), (B), (C), and (D) do the same thing, which one is preferred?

9 Control Structures in C - loop
for (j=0; j < 10; ++j) { a[j] = j; } while (n < 1000) { n *= 2; do { } while (n < 1000);

10 Control Structure - conditional
if (b > 3) { a = 1; } if (n < 1000) { n *= 2; } else { n = 0;

11 Control Structure - break
for( ; ; ) { ... if(. . .) break; }

12 Pointers Pointer is a variable in C that stores address of certain type int *p; double *ap; char *str; You make it pointing to something by (1) address operator &, e.g. p = &j, (2) malloc() function, (3) or assignment, str = ”abcd” Get the value the pointer is pointing to by dereferencing, *p

13 1D Array in C int a[4]; defines elements a[0],a[1],a[2], and a[3]
a[j] is same as *(a+j), a has a pointer value double b[4], *bb; bb=b-1; then valid range of index for b is from 0 to 3, but bb is 1 to 4. The compiler cannot check if the array is out-of-bound. Fortran is much better in array processing.

14 1D Array Argument Passing
void routine(double bb[], int n) // bb[1..n] (range is 1 to n) We can use as double a[4]; routine(a-1, 4);

15 2D Array in C double m[3][5]; defines fixed size array. Example below defines dynamic 2D array. double **m; m = (double **) malloc(3*sizeof(double *)); for(i=0; i<3; ++i) { m[i] = (double *)malloc(5*sizeof(double)); } Fortran is much better than C in handling multi-dimensional arrays.

16 Representation of 2D Array, fixed size vs dynamic

17 Special Treatment of Array in NR
float *vector(long nl, long nh) allocate a float vector with index [nl..nh] float **matrix(long nrl, long nrh, long ncl, long nch) allocate a 2D matrix with range [nrl..nrh] by [ncl..nch]

18 Header File in NR #include ”nr.h” #include ”nrutil.h”
Distinction of file names in <….> and in “ …” The use of the NR header files are discouraged for this course.

19 Precedence and Association

20 Pre/post Increment, Address of
Consider f(++i) vs f(i++), what is the difference? &a vs *a Conditional expression x = (a < b) ? c : d;

21 Macros in C #define DEBUG #define PI 3.141592653
#define SQR(x) ((x)*(x)) Why need so many parentheses in the last example? Avoid the 3rd type of macro whenever possible.

22 Computer Representation of Numbers
Unsigned or two’s complement integers (e.g., char) = 0 = 1 = 2 = 3 = 4 = 5 = 6 . . . = 127 = or = or = or = or . . . = or -4 = or -3 = or -2 = or -1 Two’s complement is completely equivalent to arithmetic modulo M, so i and i-M are equivalent, e.g., M=2^8=256 in the above.

23 Real Numbers on Computer
Example for β=2, p=3, emin= -1, emax=2 ε Normalized number is d0 > 0. (some books defines machine epsilon as half of the above). ε is called machine epsilon.

24 Floating Point, sMBe-E, not IEEE
Not a IEEE representation.

25 IEEE 754 Standard (32-bit) The bit pattern represents
If e = 0: (-1)s f 2-126 If 0<e<255: (-1)s (1+f) 2e-127 If e=255 and f = 0: +∞ or -∞ and f ≠ 0: NaN … … s b-1 b-2 b-23 e f = b b … + b See also the code testieee.c at The latest standard is IEEE

26 Error in Numerical Computation
Integer overflow Round off error E.g., adding a big number with a small number, subtracting two nearby numbers, etc How does round off error accumulate? Truncation error (i.e. discretization error) The field of numerical analysis is to control truncation error Catastrophic vs benign cancellation.

27 (Machine) Accuracy Limited range for integers (char, int, long int, and long long int) Limited precision in floating point. We define machine ε as such that the next representable floating point number is (1 + ε) after 1. ε  for float (32-bit) and  for double (64-bit) When a real number is rounded to the closest floating-point number, the relative error is always bounded by . Size of integers: char 1 byte, int, long int 4 bytes, long long it 8 bytes. 1 byte = 8 bits.

28 Stability An example of computing Φn where We can compute either by
Φn+1 = Φn Φ or Φn+1 = Φn-1 – Φn Results are shown in stability.c program The code is in stability.c

29 Reading Materials “Numerical Recipes”, Chap 1.
“What every computer scientist should know about floating-point arithmetic”. Can be downloaded from “The C Programming Language”, Kernighan & Ritchie

30 1. (a) An array is declared as
Problems for Lecture 1 (C programming, representation of numbers in computer, error, accuracy and stability, assignment to be handed in next week) 1. (a) An array is declared as char *s[4] = {”this”, ”that”, ”we”, ”!”}; What is the value of s[0][0], s[0][4], and s[2][1]? (b) If the above array is to be passed to a function, how should it be used, i.e., the declaration of the function, and use of the function in calling program? If the array is declared as char t[4][5]; instead, then how should it be passed to a function? 2. (a) Study the IEEE 754 standard floating point representation for 32-bit single precision numbers (float in C) and write out the bit-pattern for the float numbers 0.0, 1.0, 0.1, and 1/3. (b) For the single precision floating point representation (32-bit number), what is the exact value of machine epsilon? What is the largest positive number? 3. For the recursion relation: F n+1 =Fn-1 – Fn with F0 and F1 arbitrary, find the general solution Fn. Based on its solution, discuss why is it unstable for computing the power of golden mean Φ? (Hint: consider trial solution of the form Fn = Arn ). For bit patterns, instead of writing as 0 and 1, use hex notation, i.e., 0 to 9 uses decimal, 10 to 15 uses A, B, C, D, E, F.


Download ppt "Numerical Recipes The Art of Scientific Computing (with applications in computational physics) A level 5000 course based on “Numerical Recipes in C, the."

Similar presentations


Ads by Google