Download presentation

Presentation is loading. Please wait.

Published byJessie Factor Modified over 3 years ago

1
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)

2
Computer Architecture CPUMemory External Storage

3
Program Organization int main() { … } double func(double x) { … }

4
First Example #include main() { printf(hello, world\n); } gcc hello.c (to get a.out) [Or other way depending on your OS]

5
Data Types #include 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 can be evaluated to numerical values

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

8
Use a Clear Style 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); } (A) (B) (C) (D) k=(j+1)%3;

9
Control Structures in C - loop for (j=0; j < 10; ++j) { a[j] = j; } while (n < 1000) { n *= 2; } do { n *= 2; } 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. Use 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 float b[4], *bb; bb=b-1 ; then valid range of index for b is from 0 to 3, but bb is 1 to 4.

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

15
2D Array in C int m[13][4]; defines fixed size array. Example below defines dynamic 2D array. float **a; a = (float **) malloc(13*sizeof(float *)); for(i=0; i<13; ++i) { a[i] = (float *)malloc(4*sizeof(float)); }

16
Representation of 2D Array

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

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))

22
Computer Representation of Numbers Unsigned or twos complement integers (e.g., char ) 0000 0000 = 0 0000 0001 = 1 0000 0010 = 2 0000 0011 = 3 0000 0100 = 4 0000 0101 = 5 0000 0110 = 6... 0111 1111 = 127 1000 0000 = 128 or -128 1000 0001 = 129 or -127 1000 0010 = 130 or -126 1000 0011 = 131 or -125... 1111 1100 = 252 or -4 1111 1101 = 253 or -3 1111 1110 = 254 or -2 1111 1111 = 255 or -1

23
Real Numbers on Computer 0 0.5 1 2 3 4 5 6 7 ε Example for β=2, p=3, e min = -1, e max =2 ε is called machine epsilon.

24
Floating Point, s M B e-E, not IEEE

25
IEEE 754 Standard (32-bit) The bit pattern represents If e = 0: (-1) s f 2 -126 If 0

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

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. ε 10 -7 for float (32-bit) and 10 -15 for double (64-bit)

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 a simple program

29
Reading Materials Numerical Recipes, Chap 1. What every computer scientist should know about floating-point arithmetic. Can be downloaded from http://www.validlab.com/goldberg/paper.ps The C Programming Language, Kernighan & Ritchie

30
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 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 precise value of machine epsilon? What is the smallest possible number and largest possible number? 3. For the recursion relation: F n+1 =F n-1 – F n with F 0 and F 1 arbitrary, find the general solution F n. Based on its solution, discuss why is it unstable for computing the power of golden mean Φ? (Hint: consider solution of the form F n = Ar n ).

Similar presentations

OK

1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.

1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on low level language goals Ppt on technical topics related to computer science Ppt on question tags Ppt on social contract theory and criminal justice Download ppt on working of human eye Ppt on formal education quotes Ppt on ice cream industry Ppt on business cycle theories Ppt on chapter 12 electricity calculator Ppt on five generation of computers