% Solutions to heat flow in 1-D rod length L
format compact;
L = 10;			% length of rod
Temp = 100;		% Constant initial temperature
tfin = 20;		% final time
k = 1;			% heat coef of the medium
NptsX=151;      % number of x pts
NptsT=151;      % number of t pts
Nf=200;         % number of Fourier terms
x=linspace(0,L,NptsX);
t=linspace(0,tfin,NptsT);
[X,T]=meshgrid(x,t);
b=zeros(1,Nf);
U=zeros(NptsT,NptsX);
for n=1:Nf
    b(n)=(2*Temp/(n*pi))*(1-(-1)^n);  % Fourier coefficients
    Un=b(n)*exp(-(n*pi*k/L)^2*T).*sin(n*pi*X/L);  % Temperature(n)
    U=U+Un;
end
set(gca,'FontSize',[14]);
surf(X,T,U);
shading interp
colormap(jet)
xlabel('$x$','Fontsize',14,'interpreter','latex'); 
ylabel('$t$','Fontsize',14,'interpreter','latex'); 
zlabel('$u(x,t)$','Fontsize',14,'interpreter','latex');
axis tight; 
view([120 10]);
print -depsc heat_surf.eps
