function[koren, iterace] = bisekce3(f, a, b, eps)

%% [koren, iterace] = bisekce3(f, a, b, eps)
%
% Metoda BISEKCE pro odhad korene nelinearni rovnice ve tvaru f(x)=0
%
% VSTUPNI PARAMETRY
% f ... symbolicky retezec
% a, b ... krajni body intervalu
% eps ... chyba
% VYSTUPNI PARAMETRY
% koren ... odhad reseni
% iterace ... pocet iteraci

syms x;

fa = subs(f, {'x'}, a);
fb = subs(f, {'x'}, b);

if (fa*fb >= 0 | b < a)
    error('Spatne zadany interval!')
end

s = 1/2*(a+b);
fs = subs(f, {'x'}, s);
iterace = 0;

% zobrazeni iteraci
disp(['Iterace', blanks(4), 'aproximace'])
disp([num2str(iterace), blanks(10), num2str(s)])

while ((abs(fs) > eps) & (abs(a-b) > eps))
    if fa*fs < 0
        b = s;
        fb = subs(f, {'x'}, b);
    else a = s;
        fa = subs(f, {'x'}, a);
    end
    s = 1/2 * (a+b);
    fs = subs(f, {'x'}, s);
    iterace = iterace+1;
    
    % zobrazeni iteraci
    disp([num2str(iterace), blanks(10), num2str(s)])
end

koren = s;

end