function nma_HW1_part3_MAE200B
%Plot u(t,x) in  increments from 0 to .4 seconds in
%increments of 0.1

close all, clear all;

L=1
alpha=1;
x=linspace(0,L,100);
t=linspace(0,1,5);

y=x;
len=length(x);
y(1:len/2)=x(1:len/2);
y(len/2+1:end)=L-x(len/2+1:end);
%plot(x,y,'LineWidth',2.5);
%hold on;

col={'r','b','m','k','g'};
str={'t=0','t=0.1','t=0.2','t=0.3','t=0.4'};

i=1;

maxN=55 %calculated from part2 of HW1.

for j=1:length(t)
    sol=getApproximateSolution(x,L,maxN,alpha,maxN,t(j));
    plot(x,sol,col{i});
    hold on;
    i=i+1;
end

legend('t=0','t=0.1','t=0.2','t=0.3','t=0.4');
title('final solution for different times, all using n=55');
xlabel('x');
ylabel('U(t,x)');


function sol=getApproximateSolution(x,L,nTerms,maxN,alpha,t)
sol=zeros(length(x),1)';
for n=1:nTerms
    sol=sol + 4*L*sin(n*pi/2)/(n*pi)^2 .* sin(n*pi*x) .* exp(-(n*pi)^2*t);
end
    