SetDirectory[$HomeDirectory<>"/Documents/handouts/math467/WaveEquation"]

The Wave Equation

Graphics[{PointSize[Medium],Point[{{4,1},{3,2},{4,2},{5,2},{4,3}}],Dashed,Line[{{3,2},{4,3}}],Line[{{4,1},{4,3}}],Line[{{5,2},{4,3}}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SuperscriptBox[
StyleBox["r", "TI"], "2"], TraditionalForm], "errors" -> {}, "input" -> "r^{2}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{3.25,2.75}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SuperscriptBox[
StyleBox["r", "TI"], "2"], TraditionalForm], "errors" -> {}, "input" -> "r^{2}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4.75,2.75}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox["-1", TraditionalForm], "errors" -> {}, "input" -> "-1", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4.35,1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{"2", "(", 
RowBox[{"1", "-", 
SuperscriptBox[
StyleBox["r", "TI"], "2"]}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "2(1-r^{2})", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,2.35},Background->White],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
RowBox[{
StyleBox["j", "TI"], "-", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j-1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,1},{0,1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "-", "1"}], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i-1}^{j}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{3,2},{1.5,0}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,2},{1.5,1.25}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "+", "1"}], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i+1}^{j}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{5,2},{-1.5,0}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
RowBox[{
StyleBox["j", "TI"], "+", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j+1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,3},{0,-1}]},Axes->True,AxesLabel->{"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
StyleBox["x", "TI"], TraditionalForm], "errors" -> {}, "input" -> "x", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)","\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
StyleBox["t", "TI"], TraditionalForm], "errors" -> {}, "input" -> "t", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},Ticks->{{{3,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["i", "TI"], "-", "1"}], ")"}], 
StyleBox["h", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(i - 1) h", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"},{4,"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["i", "TI"], 
StyleBox["h", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "ih", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},{5,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["i", "TI"], "+", "1"}], ")"}], 
StyleBox["h", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(i + 1) h", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"}},{{1,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["j", "TI"], "-", "1"}], ")"}], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(j - 1) k", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"},{2,"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["j", "TI"], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "jk", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},{3,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["j", "TI"], "+", "1"}], ")"}], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(j + 1) k", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"}}},PlotRange->{{0,8},{0,4}},BaseStyle->FontSize->12]

Export["explicitwave.pdf",%,"PDF"]

Export["explicitwave.eps",%%,"EPS"]

Example

Clear[u,f,g]

f[x_]:=Piecewise[{{0,0≤x≤1/4},{x-1/4,1/4≤x≤3/8},{1/2-x,3/8≤x≤1/2},{0,1/2≤x≤1}}]

g[x_]:=0

h=1/20;k=1/20;r=k/h

ic=Join[{0},Table[f[x],{x,h,1-h,h}],{0}]

N[ic]

Show[Plot[f[x],{x,0,1},PlotRange->{-0.01,1/8}],ListPlot[Transpose[{Range[0,1,h],ic}]],AxesLabel->{"x","f(x)"},PlotRange->All]

u1=Join[{0},ListConvolve[{(r^2),2(1-(r^2)),(r^2)}/2,ic],{0}]

N[u1]

Join[{0},ListConvolve[{(r^2),2(1-(r^2)),(r^2)},u1],{0}]-ic

N[%]

NestList[{Last[#],Join[{0},ListConvolve[{(r^2),2(1-(r^2)),(r^2)},Last[#]],{0}]-First[#]}&,{ic,u1},5]

NestList[{Last[#],Join[{0},ListConvolve[{(r^2),2(1-(r^2)),(r^2)},Last[#]],{0}]-First[#]}&,{ic,u1},5][[All,1]]

ListLinePlot[%]

Map[Transpose[{Range[0,1,h],#}]&,%%]

result=Map[Transpose[{Range[0,1,h],#}]&,NestList[{Last[#],Join[{0},ListConvolve[{(r^2),2(1-(r^2)),(r^2)},Last[#]],{0}]-First[#]}&,{ic,u1},20][[1;;-1;;4,1]]];

ListLinePlot[result,AxesLabel->{"x","u(x,t)"}]

TeXForm[TableForm[Round[N[Transpose[Map[Transpose[#][[2]]&,result]]],0.0001]]]

PeriodicExtension[f_,L_][x_]:=f[Mod[x,2L,-L]]

EvenExtension[f_,L_][x_]:=PeriodicExtension[Piecewise[{{f[#],0≤#≤L},{f[-#],-L≤#≤0}}]&,L][x]

OddExtension[f_,L_][x_]:=PeriodicExtension[Piecewise[{{f[#],0≤#≤L},{-f[-#],-L≤#≤0}}]&,L][x]

u[x_,t_]:=(OddExtension[f,1][x+t]+OddExtension[f,1][x-t])/2

fncs[x_]:=Table[u[x,t],{t,1/5,1,1/5}]

labels={"u(x,0)","u(x,1/4)","u(x,1/2)","u(x,3/4)","u(x,1)"};

Show[ListPlot[result,PlotTheme->"Monochrome",PlotRange->{-1,1}/8],Plot[Evaluate[fncs[x]],{x,0,1},PlotTheme->{"Monochrome"},PlotRange->{-1,1}/8],Epilog->{Table[Inset[labels[[i]],{i/6,fncs[i/6][[i]]},Background->White],{i,5}]},AxesLabel->{"x","u"},PlotRange->{-1,1}/8]

Dimensions[result]

ts=Table[t,{t,0,1,1/5},{21}]

pts=Flatten[Map[Transpose,Transpose[{result[[All,All,1]],ts,result[[All,All,2]]}]],1];

exact=Flatten[Table[{x,t,u[x,t]},{x,0,1,h/10},{t,0,1,k/10}],1];

ListPlot3D[exact,PlotRange->{-1,1}/8,MeshFunctions->{#2&},PlotTheme->"Monochrome",PlotStyle->Opacity[1/2]]

ListPointPlot3D[pts]

Show[ListPlot3D[exact,PlotRange->{-1,1}/8,MeshFunctions->{#2&},Mesh->{Range[10]/10},PlotTheme->"Monochrome",ColorFunction->GrayLevel,PlotStyle->Opacity[1/2]],ListPointPlot3D[pts,PlotTheme->"Monochrome",PlotStyle->PointSize[Medium]],AxesLabel->{"\!\(\*TemplateBox[<|"boxes" -> FormBox[
StyleBox["x", "TI"], TraditionalForm], "errors" -> {}, "input" -> "x", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)","\!\(\*TemplateBox[<|"boxes" -> FormBox[
StyleBox["t", "TI"], TraditionalForm], "errors" -> {}, "input" -> "t", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)","\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", 
StyleBox["t", "TI"]}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, t)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"},BaseStyle->FontSize->12]

Export["explicitwavexmp.pdf",%,"PDF"]

Export["explicitwavexmp.eps",%%,"EPS"]

Implicit Formula

Graphics[{PointSize[Medium],Point[{{3,1},{4,1},{5,1},{3,2},{4,2},{5,2},{3,3},{4,3},{5,3}}],Dashed,Line[{{3,1},{5,1}}],Line[{{3,2},{5,2}}],Line[{{3,3},{5,3}}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "-", "1"}], 
RowBox[{
StyleBox["j", "TI"], "-", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_ {i - 1}^{j - 1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{3,1},{1.5,0}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
RowBox[{
StyleBox["j", "TI"], "-", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j-1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,1},{0,1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "+", "1"}], 
RowBox[{
StyleBox["j", "TI"], "-", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_{i+1}^{j-1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{5,1},{-1.5,0}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "-", "1"}], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i-1}^{j}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{3,2},{1.5,0}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,2},{0,1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "+", "1"}], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i+1}^{j}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{5,2},{-1.5,0}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "-", "1"}], 
RowBox[{
StyleBox["j", "TI"], "+", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_{i-1}^{j+1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{3,3},{1.5,0}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
RowBox[{
StyleBox["j", "TI"], "+", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j+1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,3},{0,-1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
RowBox[{
StyleBox["i", "TI"], "+", "1"}], 
RowBox[{
StyleBox["j", "TI"], "+", "1"}]], TraditionalForm], "errors" -> {}, "input" -> "u_{i+1}^{j+1}", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{5,3},{-1.5,0}]},Axes->True,AxesLabel->{"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
StyleBox["x", "TI"], TraditionalForm], "errors" -> {}, "input" -> "x", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)","\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
StyleBox["t", "TI"], TraditionalForm], "errors" -> {}, "input" -> "t", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},Ticks->{{{3,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["i", "TI"], "-", "1"}], ")"}], 
StyleBox["h", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(i - 1) h", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"},{4,"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["i", "TI"], 
StyleBox["h", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "ih", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},{5,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["i", "TI"], "+", "1"}], ")"}], 
StyleBox["h", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(i + 1) h", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"}},{{1,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["j", "TI"], "-", "1"}], ")"}], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(j - 1) k", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"},{2,"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["j", "TI"], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "jk", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},{3,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["j", "TI"], "+", "1"}], ")"}], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(j + 1) k", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"}}},PlotRange->{{0,8},{0,4}},BaseStyle->FontSize->12]

Export["boxwave.pdf",%,"PDF"]

Export["boxwave.eps",%%,"EPS"]

Clear[u,x,t]

nsoln=First[NDSolve[{D[u[x,t],t,t]==D[u[x,t],x,x],u[0,t]==0,u[1,t]==0,u[x,0]==x(1-x),Derivative[0,1][u][x,0]==Sin[5π x]},u,{x,0,1},{t,0,1/2},MaxStepSize->0.01]]

Plot3D[u[x,t]/.nsoln,{x,0,1},{t,0,1/2},AxesLabel->{"x","t","u(x,t)"}]

Clear[n,h,k,r,matA,matB]

n=10;h=1.0/n;k=1.0/(2n);

n=10;h=1.0/n;k=1.0/(2n);r=k/h

f[x_]:=x(1-x)

g[x_]:=Sin[5π x]

matA[r_]:=SparseArray[{Band[{1,1}]->Table[2(2+r),{n-1}],Band[{2,1}]->Table[-r,{n-2}],Band[{1,2}]->Table[-r,{n-2}]},n-1]

matB[r_]:=SparseArray[{Band[{1,1}]->Table[4(2-r),{n-1}],Band[{2,1}]->Table[2r,{n-2}],Band[{1,2}]->Table[2r,{n-2}]},n-1]

MatrixForm[matA[(r^2)]]

MatrixForm[matB[(r^2)]]

matB[(r^2)]==2matA[-(r^2)]

u0=f[Range[h,1-h,h]]

u1=Inverse[matA[(((k/h))^2)]].(2matA[-(((k/h))^2)].f[Range[h,1-h,h]]/2+k matA[(((k/h))^2)].g[Range[h,1-h,h]])

soln=NestList[{Last[#],Inverse[matA[(((k/h))^2)]].(2matA[-(((k/h))^2)].Last[#]-matA[(((k/h))^2)].First[#])}&,{u0,u1},10][[All,1]];

Table[u[x,t]/.nsoln,{t,0,10k,k},{x,h,1-h,h}];

ListPlot[%-soln]

MatrixForm[matA[(((k/h))^2)]]

MatrixForm[2matA[-(((k/h))^2)]]

Round[u1,0.0001]

Map[List,Join[Transpose[{Table[0.0,{n+1}]}],soln,Transpose[{Table[0.0,{n+1}]}],2],{2}];

Table[{x,t},{t,0,n k,k},{x,0,1,h}];

xtu=Join[%,%%,3];

Show[Plot3D[u[x,t]/.nsoln,{x,0,1},{t,0,1/2},PlotStyle->Directive[Opacity[4/5],Gray,Specularity[White,50]],PlotTheme->"Monochrome",MeshFunctions->{#2&},MeshStyle->Thin,Mesh->{Table[t,{t,0,0.50,k}]},PlotTheme->"Monochrome"],ListPointPlot3D[xtu,PlotTheme->"Monochrome"],AxesLabel->{"\!\(\*TemplateBox[<|"boxes" -> FormBox[
StyleBox["x", "TI"], TraditionalForm], "errors" -> {}, "input" -> "x", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)","\!\(\*TemplateBox[<|"boxes" -> FormBox[
StyleBox["t", "TI"], TraditionalForm], "errors" -> {}, "input" -> "t", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)","\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", 
StyleBox["t", "TI"]}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, t)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"},BaseStyle->FontSize->12]

Export["exampleboxwave.pdf",%,"PDF"]

Export["exampleboxwave.eps",%%,"EPS"]

TeXForm[TableForm[Round[Join[Transpose[{Table[0.0,{n+1}]}],soln,Transpose[{Table[0.0,{n+1}]}],2],0.0001]]]