% Find max of f(x) = -x^2 - 2x, x in [-3,6] using Dichotomous method
clear all
close all

t = 0.01;
a = -3;
b = 6;
n = ceil(log(t/(b-a))/log(1/2));
ep = 10^-3;


for k=1:n
    x1 = (a+b)/2 - ep;
    x2 = (a+b)/2 + ep;
    
    fx1 = -x1^2 - 2*x1;
    fx2 = -x2^2 - 2*x2;
    
    if fx1 >= fx2
        a = a;
        b = x2;
    else
        b = b;
        a = x1;
    end
end

x_s = (a+b)/2
Max = -x_s^2 - 2*x_s
