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

The Heat Equation

Graphics[{PointSize[Medium],Point[{{3,1},{4,1},{5,1},{4,2}}],Dashed,Line[{{3,1},{4,2}}],Line[{{4,1},{4,2}}],Line[{{5,1},{4,2}}],Text["\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
StyleBox["r", "TI"], TraditionalForm], "errors" -> {}, "input" -> "r", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)",{3.25,1.5}],Text["\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
StyleBox["r", "TI"], TraditionalForm], "errors" -> {}, "input" -> "r", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)",{4.75,1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{"1", "-", "2", 
StyleBox["r", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "1 - 2 r", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{4,1.3},Background->White],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,1},{0,1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j}", "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"]\)",{5,1},{0,1.5}],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,2},{0,-1.5}]},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,"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["j", "TI"], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "jk", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},{2,"\!\(\*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,3}},BaseStyle->FontSize->12]

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

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

Example

h=0.1;k=0.001;r=k/(h^2)

f[x_]:=100.0

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

nsoln=NestList[Join[{0.0},ListConvolve[{r,1-2r,r},#],{0.0}]&,ic,60];

xs=Table[Range[0,1,h],{61}];

xu=Map[Transpose,Partition[Riffle[xs,nsoln],2,2]];

tu=Join[Transpose[{Range[0,60k,k]}],nsoln,2];

Clear[u]

u[n_Integer][x_,t_]:=400Sum[Exp[-(((2k-1))^2)(π^2)t]Sin[(2k-1)π x]/(2k-1),{k,1,n}]/π

Show[Plot[Evaluate[Table[u[50][x,t],{t,{12,36,60}k}]],{x,0,1}],ListPlot[xu[[1+{12,36,60}]]],AxesLabel->{"\!\(\*TemplateBox[<|"boxes" -> FormBox[
StyleBox["x", "TI"], TraditionalForm], "errors" -> {}, "input" -> "x", "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"]\)"},Epilog->{Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.012"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.012)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.2,85},Background->White],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.036"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.036)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.75,65},Background->White],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.060"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.060)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.3,50},Background->White]},AspectRatio->1/2,BaseStyle->FontSize->12]

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

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

Example

h=0.1;k=0.004;r=k/(h^2)

f[x_]:=100.0

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

nsoln=NestList[Join[{0.0},ListConvolve[{r,1-2r,r},#],{0.0}]&,ic,15];

xs=Table[Range[0,1,h],{16}];

xu=Map[Transpose,Partition[Riffle[xs,nsoln],2,2]];

tu=Join[Transpose[{Range[0,15k,k]}],nsoln,2];

Clear[u]

u[n_Integer][x_,t_]:=400Sum[Exp[-(((2k-1))^2)(π^2)t]Sin[(2k-1)π x]/(2k-1),{k,1,n}]/π

Show[Plot[Evaluate[Table[u[50][x,t],{t,{12,36,60}k/4}]],{x,0,1}],ListPlot[xu[[1+{12,36,60}/4]]],AxesLabel->{"\!\(\*TemplateBox[<|"boxes" -> FormBox[
StyleBox["x", "TI"], TraditionalForm], "errors" -> {}, "input" -> "x", "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"]\)"},Epilog->{Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.012"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.012)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.2,85},Background->White],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.036"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.036)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.75,65},Background->White],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.060"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.060)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.3,50},Background->White]},AspectRatio->1/2,BaseStyle->FontSize->12]

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

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

Example

h=0.1;k=0.006;r=k/(h^2)

f[x_]:=100.0

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

nsoln=NestList[Join[{0.0},ListConvolve[{r,1-2r,r},#],{0.0}]&,ic,10];

xs=Table[Range[0,1,h],{11}];

xu=Map[Transpose,Partition[Riffle[xs,nsoln],2,2]];

tu=Join[Transpose[{Range[0,10k,k]}],nsoln,2];

Clear[u]

u[n_Integer][x_,t_]:=400Sum[Exp[-(((2k-1))^2)(π^2)t]Sin[(2k-1)π x]/(2k-1),{k,1,n}]/π

Show[Plot[Evaluate[Table[u[50][x,t],{t,{12,36,60}k/6}]],{x,0,1}],ListPlot[xu[[1+{12,36,60}/6]]],AxesLabel->{"\!\(\*TemplateBox[<|"boxes" -> FormBox[
StyleBox["x", "TI"], TraditionalForm], "errors" -> {}, "input" -> "x", "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"]\)"},Epilog->{Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.012"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.012)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.2,85},Background->White],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.036"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.036)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.75,65},Background->White],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["u", "TI"], "(", 
RowBox[{
StyleBox["x", "TI"], ",", "0.060"}], ")"}], TraditionalForm], "errors" -> {}, "input" -> "u (x, 0.060)", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)",{0.3,50},Background->White]},AspectRatio->1/2,BaseStyle->FontSize->12]

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

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

Crank-Nicolson

Graphics[{PointSize[Medium],Point[{{3,1},{4,1},{5,1},{3,2},{4,2},{5,2}}],Dashed,Line[{{3,1},{5,1}}],Line[{{3,2},{5,2}}],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,1},{0,1.5}],Text["\!\(\*TemplateBox[<|"boxes" -> FormBox[
SubsuperscriptBox[
StyleBox["u", "TI"], 
StyleBox["i", "TI"], 
StyleBox["j", "TI"]], TraditionalForm], "errors" -> {}, "input" -> "u_{i}^{j}", "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"]\)",{5,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"]\)",{3,2},{0,-1.5}],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,2},{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,2},{0,-1.5}]},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,"\!\(\*StyleBox[TemplateBox[<|"boxes" -> FormBox[
RowBox[{
StyleBox["j", "TI"], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "jk", "state" -> "Boxes"|>,"TeXAssistantTemplate"],FontSlant->"Italic"]\)"},{3/2,"\!\(\*TemplateBox[<|"boxes" -> FormBox[
RowBox[{
RowBox[{"(", 
RowBox[{
StyleBox["j", "TI"], "+", "1", "/", "2"}], ")"}], 
StyleBox["k", "TI"]}], TraditionalForm], "errors" -> {}, "input" -> "(j + 1/2) k", "state" -> "Boxes"|>,"TeXAssistantTemplate"]\)"},{2,"\!\(\*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,3}},BaseStyle->FontSize->12]

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

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

Clear[u]

k=0.01;h=0.1;r=k/(h^2)

nsoln=First[NDSolve[{D[u[x,t],t]==D[u[x,t],x,x],u[0,t]==0,u[1,t]==0,u[x,0]==x(((1-x))^2)},u,{x,0,1},{t,0,1/10}]]

data=MapIndexed[Transpose[{Table[x,{x,0,1,h}],Table[k(First[#2]-1),{x,0,1,h}],#}]&,Import["tmp.txt","Table"]];

Show[Plot3D[u[x,t]/.nsoln,{x,0,1},{t,0,1/10},PlotStyle->Directive[Opacity[1/2],Gray,Specularity[White,50]],MeshFunctions->{#2&},MeshStyle->Thin,Mesh->{Table[t,{t,0,0.10,k}]}],ListPointPlot3D[data],PlotRange->All,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["example01.eps",%,"EPS"]

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