
clear

X = linspace(0,1,101);
a(1) = 1/2;
for n=2:10
    if mod(n,2)==1
        a(n) = 0;
    else
        a(n) = -(8*n)/((n^2-1)^2*pi^2);
    end
end

figure(1)
plot(1:length(a),a,'-x','linewidth',1,'markersize',12);
axis tight
xlabel('$x$','interpreter','latex')
title('First 10 coefficients from Fourier series','FontWeight', 'Normal')
set(gca,'fontsize',18)

figure(2)
for i=1:10
    v(:,i) = a(i)*sin(i*pi*X);
    plot(X,v(:,i),'linewidth',1);
    hold on;
end
hold off;
axis tight
xlabel('$x$','interpreter','latex')
title('First 10 weighted basis functions','FontWeight', 'Normal')
set(gca,'fontsize',18)

figure(3)
plot(X,X.*sin(pi*X),'k',X,v*ones(10,1),X,v(:,1:3)*ones(3,1),'linewidth',1);
axis tight
xlabel('$x$','interpreter','latex')
title('Initial data and approximation','FontWeight', 'Normal')
l = legend('Initial data','Appr. by Fourier series, $n=10$','Appr. by Fourier series, $n=3$');
set(l,'interpreter','latex','location','southeast')
set(gca,'fontsize',18)

figure(4)
T = linspace(0,4,201);
for i=1:length(T)
    Z(:,i) = v*cos((1:10)'*pi*T(i));
end
mesh(X,T,Z')
axis tight
xlabel('$x$','interpreter','latex')
ylabel('$t$','interpreter','latex')
title('Solution wave equation','FontWeight', 'Normal')
set(gca,'fontsize',18)

figure(5)
for i=1:length(T)
    plot(X,Z(:,i),'k','linewidth',1)
    axis tight
    set(gca,'fontsize',18,'xlim',[0 1],'ylim',[-.5 .5])
    xlabel('$x$','interpreter','latex')
    ylabel('$u$','interpreter','latex')
    M(i) = getframe;
end

movie(M)
