Extensive information about Python, including the language reference, tutorials, modules etc.
can be found on www.python.org and on docs.python.org.
-
Have a look at file loop.py: The variable i is counted upwards and printed out. Try changing the loop's
rangetorange(3, 12)orrange(3, 12, 2)! You can also enterrange(in Spyder's console to get an in-line hint on its meaning. -
About Python's way of working with integer numbers: Define a variable like
num = 3ahead of the for loop and multiply it by itself during each loop iteration. What do you observe? Use the console to divide a resulting "really large" number by 3. -
Repeat 2., but this time, start with
num = 3.! Can you explain the different behavior? Use Python's built-in functiontype()to see various variables' or expressions' data types. -
Try this:
c = 1.6341 d = 9. d * (c / d) - c
Then, change
cto1.6346!
File convergence.py contains elements for a convergence test of the right-sided operator Dt that approximates the first derivative f' as discussed in the lecture.
-
Familiarize yourself with the Python code: What does each function do? Call them from the command line!
-
A script to run an actual convergence test is in convTest.py. Try it out! Which convergence order do you obtain?
-
Modify the programs to add the left-sided and central difference formulae and compare the orders of convergence.
-
What happens if you make h really small?
We continue with the derivation and test of finite difference formulae:
-
Verify the standard 3-point stencil for the second derivative f" as given in the lecture notes.
-
The discretizations addressed so far were based on equal spacings h between function values, employing function values f(x), f(x - h), f(x + h) etc.
What about a formula that uses values like
$f(x)$ ,$f(x - h_{-})$ ,$f(x + h_{+})$ , i.e. different spacings on either side? Can you derive, using Taylor expansion, stencils for f' and f" in this setting? Which order of accuracy do you get?Hint: When making convergence tests, use a fixed ratio h+ / h- for the limit h+, h- → 0.
-
Derive a one-sided, second order accurate approximation for f'(x) that employs f(x), f(x - h), and f(x - 2h). Verify!
Hint: Use your previous results.
-
Richardson extrapolation is a simple way to derive higher-order finite difference formulae from lower order stencils (see the lecture notes). Use it to improve the O(h²) central difference formulae for f' and f", respectively, and verify!
Here, we want to address the motion of a "point mass" under the influence of a central force
Closed, elliptical trajectories are approximated in file planet.py as positions of the mass at times
a) Compute the particle's acceleration
b) Compute
Which error sources come into play when computing r,
i) Finalize last week's program for the planetary motion as discussed in the lecture: Add the computation of the exact acceleration F/m to quantify the errors for different N and E.
Note: You'll find an updated version of planet.py in Moodle (be sure to back up your previous one before downloading!).
ii) Then, replace the approximate solution of Kepler's equation,
by "true" root-finding with Newton's method.
Hint: Copy code from file findroot.py into a new function keplerNewton(t, ex) inside planet.py.
Bi-sectioning and Newton's method have been implemented in findroot.py and discussed in the lecture: Add the secant method as a further possibility when f' is not available analytically. Compare with the previous two!
File decay.py approximates the solution to the simple decay equation
with
(see lecture notes).
i) Study the solution behavior for different time steps Δt: Can you verify the different regimes of the iteration eigenvalue
Which exact eigenvalue
does the differential equation give, and how is it related to the numerical λ?
ii) Alternatively, implement the backward Euler scheme,
and the trapezoidal scheme,
and compare. What are the respective iteration eigenvalues here?
In a similar way, study the ODE system of the harmonic oscillator,
as prepared in file harmosc.py (again, see the lecture notes).
a) For a pendulum with given physical parameters
What are the "units" of time, energy and angular momentum in this normalized form?
b) In pendulum.py, the normalized equations are integrated using the forward-Euler method. Compare the behavior for different time steps Δt.
c) Implement alternative methods and investigate the behavior for different Δt again:
i) the 4th order Runge-Kutta method.
ii) the trapezoidal rule.
Hint: To get an easy implementation, introduce
Plug this into the discretized equations of motion to get a single transcendent equation for solvePendel by means of Newton-Raphson iteration.
iii) the drift-kick / Leapfrog / Verlet method.
The Python program in file heatEuler.py approximates the heat equation as an initial value problem using the forward-Euler method.
a) Verify the method quantitatively: Replace the initial step function by
as initial condition, where max(f) for comparison).
Does the method work properly?
b) When using a higher resolution in N_sub):
Can you verify the stability condition
c) Prove this stability criterion by means of a von-Neumann analysis: Assume the discrete solution at
for some given wave number
i.e. the iteration eigenvalue, for different
d) The program employs Dirichlet conditions by keeping f[0] and f[-1] (last element in the Python array) unchanged. Try to implement:
i) homogeneous von-Neumann conditions,
ii) periodic boundary conditions,
Which physical situations might these conditions reflect?
We consider the diffusion equation
a) Consider the one-dimensional setup with a diffusion coefficient that depends on position, i.e.,
at “staggered” positions
Do the results match your physical expectations?
b) You find a Python implementation of the backward Euler method for the case of constant
Can you estimate and verify the stability property for this case?
Can you implement Dirichlet-type boundary conditions?
c) Generalize the implicit method from b) to
What does the discretization matrix look like?
The equations of ideal, adiabatic gas dynamics are:
-
Continuity equation:
$$ \frac{\partial \rho}{\partial t} + \mathbf{v} \cdot \nabla \rho = -\rho \nabla \cdot \mathbf{v} $$ -
Momentum equation:
$$ \rho \left( \frac{\partial \mathbf{v}}{\partial t} + \mathbf{v} \cdot \nabla \mathbf{v} \right) = -\nabla p $$ -
Energy equation:
$$ \frac{\partial p}{\partial t} + \mathbf{v} \cdot \nabla p = -\gamma p \nabla \cdot \mathbf{v} $$
Here,
Assuming all quantities depend only on one spatial coordinate
a) Show that this system admits traveling-wave solutions of the form
What is the relationship between the envelope functions
What is the sound velocity
b) Solve the system numerically with periodic boundary conditions in
You may use Leap-Frog integration between
What do you observe?
c) Can you change the initial conditions for an isolated perturbation to move only to the left or only to the right?
The file pdeSpectral.py contains a program to solve the diffusion equation
using the Fourier spectral method as discussed in the lecture.
a) Try it out and extend it to the convection-diffusion equation
with constant
b) Can you, in a similar way, integrate the time-dependent, free-space Schrödinger equation
in this one-dimensional setup? Try, for example, a wave packet
with
Hints: Start a new program file for this problem. Plot
Bonus question I: If you were to introduce an additional potential
Bonus question II: Which other methods could you envisage to use instead of the Fourier spectral approach?
📧 juergen.dreher@rub.de
📧 kevin.schoeffler@rub.de