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

The PDE is

Problem: given a bar of length and initial conditions and boundary conditions , solve the above PDE and plot the solution on 3D.

Use bar length of and and show the solution for second.

 Mathematica Matlab 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 Out[138]= InterpolatingFunction[ {{0.,4.},{0.,1.}},<>][x,t] Plot3D[y,{x,0,barLength},{t,0,timeDuration}, PlotPoints->30, PlotRange->All, AxesLabel->{"x","time","T[x,t]"}, ImageSize->300]  ContourPlot[y,{x,0,barLength},{t,0,timeDuration}, PlotPoints->15, Contours->15, ContourLines->False, ColorFunction->Hue, PlotRange->All, ImageSize->300]  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; 

me 2013-01-09