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

close all, clear all;

L=2
alpha=1;
x=linspace(0,L,100);
len=length(x);
y=x;

ul=0;
ur=1000;

y(1:len/2)=ul;
y(len/2+1:end)=ur;
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,ul,ur);
    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,ul,ur)
u0=zeros(length(x),1)';
for n=1:nTerms
    wn=n*pi/L;
    if x<L
       u0=u0 + 2*ur/(wn*L)*(cos(wn*L)-1)*(sin(n*pi*x/L));
    else
      u0=u0 + ur/wn*(cos(2*wn*L)-cos(wn*L))/(L/2- (1/4*wn)*(cos(4*wn*L)-sin(2*L*wn))) * sin(n*pi*x/L);
    end
end
