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:
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)
But, for the second time step, we are gonna have to Taylor expand the function to the past:
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