Heat equation example

Clear[f,n,a]

f[x_]=x/l

vdotv=Integrate[Sin[n Pi/l x]^2,{x,0,l},Assumptions->Element[n,Integers]]

fdotv=Integrate[f[x] Sin[n Pi/l x],{x,0,l},Assumptions->Element[n,Integers]]

a[n_]=fdotv/vdotv

u[x_,t_,l_,m_]=Sum[a[n]Exp[-(n Pi/l)^2 t]Sin[n Pi/l x],{n,1,m}]

Plot[u[x,0,1,20],{x,0,1}]

Animate[Plot[u[x,t,1,20],{x,0,1},PlotRange->{0,1.5}],{t,0,0.2},AnimationRunning->False]

Wave equation example

Clear[f,x,a,n]

f[x_]=Piecewise[{{x/5,0<x<1/2},{(1-x)/5,1/2<x<1}}]

Integrate[f[x]Sin[n Pi x],{x,0,1},Assumptions->Element[n,Integers]]

u[x_,t_,m_]:=Sum[((2 Sin[((n π)/2)])/(5 (n^2) (π^2)))Cos[n Pi t]Sin[n Pi x],{n,1,m}]

Plot[u[x,0,31],{x,0,1}]

Animate[Plot[u[x,t,31],{x,0,1},PlotRange->{-0.05,0.05}],{t,0,2}]