Interesting Examples of Computational Physics
  • Welcome to the blog of Computational Physics at the IFM!
  • Dynamical Systems
    • The simple pendulum
    • The spherical pendulum
  • The wave equation
    • The simple discretization
    • Boundary conditions
    • Interfaces between mediums
    • Knots on ropes
    • 2D wave equation
  • Time dependent Schrödinger equation
    • The Crank-Nicolson method
    • Free wave packet
    • Quantum Tunneling
    • Harmonic Oscillator
    • Forced Harmonic Oscillator
    • 2D Harmonic Oscillator
    • Wave-particle duality
Powered by GitBook
On this page
  1. The wave equation

The simple discretization

PreviousThe wave equationNextBoundary conditions

Last updated 9 months ago

Below is an example of what can be done with the work from this section. Physically, it represents the movement of a string with fixed boundaries.

As mentioned earlier, we are going to approximate the value of the function of in a discrete domain. To do this, ñets start by defining a discrete domain:

D={(xi,tj)∣xi=Xmin+iΔxtj=jΔyi=0,...,Nxj=0,...,NtΔx=Xmax−XminNxΔt=tfNt}\begin{align} D = \{(x_{i},t_{j}) | x_{i} &= X_{min} + i\Delta x \nonumber\\ t_{j} &= j\Delta y \nonumber\\ i &= 0, ... ,N_x \nonumber\\ j &= 0, ... ,N_t \nonumber\\ \Delta x &= \frac{X_{max} - X_{min}}{N_x} \nonumber\\ \Delta t &= \frac{t_f}{N_t} \} \end{align}D={(xi​,tj​)∣xi​tj​ijΔxΔt​=Xmin​+iΔx=jΔy=0,...,Nx​=0,...,Nt​=Nx​Xmax​−Xmin​​=Nt​tf​​}​​

In every point of space and time, the function f will be defined. To simplify the notation, we shall name:

fij=f(xi,tj)f_i^j = f(x_i,t_j)fij​=f(xi​,tj​)

We will now remember a very useful formula from calculus. It helps us calculate second order derivatives:

∂2f∂x2(xi,tj)=fi+1j−2fij+fi−1jΔx2+O(Δx2)\frac{\partial^2 f}{\partial x^2}(x_i,t_j) = \frac{f_{i+1}^j-2f_i^j+f_{i-1}^j}{\Delta x^2} + {\cal O}(\Delta x^2)∂x2∂2f​(xi​,tj​)=Δx2fi+1j​−2fij​+fi−1j​​+O(Δx2)

With this formula in mind, we can substitute the partial derivatives inside the wave equation with these subtractions, the result is the following:

fij−1−2fij+fij+1v2Δt2−fi−1j−2fij+fi+1jΔx2=O(Δt2,Δx2).\frac{f_i^{j-1} - 2f_i^j + f_i^{j+1}}{v^2\Delta t^2} - \frac{f_{i-1}^{j} - 2f_i^j + f_{i+1}^{j}}{\Delta x^2} = {\cal O}(\Delta t^2, \Delta x^2). v2Δt2fij−1​−2fij​+fij+1​​−Δx2fi−1j​−2fij​+fi+1j​​=O(Δt2,Δx2).

The nice thing about the obtained expression is that we can solve for f at a later time in terms of known values from the past:

fij+1=(vΔtΔx)2[fi−1j−2fij+fi+1j]+2fij−fij−1f_i^{j+1} = \left( \frac{v\Delta t}{\Delta x} \right)^2 [f_{i-1}^{j} - 2f_i^j + f_{i+1}^{j}] + 2f_i^j - f_i^{j-1} fij+1​=(ΔxvΔt​)2[fi−1j​−2fij​+fi+1j​]+2fij​−fij−1​

With this expression, we can calculate the function on a later time if we know the value of the function on the 2 prior times. To start the evolution, we require initial conditions. We must have the initial function (called g(x)) and the time partial derivative (called h(x)). With these two functions we can calculate 2 time steps and get the evolution going. The first time step would be:

fi0=g(xi)f_i^0 = g(x_i)fi0​=g(xi​)

But, for the second time step, we are gonna have to Taylor expand the function to the past:

fi−1=fi0−fi0˙Δt+12!fi0¨Δt2+O(Δt3)=fi0−hiΔt+12!∂2f∂x2v2Δt2+O(Δt3)\begin{align} f_i^{-1} &= f_i^0 - \dot{f_i^0}\Delta t + \frac{1}{2!}\ddot{f_i^0}\Delta t^2 + {\cal O}(\Delta t^3)\nonumber\\ &= f_i^0 - h_i\Delta t + \frac{1}{2!}\frac{\partial^2f}{\partial x^2}v^2\Delta t^2 + {\cal O}(\Delta t^3)\nonumber \end{align}fi−1​​=fi0​−fi0​˙​Δt+2!1​fi0​¨​Δt2+O(Δt3)=fi0​−hi​Δt+2!1​∂x2∂2f​v2Δt2+O(Δt3)​

The second derivative that appears in the expression can be computed using the same approximation used above. And this is all what we need to solve the wave equation in one dimension. The only detail missing are the boundary contidions, which will be discussed in the next section. To replicate the animations from above, the parameters and initial conditions needed are:

v(x) = 1.0 # Speed of the wave
dx = 0.006666666666666667
dt = 0.0033333333333333335
Nt = 1200

# Initial conditions
f_0(x) = 0.75*(1-x**2)*cos(2.0*x+1.0) # Initial value of the function
f'_0(x) = 0.0 # Initial value of the time derivative

# Boundary conditions
f(-1) = f(1) = 0
Numerical solution of the wave equation using fixed boundaries.