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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14.3. THE SOLUTION OF A SYSTEM OF DIFFERENTIAL EQUATIONS 273<br />

The method that we use here is the Runge-Kutta 4 method, which is described here: the<br />

solution at time step tj+1 is computed as<br />

14.3.2 Software<br />

Write a generic function<br />

uj+1 = uj + h<br />

6 (k1 + 2k2 + 2k3 + k4)<br />

k1<br />

k2<br />

=<br />

=<br />

f(uj)<br />

�<br />

f uj + h<br />

2 k1<br />

k3 =<br />

�<br />

�<br />

f uj + h<br />

2 k2<br />

�<br />

k4 = f(uj + hk3)<br />

template <br />

void rk4( U& u, F& f, T const& h ) ;<br />

that computes one time step with the Runge-Kutta 4 method. The argument u is on input the<br />

solution at time t and on output at timestep t+h. The argument f is the functor that evaluates<br />

the function f(u). The argument u is a vector.<br />

When the implementation is finished, write the concepts <strong>for</strong> U and F in comment lines in the<br />

code.<br />

14.3.3 The van der Pol oscillator<br />

Differential equations appear in the study of physical phenomena. The Van der Pol oscillator<br />

is described by the following equation:<br />

d2x dt2 − µ(1 − x2 ) dx<br />

+ x = 0 (14.3)<br />

dt<br />

with initial solution x(0) and x ′ (0). This is a non-linear second order differential equation, with<br />

a parameter µ. When µ = 0, we have a purely harmonic solution (cos and sin). When µ ≥ 0,<br />

the solution evolves to a harmonic limit cycle.<br />

Second order differential equations are usually solved by writing them as a system of first order<br />

differential equations:<br />

� � � � � �<br />

d dx<br />

dt −µ(1 − x2 dx<br />

) 1<br />

+<br />

dt = 0 .<br />

dt x<br />

−1 0 x<br />

In matrix <strong>for</strong>m, the equation can be written as<br />

where<br />

A(u) =<br />

du<br />

dt<br />

+ A(u)u = 0<br />

� −µ(1 − u 2 2 ) 1<br />

−1 0<br />

�<br />

,

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

Saved successfully!

Ooh no, something went wrong!