# Programmierpraktikum

Claudius Gros, SS2012

Institut für theoretische Physik
Goethe-University Frankfurt a.M.

# first vs. second order differential equations

### classical dynamics - second order

$$\frac{d^2}{dt^2}\,\Big(\,m\,x(t)\,\Big) \ =\ F(t,x,\dot x), \qquad \ddot x(t) \ =\ f(t,x,\dot x)$$
• every higher order differential equation can be transformed
into a set of first order differential equations
$$\begin{array}{rcl} \dot{x} & =& y \\ \dot{y} & = & f(t,x,y) \end{array}$$

### first order differential equations

• in the following we will consider
$$\dot y \ =\ f(t,y)$$

# Euler Integration

### initial condition

$$\dot y \ =\ f(t,y),\qquad y(t_0) \ =\ y_0, \qquad t_n \ =\ t_0\,+\,n\,h, \qquad n=0,\, 1,\,2,\, \dots$$

### one step algorithm

$$\frac{y(t+h)-y(t)}{h} \ \approx\ \dot y \ =\ f(t,y),\qquad y(t+h) \ \approx\ y(t) \,+\,h\,f(t,y)$$

$$y(t_{n+1}) \ \approx\ y(t_n) \,+\,h\,f(t_n,y_n)$$

# convergence considerations

• the one-step Euler integration converges
linearly in the integration step $h$
• can we find algorithms converging like $\ O(h^m)\$?

### Taylor expansion to second order

$$y(t+h) \ \approx\ y(t)\,+\,h\,\dot y(t)\,+\,\frac{h^2}{2}\,\ddot y(t), \qquad\quad \dot y = f(t,y)$$

with

$$\ddot y \ =\ {d\over dt} \dot y \ =\ {d\over dt} f(t,y) \ =\ f_t\,+\,f_y\,\dot y$$

and hence

$$y(t+h)\,-\,y(t) \ =\ \Delta y\ \approx\ h\,\dot y\,+\,\frac{h^2}{2}\,\Big(\,f_t\,+\,f_y\,f\,\big)$$

# accuracy to second order

• goal:
$$\Delta y\ =\ h\,f\,+\,\frac{h^2}{2}\,\Big(\,f_t\,+\,f_y\,f\,\big)$$
• higer derivatives in general unkown
• Runge-Kutta: we need only approximations to the derivates
to the desired order

### Runge-Kutta Ansatz

$$\Delta y(t)\ =\ a\,f(t,y)\,+\,b\,f\Big(t+\alpha h,y+\beta h f\Big)$$
• free parameters: $\ a,\ b,\ \alpha,\ \beta$

# Runge-Kutta alogrithm - derivation

$$\begin{array}{lcr} \Delta y(t)& =& a\,f(t,y)\,+\,b\,f\Big(t+\alpha h,y+\beta h f\Big) \\ & =& a\,f\,+\,b\,f\,+\,b\,\Big(f_t \alpha\,+\,f_y f\beta\Big)\,h \\ & :=& h\,f\,+\,\frac{h^2}{2}\,\Big(\,f_t\,+\,f_y\,f\,\big) \end{array}$$
• comparing coefficients
$$a+b\ =\ h,\qquad\quad \alpha\, b \ =\ \frac{h}{2} \qquad\quad \beta\, b \ =\ \frac{h}{2}$$
• one free parameter remaining
(3 conditions, 4 parameters)

# Runge-Kutta algorithm

### simple Runge-Kutta

$$y_{n+1}\ =\ y_n\,+\,\frac{h}{2}\ \Big[\, f(t_n,y_n)\,+\,f(t_n+h,y_n+hf(t_n,y_n)\, \Big]$$
• converges quadratically with integration step $\ h$

### classical Runge-Kutta algorithm

• repeat for Taylor expansion to order $\ O(h^4)$
$$\Delta y\ =\ \frac{h}{6}\ \Big[\, k_1\,+\,2k_2\,+\,2k_3\,+\,k_4\, \Big]$$

with

$$k_1 = f(t,y)\,,\qquad k_2 = f\left(t+\frac{h}{2},y+\frac{hk_1}{2}\right)$$

and

$$k_3 = f\left(t+\frac{h}{2},y+\frac{hk_2}{2}\right)\,,\qquad k_4 = f(t+h,y+hk_3)$$
• recursive function evaluation

# example: Kepler problem

### Newton's equation of motion

$$m\dot{\vec v}\ =\ -\frac{GMm}{|\vec x|^3}\, \vec x , \qquad\quad \dot{\vec v}\ =\ -\frac{g}{|\vec x|^\alpha}\, \vec x , \qquad\quad \alpha = 3$$

### first order differentional equation

$$\vec y \ =\ (\vec x, \vec v) \ =\ (x,y,\dot x,\dot y) , \qquad\quad \dot{\vec y} \ =\ \vec f(\vec y)$$
• angular momentum conservation $\rightarrow$ planar orbit
$$\vec f(\vec y) \ =\ \big(\vec v, -g\vec x/|\vec x|^\alpha \big) , \qquad\quad |\vec x| = \sqrt{x^2+y^2}$$

# integration of Kepler problem

### Runge-Kutta for vector variables

$$\dot{\vec y} \ =\ \vec f(t,\vec y)$$
• substitute scalar quantities by vectors
• simple Runge-Kutta
$$\Delta\vec y\ =\ \frac{h}{2}\ \Big(\, \vec f(t,\vec y)\,+\,\vec f(t+h,\vec y+h\vec f(t,\vec y)\, \Big)$$
• analogous for classical Runge-Kutta

# exercise: integration

• write a program for integrating the Kepler problem using
• Euler's method
• one-step Runge-Kutta
• classical Runge-Kutta
• what happens if the potential scales like $\ V(r)\propto r^{-\alpha}$
and $\ \alpha \ne 3$?