Numerical methods and F90, final exercises

Here are some topics for the final exercise. You can also suggest other topics that might be useful for your own studies.


Instructions

Additional instructions as a pdf file

Contents of the work

To be accepted the work must contain at least the following:
  1. Source code of the program.
  2. Document of the program.
  3. Test results.

Program:

The F90 program must be complete. If a module containing procedures is required, also a main program used to test the module must be included.

Things affecting the evaluation of the work include: clarity of the solution, dividing the task into proper procedures, readability (variable names, comments, etc.), generality, self-explanatory output, ease of use.

Code fragments copied (possibly with modifications) from elsewhere must be clearly indicated both in the program code and document.

Document:

The documentation must include at least the following parts:

Testing:

Test the program using several different data sets. A few should be so simple that the correctness of the solution can be checked manually. Also a few bigger and more realistic data sets must be included, as well as some erroneous data to see how the program can recover from errors.

If a user defined function is given as a parameter to e.g. an integration routine, use several functions for testing. Again, some cases must allow an analytical solution for checking.


Returning the work and assistance

When you send the work by email, the best way is to collect all related files to one directory, pack to a tar file and send as an attached file. The document can be an ascii text file or a pdf file. NO DOCX FILES! Also, using some other windows related formats/programs may cause too much work to decipher in a Linux environment, in which case a new version will be wanted.

Help can be asked any time, but please be patient; I may not have time to answer immediately.

Personal consultation is available mainly at Tuorla. See our contact information.

Return the work by the end of January If more time is needed, contact me in advance.


Topics

Here are some suggestions for the final exercise. Some may require methods not discussed in the lectures; I can provide additional material, if needed. Feel free to suggest your own topic. The amount of code should be (at least) a few hundred lines.

Beginning

Linear algebra

Make a module that contains at least the following basic routines for linear algebra:

Be prepared for as many error situations as possible. The calling program must be informed on the errors.

Coefficients are read from a file, but the program can also generate random data for testing.

Study how the execution time depends on the number of equations (must be pretty big to show the differences).

Back to contents

Back to beginning


Least squares fit

Make a module for fitting different functions to a given data set:

In addition to the coefficients the procedures must return their errors, average residual of the data points and the maximum deviation from the function fitted.

Be prepared for as many error situations as possible. The calling program must be informed on the errors.

Back to contents

Back to beginning


Spline functions

Make a module for expressing a given data set (1- or 2-dimensional) in terms of spline-functions and evaluating the fit in an arbitrary set of points. The user can choose the value of the second derivative at the endpoints (zero, or the same as in the adjacent points). Use the method in which the second derivatives appear as unknown variables. Only nonzero diagonals of the coefficient matrix are stored.

Use some graphics library or a separate program to display the results.

Back to contents

Back to beginning


Integration

Make a module containing at least the following routines:

Be prepared for as many error situations as possible. The calling program must be informed on the errors.

Back to contents

Back to beginning


Differential equations 1

Make a module for solving initial value problems of ordinary differential equations. At least the following methods must be included.

Test the module with several different wquations. Investigate the accuracy if the methods and the dependence on the order of the method, step length, and the total length of the interval.

Initial values needed by multistep methods are first calculated using a single step method. Apply the methods to the linear harmonic oscillator and compare the results to the exact solution.

Back to contents

Back to beginning


Differential equations 2

Make a module for solving initial value problems of ordinary differential equations. At least the following methods must be included.

Test the module with several different wquations. Investigate the accuracy if the methods and the dependence on the order of the method, step length, and the total length of the interval.

Initial values needed by multistep methods are first calculated using a single step method. Apply the methods to the linear harmonic oscillator and compare the results to the exact solution.

Back to contents

Back to beginning


Special functions 1

Make a module for evaluating at least the following special functions: Be prepared for as many error situations as possible. The calling program must be informed on the errors. Test the functions using many different values. Investigate the accuracy and its dependence on the arguments. Find the renge where the functions can be safely used.

The first Legendre polynomials are

P0(x) = 1,
P1(x) = x,
P2(x) = (1/2) (3x2-1),
P3(x) = (1/2) (5x2-3x),
P4(x) = (1/8) (35x4-30x2+3).

Legendre polynomials can be evaluated using the recurrence relations

P0(x) = 1,
P1(x) = x,
(2n+1)xPn(x) = (n+1)Pn+1(x) + nPn-1(x).

The points needed for the Gaussian quadrature are zeros of the Legendre polynomials.

A lot of additional information can be found in e.g.
Arfken: Mathematical methods for physicists, Academic Press,
Abramowitz, Stegun: Handbook of mathematical functions, Dover.

Back to contents

Back to beginning


Special functions 2

Make a module for evaluating at least the following special functions:

Be prepared for as many error situations as possible. The calling program must be informed on the errors.

Test the functions using many different values. Investigate the accuracy and its dependence on the arguments. Find the renge where the functions can be safely used.

The elliptic integrals of the first kind are defined as

F(x | m) = \int0x [(1-t2) (1-mt2)]-1/2 d t, 0 <= m < 1,

and the integrals of the second kind as

E(x | m) = \int0x [ (1-mt2) / (1-t2) ]1/2 d t, 0 <= m <= 1.

The Bessel function Jk(x) is

Jk(x) = (1 / 2\pi) \int02\pi cos(kt - x sin t) d t.

The Bessel functions can also be evaluated from the series expansions

J0(x) = 1-(x/2)2 + (1/4) (x/2)4 - ... + [(-1)n / n!]2 (x/2)2n + ...

Jk(x) = (x/2)k (1/k !) [ 1 - (1 / k+1) ((x/2)2 + ... +
((-1)n / n !) (k+1)(k+2) ... (k+n) (x/2)2n + ... ].

Approximate formulas for the first Bessel functions are

J0(x) = 1 - x2/4 + x4/64 + O(x6),
J1(x) = x/2 - x3/16 + O(x5),
J2(x) = x2/8 - x4/96 + O(x6),
J3(x) = x3/48 + O(x5),
J4(x) = x4/384 + O(x6).

A lot of additional information can be found in e.g.
Arfken: Mathematical methods for physicists, Academic Press,
Abramowitz, Stegun: Handbook of mathematical functions, Dover.

Back to contents

Back to beginning


Special functions 3

Make a module for evaluating at least the following special functions:

Be prepared for as many error situations as possible. The calling program must be informed on the errors.

Test the functions using many different values. Investigate the accuracy and its dependence on the arguments. Find the renge where the functions can be safely used.

The Chebychev polynomial Tn is defined as

Tn(x) = cos n \phi = cos(n arccos x).

T0(x) = cos 0 = 1,
T1(x) = cos arccos x = x,
T2(x) = cos (2 arccos x) = cos 2\phi
= 2 cos2 \phi - 1
= 2 x2 - 1,
T3(x) = 4x3 - 3x,
T4(x) = 8x4 - 8x2 + 1
T5(x) = 16x5 - 20x3 + 5x.

Since the zeros of cos \phi are \phi=(2k+1)\pi/2, k=0,1,..., the zeros of the function cos n\phi are

\phi = ((2k + 1) / n) (\pi / 2), k=0, 1, ...

Since Tn(x)=cos n phi, these are also the zeros of the polynomial Tn. In terms of x the zeros are

xk = cos [ ((2kk+1)/n) (\pi/2)], k=0, 1, ... , n-1.

A lot of additional information can be found in e.g.
Arfken: Mathematical methods for physicists, Academic Press,
Abramowitz, Stegun: Handbook of mathematical functions, Dover.

Back to contents

Back to beginning


Optimisation

Make a program for n-dimensional local optimisation, when the derivatives of the function are not known. Proceed alternatively in the direction of each coordinate axis using a simple line search.

Allowed parameter ranges can be constrained by a penalty function that yields very high values outside the allowed region.

Back to contents

Back to beginning


Time series

Make a program for analysing unequally spaced time series. The analysis consists of the following stages: The data is read from a file. Make also procedures for generating test material. The test date will contain a given number of points, the x-coordinate being random and y-coordinate sin x plus a given amount of random noise.

Autocorrelation function

Dependence of the values separated by k time steps is described by the autocorrelation

R(k) = (1 / (N-k-1)) \sumi=1N-k fi fi+k,

whereN is the number of points. When this is evaluated for different values of the delay k, k=1, ..., N-2, the autocorrelation function is obtained.

Multiples of the actual period are always visible in the autocorrelation function.

In case of pure white noise successive values are not correlated at all. If there is continuous temporal variation, successive values are close to each others and the strongest correlation is found between adjacent (k=1) points. High correlations at small values of k are not due to periodicity. Only the next local maximum is an indication of the periodic nature of the series.

Structure function

Consider the difference x(t+\tau)-x(t). The first structure function is

D(\tau) = (1/N) \sum [ x(t+\tau) - x(t]2.

This is well defined even when the mean value and variance are not. Thus structure functions can be applied to more general time series than autocorrelation functions.

Back to contents

Back to beginning


Probability distributions

Make procedures for computing density and cumulative probability functions for different distributions and find the critical values xc, i.e. points where the cumulative probability has a given value P(xc)=c.

The following distributions must be included

The density function of the normal distribution is

f(x) = 1 / (\sqrt(2\pi)\sigma) exp( -(x-\mu)2 / \sigma2),

where \mu is expectation and \sigma2 variance.

The cumulative probability function of the normal distribution

P(x) = (1 / \sqrt(2\pi)) \int-\inftyx exp( - t2 / 2) d t

cannot be calculated analytically. However, several approximate expressions can be derived. The cumulative probability function of the (0, 1) normal distribution is approximately

P(x) = 1 - exp( -x2/2 / \sqrt(2\pi)) (0.4361836t-0.1201676t2+0.9372980t3),

where t=1/(1+0.33267x).
(Abramowitz, Stegun: Handbook of mathematical functions, Dover)

Back to contents

Back to beginning


Chaos

The Lorenz equations are classic equations of a chaotic phenomenon. They describe the atmospheric convection in a simplified form. The original Lorenz equations are:

d x/d t = a(y-x)
d y/d t = -xz+bz-y
d z/d t = xy-cz,

where a=10, b=28 ja c=8/3.

Select a suitable starting point, and draw x (or y) as a function of time. In the same picture, draw several curves using slightly different initial values.

Draw a diagram showing how y behaves as a function of x. Study how this curve changes when the constants a, b and c are changed.

Back to contents

Back to beginning


Rising and setting times

Calculate and plot a curve showing the rising and setting times for a given observing site (longitude, latitude). The object can be All formulas needed are found in the book Fundamental Astronomy. Draw the curves e.g. for a site at the equator, Helsinki, Oulu, and Utsjoki. The rising and setting times for the Finnish sites are found in the Finnish almanac and the yearbook Tähdet published by Ursa.

Back to contents

Back to beginning