4.7 Solve the 1-D heat partial differential equation (PDE)

The PDE is

\[ \frac {\partial T\left ( x,t\right ) }{\partial t}=k\frac {\partial ^{2}T\left ( x,t\right ) }{\partial x^{2}} \]

Problem: given a bar of length \(L\) and initial conditions \(T\left ( x,0\right ) =\sin \left ( \pi x\right ) \) and boundary conditions \(T\left ( 0,t\right ) =0,T\left ( L,t\right ) =0\), solve the above PDE and plot the solution on 3D.

Use bar length of \(4\) and \(k=0.5\) and show the solution for \(1\) second.

Mathematica

Clear[y,x,t]; 
barLength = 4; 
timeDuration = 1; 
eq = D[y[x, t], t] == k*D[y[x, t], x, x]; 
eq = eq /. k -> 0.5; 
boundaryConditions = {y[0,t]==0, 
                     y[barLength,t]==0}; 
initialConditions = y[x,0]==Sin[Pi x]; 
sol=First@NDSolve[{eq, 
 boundaryConditions,initialConditions}, 
 y[x,t],{x,0,barLength},{t,0,timeDuration}]; 
y = y[x,t]/.sol 
Plot3D[y,{x,0,barLength},{t,0,timeDuration}, 
  PlotPoints->30,PlotRange->All, 
  AxesLabel->{"x","time","T[x,t]"}, 
  ImageSize->300]
 

PIC

ContourPlot[y,{x,0,barLength}, 
  {t,0,timeDuration},PlotPoints->15, 
  Contours->15,ContourLines->False, 
  ColorFunction->Hue,PlotRange->All, 
  ImageSize->300]
 

PIC

 

Matlab

function e57 
m = 0; 
x = linspace(0,4,30); 
t = linspace(0,1,30); 
 
sol = pdepe(m,@pde, 
        @pdeInitialConditions,... 
        @pdeBoundaryConditions,x,t); 
 
u = sol(:,:,1); 
 
surf(x,t,u) 
title('Numerical PDE solution') 
xlabel('x'); ylabel('Time t') 
set(gcf,'Position',[10,10,320,320]); 
% ---------------- 
function [c,f,s] = pde(x,t,u,DuDx) 
k=0.5; 
c = 1/k; 
f = DuDx; 
s = 0; 
% --------------- 
function T0 = pdeInitialConditions(x) 
T0 = sin(pi*x); 
% --------------- 
function [pl,ql,pr,qr] = ... 
   pdeBoundaryConditions(xl,ul,xr,ur,t) 
pl = ul; 
ql = 0; 
pr = 0; 
qr = 1;
 

PIC