Heat Equation with Dirichlet Boundary Conditions

Consider the Equation u_t=γ*u_xx on the domain [0,L] with Dirichlet boundary conditions and initial profile u(0,x)=f(x). We know that the generic form of the solution is given by
	u(t,x)=(∑_(n=1)^∞)(Subscript[b, n]) exp(((-(n^2)(π^2)γ)/(L^2))t)sin(((nπx)/L))
We want to plot the generic behavior of such solutions for different functions.

Example

Lets consider the problem on the domain [0,2] with γ=3 and initial profile f(x)=x(2-x).

L=2;
γ=3;
f[x_]:=x*(2-x);

Simplify[2/L*Integrate[f[x]*Sin[n*π*x/L],{x,0,L}],Assumptions->n∈Integers]

b[n_]:=-((16 (-1+(((-1))^n)))/((n^3) (π^3)))
u[t_,x_,N_]:=Sum[b[n]*Exp[-n^2*π^2*γ/L^2*t]*Sin[n*π*x/L],{n,1,N}];

Plot[{u[0,x,20],u[.04,x,20],u[.08,x,20],u[.12,x,20],u[.16,x,20],u[.2,x,20]},{x,0,L},PlotStyle->Thick,AxesStyle->Directive[Black, 12],AxesLabel->{"x","u(t,x)"}]

ContourPlot[u[t,x,20],{t,0,1},{x,0,2},PlotRange->{0,1},FrameStyle->Directive[Black, 12],FrameLabel->{"t","x"}]

Heat Equation with Neumann Boundary Conditions

Consider the Equation u_t=γ*u_xx on the domain [0,L] with Neumann boundary conditions and initial profile u(0,x)=f(x). We know that the generic form of the solution is given by
	u(t,x)=((Subscript[a, 0])/2)+(∑_(n=1)^∞)(Subscript[a, n]) exp(((-(n^2)(π^2)γ)/(L^2))t)cos(((nπx)/L))
We want to plot the generic behavior of such solutions for different functions.

Example

Lets consider the problem on the domain [0,2] with γ=3 and initial profile f(x)=x(2-x).

L=2;
γ=3;
f[x_]:=x*(2-x);

Simplify[2/L*Integrate[f[x],{x,0,L}]]
Simplify[2/L*Integrate[f[x]*Cos[n*π*x/L],{x,0,L}],Assumptions->n∈Integers]

a0=4/3;
a[n_]:=-((8 (1+(((-1))^n)))/((n^2) (π^2)))
u[t_,x_,N_]:=a0/2+Sum[a[n]*Exp[-n^2*π^2*γ/L^2*t]*Cos[n*π*x/L],{n,1,N}];

Plot[{u[0,x,40],u[.02,x,40],u[.04,x,40],u[.06,x,20],u[.08,x,40],u[.1,x,40]},{x,0,L},PlotRange->{0,1},PlotStyle->Thick,AxesStyle->Directive[Black, 12],AxesLabel->{"x","u(t,x)"}]

ContourPlot[u[t,x,20],{t,0,.2},{x,0,2},PlotRange->{0,1},FrameStyle->Directive[Black, 12],FrameLabel->{"t","x"}]