function priklad2GalerkinovyMetody()

% -k*u''(x) = f(x) v (0,1)
%      u(0) = 0
%   k*u'(1) = tau

f = @(x) (exp(-2*cos(10*x)));
k = 1; tau = 0; %3;

% phi = @(i,x) (sin(i*x));
% dphi = @(i,x) (i*cos(i*x));

% phi = @(i,x) (exp(i*x)-1);
% dphi = @(i,x) (i*exp(i*x));

phi = @(i,x) (x.^i);
dphi = @(i,x) (i*x.^(i-1));


figure
x = linspace(0,1,100);
for n = 1:100
    n
    A = zeros(n,n); b = zeros(n,1);
    for i=1:n
        for j=1:n
            A(i,j) = k * numerickaKvadratura(@(x)(dphi(i,x).*dphi(j,x)),0,1,100);
        end
        b(i) = numerickaKvadratura(@(x)(f(x).*phi(i,x)),0,1,100) + tau*phi(i,1);
    end
    alfa = A\b;
    
    clf
    hold off
    un = 0*x;
    for j=1:n
        un = un + alfa(j) * phi(j,x);
    end

    hold on
    plot(x,un)
    pause
end

end


function IN = numerickaKvadratura(f,a,b,N)
x = linspace(a,b,N);
IN = (b-a) * ( sum(f(x)) - f(a)/2 - f(b)/2 );
end
