function [x, cnt] = calcolaZeri(f, a, b, tolX, tolY)


if ~exist('tolX', 'var') || isempty(tolX)
    tolX = 1e-5;
end

if ~exist('tolY', 'var') || isempty(tolY)
    tolY = 1e-5;
end


if (f(a) * f(b) > 0)
    x = nan;
    cnt = 0;
    return
end

if f(a) == 0
    x = a;
    cnt = 0;
    return
end

if f(b) == 0
    x = b;
    cnt = 0;
    return
end    

c = (a + b) / 2;
    
if abs(f(c)) < tolY || (abs(b-a) < tolX)
   x = c;
   cnt = 1;
   return;
end

if(f(c) * f(b) < 0)
    [x, cnt] = calcolaZeri(f, c, b, tolX, tolY);
else
    [x, cnt] = calcolaZeri(f, a, c, tolX, tolY);    
end
cnt = cnt + 1;



