%%%% This script plots the first 500 terms of the Fourier sine series %%%% solution to the heat equation with constant initial condition. %%%%Parameter values are set L=1; k=1; %%%%Intervals for x and t are defined x=linspace(0,L,200); tend=0.75; t=linspace(0,tend,200); %%%%Initial condition is defined IC=23; %%%%First 500 terms of series solution Phi=zeros(1,200); G=zeros(200,1); u=zeros(200); for j=1:2:999 Phi=sin(j*pi*x/L); G=exp(-1*(j*pi/L)^2*k*t.'); u=u+4*IC/(j*pi)*G*Phi; end %%%%Plot solution surface figure(1); surf(x,t,u); shading interp; title('First 500 terms of Analytical Solution of Heat Equation') xlabel('x') ylabel('Time t') %%%%Plot spatial distribution over time figure(2); for m=1:length(t) v=u(m,:); figure(2) plot(x,v,'-k'); title(['Solution at t=',num2str(t(m))],'fontsize',16); xlabel('Distance x', 'fontsize',16) ylabel('u(x)','rotation',0,'fontsize',16) ylim([0,25]); pause(0.04); end