KTH - Examples Total

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

Prepatory examples for KTH COSSE programme.

Text of KTH - Examples Total

  • Refresher Material

    Numerical Analysis and Programming

    Edited by Michael Hanke, hanke@csc.kth.se

    version of April 28, 2010

  • Contents1 Introduction (Bengt Lindberg) 5

    2 Matlab exercises (Lennart Edsberg) 62.1 What is Matlab? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2 Vectors and matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.3 Linear algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.4 Graphics and plotting of curves . . . . . . . . . . . . . . . . . . . . . . . . 92.5 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    3 Programming (Bjrn Sjgren) 103.1 Simple problems to get started . . . . . . . . . . . . . . . . . . . . . . . . 103.2 Numerical problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.3 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    4 Linear algebra (Bengt Lindberg) 134.1 General concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.2 Systems of linear equations, formulations . . . . . . . . . . . . . . . . . . . 154.3 Systems of linear equations, accuracy . . . . . . . . . . . . . . . . . . . . . 164.4 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

    5 Least squares problems (Lennart Edsberg) 195.1 General concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195.2 Formulation and solution in Matlab . . . . . . . . . . . . . . . . . . . . . 205.3 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    6 Non-linear equations (Bengt Lindberg) 226.1 General concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226.2 Non-linear equations, formulations . . . . . . . . . . . . . . . . . . . . . . . 226.3 Non-linear equations, numerical methods . . . . . . . . . . . . . . . . . . . 226.4 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    7 Finite dierences and such (Jesper Oppelstrup) 247.1 Dierence operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.2 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.3 Quadrature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257.4 Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267.5 Band-matrix method for boundary value problems . . . . . . . . . . . . . 277.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

    3

  • 8 Ordinary dierential equations (Bengt Lindberg) 288.1 General concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288.2 Linear dierential and dierence equations and systems . . . . . . . . . . . 308.3 Numerical methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318.4 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    9 Mathematical Modeling (Jesper Oppelstrup) 339.1 Applied problems: Particle Dynamics . . . . . . . . . . . . . . . . . . . . 339.2 Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349.3 Vector analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359.4 Law of Mass Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369.5 Hydrodynamics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    10 Partial Dierential Equations (Gunilla Kreiss) 3810.1 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

    4

  • 1 Introduction (Bengt Lindberg)This collection of problems is organized into sections dealing with dierent aspects ofnumerical mathematics and programming.

    The idea behind the collection is to gather central prerequisites for starting your studyat KTH Royal Institute of Technology, Stockholm, Sweden, in Computational Science andEngineering on the Masters level. So you can see what we expect you to know in thedierent areas. If you are lacking some of this knowledge, you may need to read from thereferences to be able to solve the problems. You will have a minimum common platformto work from when you have solved this collection of problems. For us, the teachers of theMasters Programmes, it is of great value to know what the common platform is and whatwe can build upon.

    Many of the problems could be solved without a computer in a few minutes, whileothers may need a computer, reference material and more time.

    Some of the problems are intended to also give you an introduction to the computingenvironment at the School of Computer Science ans Communication (CSC) of KTH. Moredetails of the computing environment are described elsewhere.

    After having solved part of or all the problems from a section you may contact a teacheror tutor for discussions, hints and advice on possible further reading. You should solve theproblems individually, but discussions with classmates are encouraged.

    List of references are included for some of the sections but the lists are incomplete, soyou also need to use your own text-books for the refresher.

    5

  • 2 Matlab exercises (Lennart Edsberg)

    2.1 What is Matlab?

    Matlab1 is a programming environment for scientic and technical computations. It con-sists of a high-level programming language, a corresponding interpretation/compilation/runtimesystem, and an integrated development environment. Additionally, Matlab can be extendedby many toolboxes. On of the most important properties is the support of high-level nu-merical methods and easy-to-use graphical tools which makes it an excellent and versatiletool for fast prototyping both in education, academia, and industry. At KTH, many coursesmake extensive use of Matlab.It is therefore essential that you are comfortable with thebasic concepts of Matlab. The number of engineering textbooks introducing Matlab isunlimited. If you do not already have done so, learn Matlab immediately. It is worth theeort.

    Unfortunately, Matlab is a commercial product. There are some free open-source im-plementations of the Matlab programming language available. If you do not have accessto Matlab, we recommend to use GNU Octave2. Prebuild binaries for many systems areavailable, some with restricted capabilities. Try to nd one with sparse matrix capabilitiesincluded. Of course, it is not hard (but, maybe, time-consuming) to build GNU Octavefrom source. Even IDEs are available, one example is qtoctave3.

    2.2 Vectors and matrices

    1. Write a Matlab statement that generates the following row vector

    x = [0, 2, 4, . . . 322, 324, 1, 3, 17]

    2. Generate the 7 5 matrix A with the elements

    A =

    1 1 1 1 41 2 4 1/2 71 3 9 1/3 11 4 16 1/4 51 5 25 1/5 21 6 36 1/6 01 7 49 1/7 1

    3. Given the x-vector x = [0.1, 0.2, . . . , 2.0]. Generate a table [x y z] where

    y = sin(x2)/x + 1 and z = xex + 2x/(1 + x).

    1Matlab is a registrered trademark of The MathWorks, Inc., Natick, Massachusetts.2GNU Octave is Copyright John W. Eaton. It is provided under GPL.3http://qtoctave.wordpress.com/

    6

  • 4. Given the vector x = [0.98, 0.72, 0.75, 0.65, 0.07, 0.63]. Use the Matlab dot operators(+,, ., ., ./) to generate the following quantities: the mean value, the median, thestandard deviation and the harmonic mean H from the formula

    1

    H=

    1

    n

    ni=1

    1

    xi

    Check your results with the results obtained from the Matlab functions mean, median,std.

    5. Given the column vector y with the components y1, y2, . . . , y10, e.g. the digits of yoursocial security number, your bank account number, an ISBN or something similar.Generate a new vector dy containing the elements yj+1yj . Generate (without usinga for-loop) a vector with the nine components (yj + yj+1)/2. Finally, generate avector b with the following ten components: b1 = dy1, bj = dyj1+dyj, j = 2, 3, . . . , 9and b10 = dy9.

    7

  • 6. Generate the following 10 10 matrices in Matlaba. A = diag(2, 2, 2, . . . , 2)

    b. A tridiagonal matrix with 2 in the diagonal and 1 in the sub- and super diago-nals.

    c. A being a triangular matrix with the element 1 on and below the diagonal.

    7. Given the matrix A being the tridiagonal matrix in 6 b. Generate an augmentedmatrix B having the structure

    B =(A aT

    a

    )

    where a = [1, 1, . . . , 1] and = 1/2.

    2.3 Linear algebra

    8. Given the following two matrices A and B and the vector x

    A =(

    2 14 5

    ), B =

    (1 12 3

    ), x =

    (79

    )

    Generate these variables in Matlab and then generate C = AB, z = Ax, p = zT z,E = ATA, q = xTAx. Write out the results

    9. Use Matlab to solve the linear system of equations

    1 1 1 12 4 8 163 9 27 814 16 64 256

    x1x2x3x4

    =

    42214

    Write out x together with the residual r = b Ax.10. Use Matlab to solve the following over determined linear system of equations Ax b

    in the least squares sense

    1 32 43 54 65 7

    (x1x2

    )

    810121520

    Write out the least squares solution x and the residual r = b Ax. The residualshould be orthogonal to the columns of A. Check that with a Matlab command.

    8

  • 2.4 Graphics and plotting of curves

    11. The corners of a triangle have the x-coordinates 0, 4, 6.5 and the y-coordinates

    1.42, 6.18, 4.75. Mark the corners with an and draw the sides of the triangle.Compute the lengths of the sides, the circumference and the area.

    12. A circle is given by the parameter form x = xc +Rcos(), y = yc +Rsin(), where varies between 0 and 2. Plot a circle with radius 3 and with the