function nma_HW1_MAE200B
%function to plot fourier series expansion approximation to u0

close all, clear all;

L=1
alpha=1;
x=linspace(0,L,100);
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'};
str={'n=1','n=3','n=5','n=7'};

i=1;
for n=1:2:7
    u0=getApproxInitial(x,L,n);
    plot(x,u0,col{i});
    i=i+1;
end

legend('U0','n=1','n=3','n=5','n=7');
title('Approximation to initial conditions by increasing number of terms of fouries series');
xlabel('x');
ylabel('U(0,x)');

function u0=getApproxInitial(x,L,nTerms)
u0=zeros(length(x),1)';
for n=1:nTerms
    u0=u0 + 4*L*sin(n*pi/2)/(n*pi)^2 * sin(n*pi*x);
end
