Computational Methods - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Computational Methods

Description:

Numerical calculations: large scale, not analytically solvable problems ... Frequently-made beginner mistakes: if (a=1) always true, a is set to 1. ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 18
Provided by: bul8
Category:

less

Transcript and Presenter's Notes

Title: Computational Methods


1
Computational Methods
  • H.J. Bulten henkjan_at_nikhef.nl
  • website www.nikhef.nl/henkjan , click on
    Computational Methods
  • Aim course practical understanding of numerical
    methods in (physics) problems.
  • Numerical calculations large scale, not
    analytically solvable problems
  • techniques integration, Fourier analysis etc.
  • are the results correct? Verify.
  • Contents course theory department
  • Practical book Numerical Recipes.
  • available on web https//www.fyzika.umk.pl/nrboo
    k/bookcpdf.html
  • covered material see next slide
  • extra material FORM, roofit fitting,
    Schroedinger equation
  • Examination
  • Completion of exercises required
  • paper on numerical problem

2
Exam
  • the numerical methods are divided in three
    groups
  • PART 1
  • Interpolation and extrapolation, chapter 3
  • Evaluation of functions, chapter 5
  • Roots, Minima and maxima, chapters 9 and 10
  • Fourier methods, chapters 12 and 13
  • PART 2
  • Systems of linear equations, chapter 2
  • Eigensystems, chapter 11
  • Integration of functions, chapter4
  • PART 3
  • Integration of differential equations, chapter
    16
  • Boundary value problems, chapter 17
  • Monte Carlo techniques chapter 7

3
Examination
  • Exercises mail code and result within 2 weeks
  • results will be discussed 1 week after that
  • Project
  • define a (preferably physics) problem, that needs
    techniques from at least 2 of the three parts
    discussed before (e.g. function approximation and
    integration of differential equations).
  • try for each part at least 3 different approaches
    to solve your problem
  • discuss the results in terms of accuracy,
    stability, speed, applicability, .....

4
Week 1
  • Computational methods
  • non-analytical problems
  • computing uncertainties
  • speed
  • accuracy
  • stability
  • applicability
  • maintainability
  • elegance
  • simplicity

optimize algorithms
5
week 1, uncertainty
  • error
  • integers fixed point
  • int, long, short, char
  • signed/unsigned
  • accurate
  • division skip remainder
  • underflows/overflows
  • banking
  • floating point
  • float, double, real4,real8, complex16
  • machine-dependent representation
  • signbit,mantissa,exponent
  • base usually 2, sometimes 16
  • E fixed offset

6
week 1, floating precision
  • e.g. ½ 0 1000000.. 1000..
  • 30 1100000 10..010
  • leading 1 may be omitted
  • addition left shift mantissa, until exponents
    equal
  • precision machine accuracy
  • which number is the smallest number added to 1,
    so that result differs from 1?
  • subtract? might differ by a factor 2
  • smallest non-negative number depends on
    exponent (and definition of mantissa)
  • largest number depends on exponent

7
week 1, floating errors
  • round off errors
  • machine compiler dependent
  • typically 10-17 double, 10-7 float
  • round-off always contributes
  • lucky ?
  • different size numbers
  • kinematics, e.g. laser cooling of atoms
  • roots
  • a or c small?
  • think before you program. No such terms with
    widely different magnitude should be present in
    your code.

8
week 1 round off/truncation error
  • Stability round off error may accumulate
  • e.g. golden mean
  • - example phi has roundoff error. The zeroth
    term is exact (1) but the first a little bit too
    small or too large
  • error blows up exponentially
  • floats completely off after 16 recursions
  • Exercise
  • Bessel functions
  • truncation
  • infinite terms
  • sin(x) dev.
  • needs to be minimized by algorithms
  • goal of numerical analysis derive optimal
    algorithms for the task at hand.

9
programming basics Fortran
  • Syntax can be found on the web
  • e.g http//h18009.www1.hp.com/fortran/docs/rm/dflr
    m.htm
  • Nowadays free style format available (lines of
    132 characters).
  • Used to be 72 characters (punch cards)
  • 1st column to indicate comment line
  • 2nd-5th labels
  • 6th continuation of previous line
  • 7-72 statements
  • 73-80 comment

10
Fortran example
  • c example
  • program p()
  • real8 a
  • integer i
  • do 10, i1,100,3
  • aexp(i.1d-01)
  • write(6,99) i,a
  • 10 continue
  • 99 format(i4,E12.2)
  • end

11
Fortran
  • Fortran passes variables by reference
  • subroutines change the value of passed parameters
    inside the main program (or the subroutine that
    called it)
  • Special care needed with common blocks
  • it is advisable to specify the common block in an
    include file.
  • e.g. routine 1
  • implicit integer4 a-h
  • real8 a(100)
  • common/h/a,b,c,d
  • routine 2
  • float f
  • dimension f(2,10,10)
  • /common/h/f
  • with such constructs one may overwrite ALL kinds
    of variables. If f(1,1,3) is overwritten, some a
    will have an arbitrary value.

12
Fortran
  • IO via open(unitX,....)
  • standard input unit 5
  • standard output unit 6
  • free format is allowed, but I caution against it
    when one uses external subroutines as well. Old
    programs DO sometimes put comment in columns
    73-80.

13
C
  • The C programming language by Kernigham and
    Ritchie, ed. Prentice Hall, New Jersey.
  • (ANSI C)
  • common compilers visual studio on windows,
    gcc/g on unix.
  • C passes all variables by value.
  • C is more complex than fortran, using pointers
    and variables. Frequently-made beginner mistakes
  • if (a1)
  • always true, a is set to 1.
  • float b, myvar bmyvar delete b
  • deletes myvar as well. Memory may be overwritten.
  • especially when variables themselves are
    pointers, things may go awry.

14
C
  • frequently-made mistakes
  • double a100 a1001e7
  • array a runs from 0 to 99, a100 is not defined
  • syntax, e.g. ab, aptrb, a/ptrb
  • last statement starts comment
  • nested comments
  • e.g. when you comment out an action

15
C
  • Bjarne Stroustrup, the C programming language
  • On unix g compiler
  • On windows visual studio
  • C contains C
  • C is written in C (so C also contains C)
  • pointers, references and objects
  • Object-oriented
  • method overloading by use of the keyword const a
    simple function working on a double may be
    defined in 18 different ways.
  • for most classes (certainly containing pointers)
    one should specify the assignment constructor,
    copy constructor and destructor as well.

16
include files
  • One often uses include files. If you do so, I
    advise to use the precompiler statements
  • ifndef mymethod_h
  • define mymethod_h
  • here goes the stuff you want to put in the
    include file
  • endif
  • The include file should be included only once,
    else new functions and variables with the old
    names are declared.
  • If, in a certain method, the function prototype
    that you use is not known (e.g. from an include
    file), the program WILL compile and run. The
    function WILL be executed, but the return value
    is interpreted as an integer!

17
C-example
  • example to test the accuracy of your machine and
    to print out the bit pattern.
  • bit patterns needed when dealing with binary
    data across platforms.
  • computational
Write a Comment
User Comments (0)
About PowerShow.com