%estimates intersection points of y=e^x and y=3*x via the bisection method

f = @(x) exp(x)-3*x;
n = 10;            %number of iterations

a = 0;            %left endpoint
b = 1;             %right endpoint

for i=1:n
    c = 0.5*(a+b);                 %midpoint
    if sign(f(c)) ~= sign(f(a))
        b = c;                     %f(a)f(c)<0 means the root is in [a,c]
    else
        a = c;                     %f(a)f(c)>0 means the root is in [c,b]
    end
end

disp('x-coordinate of first intersection point is...')
a
        
a = 1;
b = 2;

for i=1:n
    c = 0.5*(a+b);                 %midpoint
    if sign(f(c)) ~= sign(f(a))
        b = c;                     %f(a)f(c)<0 means the root is in [a,c]
    else
        a = c;                     %f(a)f(c)>0 means the root is in [c,b]
    end
end

disp('x-coordinate of second intersection point is...')
a
