Wave Equation on the Line

In this code we present solutions to the wave equation u_tt=c^2u_xx for different initial data.

Solution with initial displacement but zero initial velocity

f[x_]:=Piecewise[{{-Abs[x]+1,Abs[x]<1},{0,x≥0}}];
c=1/2;

Plot[f[x],{x,-3,3},PlotRange->{{-3,3},{-.01,1.1}},AxesLabel->{"x","f(x)"},LabelStyle->Directive[Black, Large],PlotStyle->Thickness[.01]]

u[x_,t_]:=1/2*f[x-c*t]+1/2*f[x+c*t]

Plot[{u[x,0],u[x,1/2],u[x,1],u[x,3/2],u[x,2]},{x,-3,3},LabelStyle->Directive[Black, Large],PlotStyle->Thickness[.01]]

DensityPlot[u[x,t],{x,-3,3},{t,0,3},PlotRange->{0,1.1},PlotLegends->Automatic,PlotPoints->100,FrameLabel->{"x","t"},LabelStyle->Directive[Bold,Black, Medium],ColorFunction->"Rainbow"]

Animate[Plot[u[x,t],{x,-10,10},PlotRange->{{-10,10},{0,1.1}},AxesLabel->{"x","u(x,t)"},LabelStyle->Directive[Bold,Black, Medium],PlotStyle->Thickness[.0075]],{t,0,10}]

Solution with initial displacement but zero initial velocity

f[x_]:=Piecewise[{{-Abs[x]+1,Abs[x]<1},{0,x≥0}}];
c=1;

Plot[f[x],{x,-3,3},PlotRange->{{-3,3},{-.01,1.1}},AxesLabel->{"x","f(x)"},LabelStyle->Directive[Black, Large],PlotStyle->Thickness[.01]]

u[x_,t_]:=1/(2*c)*Integrate[f[s],{s,x-c*t,x+c*t}]

Plot[{u[x,0],u[x,1/2],u[x,1],u[x,3/2],u[x,2]},{x,-3,3},LabelStyle->Directive[Black, Large],PlotStyle->Thickness[.01]]

DensityPlot[u[x,t],{x,-3,3},{t,0,3},PlotRange->{0,1.1},PlotLegends->Automatic,FrameLabel->{"x","t"},LabelStyle->Directive[Bold,Black, Medium],ColorFunction->"Rainbow"]

Animate[Plot[u[x,t],{x,-10,10},PlotRange->{{-10,10},{0,1.1}},AxesLabel->{"x","u(x,t)"},LabelStyle->Directive[Bold,Black, Medium],PlotStyle->Thickness[.0075]],{t,0,10}]