clc; clear; close all;

print_figures = 0;

N = 5; % stupen polynomu repezentujici reseni na elementu
n = N + 1; % pocet uzlu na elementu

nodes = GaussLobattoNodes(n); % uzly Gauss-Lobattovovy integracni formule
L = min(nodes); R = max(nodes); dx = L:0.01:R;


format short E; % v Matlabu je spravny format shortE


LegPols = LegendrePolynomialsNormalized(n - 1);
for i = 1:n
    pol = LegPols{i};
    
    figure;
    
    plot(dx, polyval(pol,dx), 'LineWidth', 1.5);
    
    %title(['P_',num2str(i - 1),'(r)']);
    axis([-1, 1, -2.5, 2.5]); xlabel('x'); ylabel('y');
    grid on;
    
    filename_string = ['1D_modal_basis_for_N=',num2str(N),'_and_i=',num2str(i - 1)];
    if print_figures == 1
        saveas(gcf,filename_string,'pdf');
        saveas(gcf,filename_string,'png');
    end
    
    close;
end
disp('Bazove ortonormalni (Legendrovy) polynomy dle definice:');
myCellDisp(LegPols);
disp('');


LagPols = LagrangePolynomialBasis(nodes); % Lagrange polynomials for interpolation
for i = 1:n
    pol = LagPols{i};
    
    figure;
    
    plot(nodes, zeros(1,n), 'o', 'Color', 'black', 'LineWidth', 1.5); hold on;
    plot(nodes(i), 1, 'o', 'Color', 'red', 'LineWidth', 1.5); hold on;
    plot(dx, polyval(pol, dx), 'LineWidth', 1.5);
    
    %title(['l_',num2str(i),'(r)']);
    axis([-1, 1, -1, 2]); xlabel('x'); ylabel('y');
    grid on;
    
    filename_string = ['1D_nodal_basis_for_N=',num2str(N),'_and_i=',num2str(i - 1)];
    if print_figures == 1
        saveas(gcf,filename_string,'pdf');
        saveas(gcf,filename_string,'png');
    end
    
    close;
end
disp('Bazove Lagrangeovy polynomy dle definice:');
myCellDisp(LagPols);
disp('');


V = VandermondeMatrix1D(nodes);
% prechod od nodalni k modalni reprezentaci
T = V'; % transformacni matice
LegPols2 = cell(n,1);
for i = 1:n
    LegPols2{i} = T(i,1) * LagPols{1};
    if n > 1
        for j = 2:n
            LegPols2{i} = LegPols2{i} + T(i,j) * LagPols{j};
        end
    end
end
disp('Bazove ortonormalni (Legendrovy) polynomy spocteny pomoci Lagrangeovych polynomu a Vandermondovy matice:');
myCellDisp(LegPols2);
disp('');


% prechod od modalni k nodalni reprezentaci
T = inv(V'); % transformacni matice
LagPols2 = cell(n,1);
for i = 1:n
    temp = LegPols{1};
    if length(temp) ~= n
        temp = LeftZeroFill(temp,n);
    end
    LagPols2{i} = T(i,1) * temp;
    if n > 1
        for j = 2:n
            temp = LegPols{j};
            if length(temp) ~= n
                temp = LeftZeroFill(temp,n);
            end
            LagPols2{i} = LagPols2{i} + T(i,j) * temp;
        end
    end
end
disp('Bazove Lagrangeovy polynomy spocteny pomoci ortonormalnich (Legendrovych) polynomu a Vandermondovy matice:');
myCellDisp(LagPols2);
disp('');

format short; % the default


% pokus, zda ortogonalizaci standartni baze 1,x,x^2,x^3,... dostaneme vazne normalizovane Legendrovy polynom
N = 5;
polynomials = zeros(N + 1);
for i = 1:(N + 1)
    polynomials(i,(N + 1) - i + 1) = 1;
end
disp('Ortonormalni baze pomoci Gram-Schmidtova algoritmu (maji vyjit normlaizovane Legendrovy polynomy)');
orthonormalized = PolynomialGramSchmidtOrthonormalization(polynomials)