25
Computational Physics Course 17094 Lecture 1 Hartmut Ruhl and Nina Elkina LMU, Theresienstrasse 37, Munich, Room A238 Topics Literature Topic today SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats SP in C: Funcs Computational Physics Course 17094 Lecture 1 Hartmut Ruhl and Nina Elkina LMU, Theresienstrasse 37, Munich, Room A238 Oct. 23, 2009

Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Computational Physics Course 17094Lecture 1

Hartmut Ruhl and Nina ElkinaLMU, Theresienstrasse 37, Munich, Room A238

Oct. 23, 2009

Page 2: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: Contr Stats

SP in C: Funcs

Page 3: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Topics covered in the course

Scientific programming in C

Maxwell’s equations

The Vlasov equation

The Boltzmann equation

The Vlasov-Boltzmann equation

Integration of ODEs

Monto-Carlo methods

The Vlasov-Maxwell-Boltzmann system

Page 4: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Literature

S. Oualline, Practical C, ISBN 1-56592-306-5

Numerical Recipes in C++, ISBN-10 0-521-75033-4

L. D. Landau and E. M. Lifshitz, PhysikalischeKinetik, ISBN 3-05-50063-3

J.M. Thijssen, Computational Physics, ISBN-100-521-57588-5

G. A. Bird, Direct Simulation of Gas Flows, ISBN 119 856195 4

Yu. N. Grigoryev, V. A. Vshivkov, M. P. Fedoruk,Numerical Particle-In-Cell Methods, ISBN90-6764-368-8

A. Bondeso, T. Rylander, P. Ingelstrom,Computational Electromagnetics, ISBN-100-387-26158-3

Page 5: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Topics today

Scientific programming in C

Page 6: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Introduction

C, C++, and FORTRAN are powerful languages for scientific programming.

C, C++ are more common, but we will restrict ourselves to C.

Only a few feature of C are needed for scientific computing.

Our cut down version of C resembles FORTRAN.

Page 7: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Variables

Possible variable names in C: x , mass, hello world .

Data types: Integer denoted int.

Data types: Floating-point denoted double.

int and double variables must be declared before they can be used.

Integer constants: 4, 7, 58, 2190.

Floating-point constants: 0.002, 34.345, 3e + 6.

Strings are used for data input or output.

Declarations: int a, b, c;

Declarations: double a1, b2, cc;

Declarations with inital values: int a = 1, b = 3, c = 12;

Declarations with inital values: double a1 = 1.0e5, b2 = 2.3e − 3, cc = 1.2e − 6;

Page 8: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Expressions andstatements

Expressions are: a + b, + addition operator.

Expressions are: x = y , = assignment operator.

Expressions are: t = u + v , addition and assignment.

Expressions are: x <= y , <= relational operator.

Expressions are: i + +, i = i + 1

Expression statement: i = i + 1;

Expression statement: x = y ;

Compound statement:

{

a = a + 1;

b = a + 5;

}

Symbolic constants are defined at the beginning of a program:

#define name text

#define pi 3.1415

Symbolic constants useful for natural constants.

Page 9: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Operators

The 4 main arithmetic operators:

addition +

subtraction −

multiplication ∗

division /

Avoid mixing types.

Type conversions:

(double) j

x/(double)j

Precedence: (∗, /) come before (+,−).

The 4 relational operators:

less than <

less than or equal <=

greater than >

greater than or equal >=

Precedence lower than for arithmetic operators.

Page 10: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Operators

Logical operators:

and &&

or ||

The 4 additional assignment:

i = i + 6 → i+ = 6

i = i − 6 → i− = 6

i = i ∗ 6 → i∗ = 6

i = i/6 → i/ = 6

Reduction of operations:

y = x + x ∗ x + x ∗ x ∗ x + x ∗ x ∗ x ∗ x

y = x ∗ (1 + x + x ∗ (x + x ∗ x))

Page 11: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Library functions

To use library functions a header file has to be called via a preprocessor statement:

#include < math.h >

C math library has the header file math.h.

Useful mathematical C functions are:

acos(x) double arccos of x

asin(x) double arcsin of x

atan(x) double arctan of x

cos(x) double cos of x

cosh(x) double hyperboliccos of x

sin(x) double sin of x

sinh(x) double hyperbolicsin of x

tan(x) double tan of x

tanh(x) double hyperbolictan of x

exp(x) double exponential of x

log(x) double naturallogarithm of x

log10(x) double logarithm base 10 of x

fabs(x) double absolute value of x

sqrt(x) double square root of x

Mathematical library functions are expensive.

Page 12: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Data I/OThe I/O header file statement is:

#include < stdio.h >

Useful I/O functions are:

scanf reads from terminal

printf writes to terminal

fscanf reads from file

fprintf writes to file

A call to scanf takes the form:

scanf (control string, arg1, arg2, arg3, ...)

An example:

#include < stdio.h >

...

int k ;

double x, y ;

...

scanf (”%d %lf %lf”, &k, &x, &y);

...

Page 13: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Data I/O

A call to printf takes the form:

printf (control string, arg1, arg2, arg3, ...)

An example:

#include < stdio.h >

...

int k = 5;

double x = 4.5, y = 2.6;

...

printf (”%d %f %f”, k, x, y);

...

Another example:

#include < stdio.h >

...

int k = 5;

double x = 4.5, y = 2.6;

...

printf (”k = %3d x + y = %8.4f x ∗ y = %10.3ef”, k, x, y);

...

Page 14: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Data I/O

When working with data files a buffer area needs to be created:

file ∗ stream;

A data file must be opened and attached to an I/O stream:

stream = fopen(file name, file type);

The file type string must be one of the following:

”r” open existing file for reading only

”w” open existing file for writing only

”a” open existing file in append mode

A data file must be closed at the end of a program:

fclose(stream);

Page 15: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Data I/OData can be read from an open data file:

fscanf (steam, control string, arg1, arg2, arg3, ...)

Data can be written to an open data file:

fprintf (steam, control string, arg1, arg2, arg3, ...)

An example:

#include < stdio.h >

...

int k = 5;

double x = 4.5, y = 2.6;

file ∗ output;

...

output = fopen(”data.out”, ”w”);

if (output == NULL)

{

printf (”Error opening file data.out”);

}

...

fprintf (output, ”k = %3d x + y = %8.4f x ∗ y = %10.3ef”, k, x + y, x ∗ y);

...

fclose(output);

...

Page 16: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Structure

Syntax of a complete C program:

...

int main()

{

...

return0;

}

...

All executable statements must be placed between int main() and return 0.

Function definitions are placed after the return 0 statement.

Page 17: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Structure

Calculation of the roots of a quadratic equation:

#include < stdio.h >

#include < math.h >

int main()

{

double a, b, c, d, x1, x2;

printf (”a = ”);

scanf (”%lf”, &a);

printf (”b = ”);

scanf (”%lf”, &b);

printf (”c = ”);

scanf (”%lf”, &c);

d = sqrt(b ∗ b − 4. ∗ a ∗ c);

x1 = (−b + d)/(2. ∗ a);

x2 = (−b − d)/(2. ∗ a);

printf (”x1 = %12.3e x2 = %12.3e”, x1, x2);

return0;

}

Page 18: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Contr Stats

The previous C program fails if b2 < 4ac or a < 0.

C has if − else statements which take the form:

if (expression) statement

or in a more general case:

if (expression) statement1 else statement2

Page 19: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Contr Stats

Usage of control statements:

#include < stdio.h >

#include < math.h >

int main()

{

double a, b, c, d, x1, x2;

printf (”a = ”);

scanf (”%lf”, &a);

printf (”b = ”);

scanf (”%lf”, &b);

printf (”c = ”);

scanf (”%lf”, &c);

if (a == 0.)

{

printf (”Error : a = 0.”);

exit(1);

}

Page 20: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Contr Stats

e = b ∗ b − 4. ∗ a ∗ c;

if (e > 0.)

{

d = sqrt(e);

x1 = (−b + d)/(2. ∗ a);

x2 = (−b − d)/(2. ∗ a);

printf (”x1 = %12.3e x2 = %12.3e”, x1, x2);

}

else

{

d = sqrt(−e);

x1 = −b/(2. ∗ a);

x2 = d/(2. ∗ a);

printf (”x1 = (%12.3e, %12.3e) x2 = (%12.3e, %12.3e)”, x1, x2, x1, −x2);

}

return0;

}

Page 21: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Contr Stats

C has while statements which take the form:

while (expression) statement

The statement is executed repeatedly as long as expression is true.

Example:

x5+ ax2

− b = 0

Iteration scheme:

xn+1 = (b − ax2n )

0.2

While statement can be applied.

Page 22: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Contr Stats

Usage of while control statements:

#include < stdio.h >

#include < math.h >

#include < stdlib.h >

#define NITER = 30;

int main()

{

double a, b, eps, x, x0, dx = 1.0, d ;

printf (”a = ”);

scanf (”%lf”, &a);

printf (”b = ”);

scanf (”%lf”, &b);

printf (”eps = ”);

scanf (”%lf”, &eps);

printf (”Initial guess for x = ”);

scanf (”%lf”, &x);

x0 = x ;

Page 23: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Contr Stats

while (dx > eps)

{

+ + count;

if (count > NITER)

{

printf (”Error : no convergence”);

exit(1);

}

d = b − a ∗ x ∗ x ;

if (d < 0.)

{

printf (”Error : complex roots − try another initial guess”);

exit(1);

}

x = pow(d, 0.2);

dx = fabs((x − x0)/x);

x0 = x ;

printf (”Iter = %3d x = %8.4fdx = %12.3e”, count, x, dx);

}

return0;

}

Page 24: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Functions

The first line of a function takes the form:

data − type name(type1 arg1, type2 arg2, ..., typen argn)

data − type represents the data type of the item that is returned by the function.

name represents the name of the function.

type1, ..., typen represent the data types of arg1, ..., argn.

Permissible data types for functions are:

int function that returns an integer value

double function that returns a floating-point value

void function that does not return a value

The body of a function is a compound statement.

return expression returns the value of expression to the calling part of the program.

For void functions return is used.

Page 25: Computational Physics Course 17094 Lecture 1 · SP in C: Intro SP in C: Vars SP in C: Stats SP in C: Ops SP in C: Lib funcs SP in C: Data I/O SP in C: Structure SP in C: Contr Stats

ComputationalPhysics Course

17094Lecture 1

Hartmut Ruhl andNina Elkina

LMU,Theresienstrasse37, Munich, Room

A238

Topics

Literature

Topic today

SP in C: Intro

SP in C: Vars

SP in C: Stats

SP in C: Ops

SP in C: Lib funcs

SP in C: Data I/O

SP in C: Structure

SP in C: ContrStats

SP in C: Funcs

Scientific programming in C: Functions

Example of a function that calculates factorials:

double factorial(int n)

{

int count;

double fact = 1.;

if (n < 0)

{

printf (”Error : factorial of negative integer not defined”);

exit(1);

}

for (count = n; count > 0; − − count) fact ∗ = (double) count;

return fact;

}