Heat Equation with a Source

Spatial Source

In this section we want to solve the following heat equation with a spatial source term:
u_t=u_xx+sin^2(x)
u(0,t)=0
u(π,t)=0
u(x,0)=sin(3*x)

Steady State Solution

us[x_]:=-x^2/4-Cos[2*x]/8+π/4*x+1/8;

Plot[us[x],{x,0,π},PlotStyle->Thickness[.01],PlotRange->All,AxesLabel->{"x","(SuperStar[u])(x)"},LabelStyle->Directive[Bold, Black,Medium],AxesStyle->Directive[Black, 24]]

Calculation of Fourier Coefficients

2/π*Integrate[(Sin[3*x]+x^2/4+Cos[2*x]/8-π/4*x-1/8)*Sin[n*x],{x,0,π},Assumptions->n∈Integers]

bn[n_]:=((Sin[((n π)/2)] (((n ((n^2) (-12+π)-9 π) Cos[((n π)/2)])/(-9+(n^2)))+((8 Sin[((n π)/2)])/(-4+(n^2)))))/((n^3) π));

The above formula only works if n≃2 or n≃3. We need to do these cases separately. We might as well do n=1 separately as well.

n=1;
2/π*Integrate[(Sin[3*x]+x^2/4+Cos[2*x]/8-π/4*x-1/8)*Sin[n*x],{x,0,π}]
Clear[n]

u1[x_,t_]:=-(8/(3 π))*Exp[-9*t]*Sin[x];

n=2;
2/π*Integrate[(Sin[3*x]+x^2/4+Cos[2*x]/8-π/4*x-1/8)*Sin[n*x],{x,0,π}]
Clear[n]

n=3;
2/π*Integrate[(Sin[3*x]+x^2/4+Cos[2*x]/8-π/4*x-1/8)*Sin[n*x],{x,0,π}]
Clear[n]

u3[x_,t_]:=((2 ((4/135)+(π/2)))/π)*Exp[-9*t]*Sin[3*x];

v[x_,t_,M_]:=u1[x,t]+u3[x,t]+Sum[bn[n]*Exp[-n^2*t]*Sin[n*x],{n,4,M}]

Plot of Full Solution

u[x_,t_,M_]:=v[x,t,M]+us[x];

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

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

Movie of Solution

Animate[Plot[u[x,t,M],{x,0,π},PlotRange->{-1,1},PlotStyle->Thickness[.01],PlotRange->All,AxesLabel->{"x","u(x,t)"},LabelStyle->Directive[Bold, Black,Medium],AxesStyle->Directive[Black, 12]],{t,0,1/2}]

Spatio-Temporal Source

In this section we want to solve the following heat equation with a spatial source term:
u_t=u_xx+sin(x)(cos^2)(t)
u(0,t)=0
u(π,t)=0
u(x,0)=0

Auxiliary Solution

From class we know that:

w[x_,t_,τ_]:=Cos[τ]^2*Exp[-t]*Sin[x];

Full Solution

To determine the actual solution we integrate

Integrate[w[x,t-τ,τ],{τ,0,t}]

u[x_,t_]:=(1/10) (5-6 (E^(-t))+Cos[2 t]+2 Sin[2 t]) Sin[x]

Plot of Full Solution

T=15;
DensityPlot[u[x,t], {x, 0, π}, {t, 0, T}, PlotRange->{0,1}, PlotLegends->Automatic, PlotPoints-> 30, FrameLabel->{"x","t"}, LabelStyle->Directive[Bold, Black, Medium],ColorFunction->"Rainbow"]

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

Movie of Solution

Animate[Plot[u[x,t],{x,0,π},PlotRange->{0,1},PlotStyle->Thickness[.01],PlotRange->All,AxesLabel->{"x","u(x,t)"},LabelStyle->Directive[Bold, Black,Medium],AxesStyle->Directive[Black, 12]],{t,0,10}]