The One-Dimensional Wave Equation

Part 1. Traveling Waves

1 and 2. Enter and graph the function f.  We plot two copies, saving one (in blue) for latter comparison.

f[x_] :=  
        If[x ≥ 1  &&  x ≤  3, 1 - Abs[x - 2], 
          0];
graph1  =  
        Plot[f[x], {x, -4, 8}, 
          PlotStyle -> RGBColor[1, 0, 0]];
graph1b  =  
        Plot[f[x], {x, -4, 8}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

Define a solution function of the wave equation with the initial condition.

a  =  1;
y[x_, t_] :=  1/2*f[x + a*t] + 1/2*f[x - a*t];

Graph y as a function of x for a range of values of t.

t  =  0;   
graph2  =  
        Plot[y[x, t], {x, -4, 8}, PlotStyle -> RGBColor[1, 0, 0], 
          DisplayFunction -> Identity];
Show[graph1b, graph2, 
        DisplayFunction -> $DisplayFunction];

4. Define our extended initial condition.

fe[
          x_] :=  (-1)^
            Floor[x/4]*(Round[(x + 1)/4] - 
              Floor[(x + 1)/4])*(1 - 
              4*Abs[x/4 - Floor[x/4] - 1/2]);
Plot[fe[x], {x, -8, 8}, 
        PlotStyle -> RGBColor[1, 0, 0]];

Now consider the traveling wave solution for the range of t.

a  =  1;
ye[x_, t_] :=  1/2*fe[x + a*t] + 1/2*fe[x - a*t];

t  =  0;   
graph3  =  
        Plot[ye[x, t], {x, -8, 8}, 
          PlotStyle -> RGBColor[1, 0, 0]];

Part 2. Fourier Series Form of Solutions

Calculate the Fourier coefficients for the half range sine expansion for f.  Note that we have given the UnitStep formulation of the function f.  Since f is equal to zero outside of [1,3], we only need to integrate over [1,3] when calculating c[k].  Check that this expression for f is correct.  We save the plot for later comparison with the Fourier approximation.

f[
          x_] :=  (x - 1)*
            UnitStep[-(x - 1) (x - 2)] + (3 - x)*
            UnitStep[-(x - 2) (x - 3)];
graph4b  =  
        Plot[f[x], {x, 0, 4}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

1/2*Integrate[f[x]*Sin[k*(Pi/4)*x], {x, 1, 3}];
c[k_] = %

Note that by symmetry, we could have written c[k] = 2 (∫_1^2)(x - 1) 
            Sin[(π/4) kx] dx.

Define the Fourier approximations to f.

FA[x_] :=  Sum[c[k]*Sin[k*Pi/4*x], {k, n}];

Compare the graph of the Fourier approximation with the graph of f.

\(n\  = \ 1; \  (*\ An\ Example\ *) 

    graph4a\  = \ 
      Plot[FA[x], {x, 0, 4}, PlotStyle -> RGBColor[1, 0, 0], 
        DisplayFunction -> Identity];\)
Show[graph4a, graph4b, 
        DisplayFunction -> $DisplayFunction];

Define the approximation A to the solution of the initial/boundary value problem.

A [x_, t_] :=  
        Sum[c[k]*Sin[k*Pi/4*x]*Cos[k*Pi/4*t], {k, n}];

Compare the time snapshots of the separation-of-variables approximation (in red) with the traveling wave solution (in blue).

t  =  0;   
graph4c  =  
        Plot[A[x, t], {x, 0, 4}, PlotStyle -> RGBColor[1, 0, 0], 
          DisplayFunction -> Identity];
graph4d  =  
        Plot[ye[x, t], {x, 0, 4}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];
Show[graph4c, graph4d, DisplayFunction -> $DisplayFunction, 
        PlotRange -> {-1, 0}];

Part 3. Non-Zero Initial Velocity

Define and graph the initial velocity function g.  Since we will be plotting graphs with similar names, we suppress the spelling warnings.

Off[General::spell1];

g[x_] :=  
        Which[x ≥ 1  &&  x < 2, 1/2*(x - 1)^2, 
          x ≥ 2  &&  x < 3, 1/2*(x - 3)^2, True, 
          0];
graph5  =  Plot[g[x], {x, 0, 4}];
graph5b  =  
        Plot[g[x], {x, 0, 4}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

Here is the UnitStep form for g.  Check that it is correct.

g[
          x_] :=  (1/2*(x - 1)^2)*
            UnitStep[-(x - 1) (x - 2)] + (1/2*(x - 3)^2)*
            UnitStep[-(x - 2) (x - 3)];

Calculate the coefficients d[k].  Note that we only need to integrate over [1,3].

2/(k*Pi)*
        Integrate[g[x]*Sin[k*(Pi/4)*x], {x, 1, 3}];
d[k_] =  %

As before, we could have used symmetry to simplify d[k] so that d[k] = (4/k)π(∫_1^2)(((x - 1))^2) 
          Sin[(π/4) kx] dx.

Compare the graphs of the Fourier approximations to g with the graph of g.

FB[x_] :=  Sum[d[k]*k*Pi/4*Sin[k*Pi/4*x], {k, n}];

\(n\  = \ 1; \  (*\ An\ Example\ *) 

    graph5c\  = \ 
      Plot[FB[x], {x, 0, 4}, PlotStyle -> RGBColor[1, 0, 0], 
        DisplayFunction -> Identity];\)
Show[graph5b, graph5c, PlotRange -> {0, 1}, 
        DisplayFunction -> $DisplayFunction];

Define the approximation to the solution of the initial/boundary value problem.

A2[x_, t_] :=  
        Sum[Sin[k*Pi/4*x]*(c[k]*Cos[k*Pi/4*t] + d[k]*Sin[k*Pi/4*t]), {k, 
            n}];

Look at time snapshots of the solution.

t  =  15;   
graph6  =  
        Plot[A2[x, t], {x, 0, 4}, PlotRange -> {-1, 1}, 
          PlotStyle -> RGBColor[1, 0, 0]];

Part 4. Summary