function [x_sol, f_at_x_sol, N_iterations] = Ridders(f, x1, x2, eps_f, eps_x)

if sign(f(x1))==sign(f(x2))
    error ('Function values at x1 and x2 must have opposite sign')
end

x3=(x1+x2)/2;
f1=f(x1);
f2=f(x2);
f3=f(x3);
n=0;

while (f(x3+sign(f1-f2)*(f3/sqrt(f3^2-f1*f2))*(x3-x1))>eps_f) ...
        && (x3-(x3+sign(f1-f2)*(f3/sqrt(f3^2-f1*f2))*(x3-x1))>eps_x)
    
   x4=x3+sign(f1-f2)*(f3/sqrt(f3^2-f1*f2))*(x3-x1);
   n=n+1;
     if (sign(f(x3))~=sign(f(x4)))
         x3=(x4+x3)/2;
     else 
         if (sign(f(x1))~=sign(f(x4)))
             x3=(x1+x4)/2;
         else
             x3=(x2+x4)/2;
         
         end
     end   
end

    x_sol=x3+sign(f1-f2)*(f3/sqrt(f3^2-f1*f2))*(x3-x1);
    f_at_x_sol=f(x_sol);   
    N_iterations=n;
    
end

