03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

264 CHAPTER 14. NUMERICAL EXERCISES<br />

This is called a boundary value problem.<br />

The goal is to compute the solution u <strong>for</strong> all x ∈ [0, 1]. Since this is not possible numerically, we<br />

only compute u <strong>for</strong> a discrete number of x’s, which we call discretization points. We discretize x<br />

as xj = jh <strong>for</strong> j = 0, . . . , n+1 and h = 1/(n+1). This is called an equidistant distribution. The<br />

smaller h, the closer we are to the continuous problem, i.e. we have more points in [0, 1], but, as<br />

we shall see, the problem becomes more expensive to solve. One method <strong>for</strong> solving boundary<br />

value problems is to replace the derivative by finite differences. We use finite differences <strong>for</strong> the<br />

second order derivatives:<br />

Filling this in (14.1), we obtain<br />

d 2 u<br />

dx 2 (xj) ≈ 1<br />

h 2 (−2u(xj) + u(xj−1) + u(xj+1)) .<br />

1<br />

h 2 (−u(xi−1) − u (xi+1) + 2u(xi)) = f(xi) <strong>for</strong> j = 1, . . . , n . (14.2)<br />

Note that u(x0) = u(xn+1) = 0. Now define the vectors<br />

u = [u(x1), . . . , u(xn)] T<br />

and f = [f(x1), . . . , f(xn)] T .<br />

Putting together (14.2) <strong>for</strong> j = 1, . . . , n leads to the algebraic system of equations Au = f with<br />

n rows and columns where ⎡<br />

2<br />

⎢ −1<br />

A = ⎢<br />

⎣<br />

−1<br />

2<br />

. ..<br />

−1<br />

. .. . ..<br />

⎤<br />

⎥<br />

⎦<br />

−1 2<br />

.<br />

Note that A is a symmetric tridiagonal matrix. We can show that it is positive definite.<br />

In the algorithms, we need operations on this matrix. We will use two different types of<br />

operations. The first one is the matrix-vector product y = Ax. We write a function <strong>for</strong> this<br />

with a template argument <strong>for</strong> the vectors since we do not know be<strong>for</strong>ehand what the type of<br />

the vectors will be.<br />

#ifndef athens poisson 1d hpp<br />

#define athens poisson 1d hpp<br />

#include <br />

#include <br />

namespace athens {<br />

template <br />

void poisson 1d( X const& x, Y& y ) {<br />

assert( glas::size(x)==glas::size(y) ) ;<br />

assert( glas::size(x) > 1 ) ;<br />

y(0) = 2.0∗x(0) − x(1) ;<br />

<strong>for</strong> ( int i=1; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!