MAS2008 Scientific Computing: Lab 10
3D Plotting and animations

Instructions

The following notebooks and videos are relevant for this lab:
       
Plotting a three-core cable View Download
An eigenvector animation View Download
Plotting a torus View Download

Task 1: The dodecahedron

Make a 3D plot of a dodecahedron, as follows.

Task 2: Solitons

The following partial differential equation is called the Korteweg–De Vries equation: $$ \frac{\partial\phi}{\partial t} + \frac{\partial^3\phi}{\partial x^3} + 6\phi\frac{\partial\phi}{\partial x} = 0. $$ It is a mathematical model of water waves propagating in a shallow, narrow channel. As the last term involves $\phi$ multiplied by a derivative of $\phi$, this PDE is nonlinear, which usually means that it is impossible to find exact solutions. However, this particular PDE has some special mathematical features which mean that some exact solutions (called solitons) can in fact be found. In this task we will make animated plots of solitons.

Use these imports:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

%matplotlib widget   
   
(Again, you could use ipympl or qt instead of widget.)

Given real numbers $t$ and $x$, we make the following definitions: \begin{align*} q &= \sqrt{2} & p &= \log(3+2q) \\ r &= x-4t & s &= q(x-8t) \\ T &= 32\cosh(2r-p) + 16\cosh(2s-p) + 16 & B &= 4(1+q)\cosh(r)\cosh(s) + (4q-8)e^{r+s} \end{align*} Using these quantities, we can define functions $\phi_i(t,x)$ for $i=0,\dotsc,4$. One can check that these are all solutions of the KdV equation. \begin{align*} \phi_0(t,x) &= 2\cosh(r)^{-2} & \phi_1(t,x) &= 4\cosh(s)^{-2} \\ \phi_2(t,x) &= 2\cosh(r-p)^{-2} & \phi_3(t,x) &= 4\cosh(s-p)^{-2} \\ \phi_4(t,x) &= T/B^2. \end{align*} Write a Python function phi(t, x) that calculates the vector $(\phi_0(t,x),\dotsc,\phi_4(t,x))$. Your code should closely mirror the above definitions; do not try to collapse everything into a single formula. Make sure that your code is vectorized, so that it works correctly when t and x are arrays of the same shape. (It will be even better if your code handles broadcasting correctly when t and x have different shapes, but that is left as an additional challenge for enthusiasts.) Enter your definition in the online test system.

Now plot $\phi_4(-0.8,x)$ for $x$ from $-30$ to $30$, using at least $1000$ points to ensure that the plot is smooth. Experiment with changing the $-0.8$ to various other values, or plotting $\phi_i$ for some other $i$.

Now make an animation of $\phi_4$.

Task 3: Stacked balls

Make a 3D plot of 20 spheres arranged in a tetrahedron, as follows.