# FD1D_WAVE Finite Difference Method 1D Wave Equation

FD1D_WAVE is a C program which applies the finite difference method to solve a version of the wave equation in one spatial dimension.

This program solves the 1D wave equation of the form:

```        Utt = c^2 Uxx
```
over the spatial interval [X1,X2] and time interval [T1,T2], with initial conditions:
```        U(T1,X)  = U_T1(X),
Ut(T1,X) = UT_T1(X),
```
and boundary conditions of Dirichlet type:
```        U(T,X1) = U_X1(T),
U(T,X2) = U_X2(T).
```
The value C represents the propagation speed of waves.

The program uses the finite difference method, and marches forward in time, solving for all the values of U at the next time step by using the values known at the previous two time steps.

Central differences may be used to approximate both the time and space derivatives in the original differential equation.

Thus, assuming we have available the approximated values of U at the current and previous times, we may write a discretized version of the wave equation as follows:

```        Uxx(T,X) = ( U(T,   X+dX) - 2 U(T,X) + U(T,   X-dX) ) / dX^2
Utt(T,X) = ( U(T+dt,X   ) - 2 U(T,X) + U(T-dt,X   ) ) / dT^2
```
If we multiply the first term by C^2 and solve for the single unknown value U(T+dt,X), we have:
```        U(T+dT,X) =        (     C^2 * dT^2 / dX^2 ) * U(T,   X+dX)
+  2 * ( 1 - C^2 * dT^2 / dX^2 ) * U(T,   X   )
+      (     C^2 * dT^2 / dX^2 ) * U(T,   X-dX)
-                                  U(T-dT,X   )
```
(Equation to advance from time T to time T+dT, except for FIRST step!)

However, on the very first step, we only have the values of U for the initial time, but not for the previous time step. In that case, we use the initial condition information for dUdT which can be approximated by a central difference that involves U(T+dT,X) and U(T-dT,X):

```        dU/dT(T,X) = ( U(T+dT,X) - U(T-dT,X) ) / ( 2 * dT )
```
and so we can estimate U(T-dT,X) as
```        U(T-dT,X) = U(T+dT,X) - 2 * dT * dU/dT(T,X)
```
If we replace the "missing" value of U(T-dT,X) by the known values on the right hand side, we now have U(T+dT,X) on both sides of the equation, so we have to rearrange to get the formula we use for just the first time step:
```        U(T+dT,X) =   1/2 * (     C^2 * dT^2 / dX^2 ) * U(T,   X+dX)
+       ( 1 - C^2 * dT^2 / dX^2 ) * U(T,   X   )
+ 1/2 * (     C^2 * dT^2 / dX^2 ) * U(T,   X-dX)
+  dT *                         dU/dT(T,   X   )
```
(Equation to advance from time T to time T+dT for FIRST step.)

It should be clear now that the quantity ALPHA = C * DT / DX will affect the stability of the calculation. If it is greater than 1, then the middle coefficient 1-C^2 DT^2 / DX^2 is negative, and the sum of the magnitudes of the three coefficients becomes unbounded.

### Languages:

FD1D_WAVE is available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version and a MATLAB version.

### Related Data and Programs:

FD1D_ADVECTION_LAX, a C program which applies the finite difference method to solve the time-dependent advection equation ut = - c * ux in one spatial dimension, with a constant velocity, using the Lax method to treat the time derivative.

FD1D_BURGERS_LAX, a C program which applies the finite difference method and the Lax-Wendroff method to solve the non-viscous time-dependent Burgers equation in one spatial dimension.

FD1D_BURGERS_LEAP, a C program which applies the finite difference method and the leapfrog approach to solve the non-viscous time-dependent Burgers equation in one spatial dimension.

FD1D_BVP, a C program which applies the finite difference method to a two point boundary value problem in one spatial dimension.

FD1D_HEAT_EXPLICIT, a C program which uses the finite difference method and explicit time stepping to solve the time dependent heat equation in 1D.

FD1D_HEAT_IMPLICIT, a C program which uses the finite difference method and implicit time stepping to solve the time dependent heat equation in 1D.

FD1D_HEAT_STEADY, a C program which uses the finite difference method to solve the steady (time independent) heat equation in 1D.

STRING_SIMULATION, a C program which simulates the behavior of a vibrating string.

### Reference:

1. George Lindfield, John Penny,
Numerical Methods Using MATLAB,
Second Edition,
Prentice Hall, 1999,
ISBN: 0-13-012641-1,
LC: QA297.P45.

### Examples and Tests:

test01_plot sets up the "shark wave".

test02_plot sets up a sine wave.

### List of Routines:

• FD1D_WAVE_ALPHA computes ALPHA for the 1D wave equation.
• FD1D_WAVE_START takes the first step for the wave equation.
• FD1D_WAVE_STEP computes a step of the 1D wave equation.
• R8_ABS returns the absolute value of an R8.
• R8MAT_WRITE writes an R8MAT file.
• R8VEC_LINSPACE creates a vector of linearly spaced values.
• TIMESTAMP prints the current YMDHMS date as a time stamp.

You can go up one level to the C source codes.

Last revised on 24 January 2012.