Heat Equation on the Line

Heat Equation for a Gaussian

This code gives an exact solution to the heat equation u_t=u_xx with u(x,0)=e^{-x^2}. We are going to plot the solution at multiple, create a contour plot, and create a movie.

Calculate the Exact Solution

We know that the solution in Fourier space is uhat(k)=fhat(k)e^{-k^2t}.

Integrate[Exp[-x^2]*Exp[I*k*x],{x,-∞,∞}]

1/(2*π)*Integrate[(E^(-((k^2)/4))) (Sqrt[π])*Exp[-k^2*t]*Exp[-I*k*x],{k,-∞,∞},Assumptions->t>0]

u[x_,t_]:=((E^(-((x^2)/(1+4 t))))/(Sqrt[1+4 t]));

Plotting the Solutions on one graph

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

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

ContourPlot[u[x,t],{x,-10,10},{t,0,10},PlotRange->{0,1},PlotLegends->Automatic,PlotPoints->100,FrameLabel->{"x","t"},LabelStyle->Directive[Bold,Black, Medium],Contours->20,ColorFunction->"Rainbow"]

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