%Finding the roots of polynomials using bisect
a=2; b=1; c=2; d=1;

ezplot(@(x) myfun(x,a,b,c,d),[-2 2]);

[xstar,iter]=bisect(@(x) myfun(x,a,b,c,d),-1,1,1e-10);

a=0; b=-2; c=1; d=3;

% We first plot the function to see what intervals to chose
ezplot(@(x) myfun(x,a,b,c,d),[-2 2]);

% there are two roots one is -1 and the other is 3/2 

[xstar1,iter]=bisect(@(x) myfun(x,a,b,c,d),-1.2,-0.5,1e-10)

% or alternative 

f=inline('-2*x^2+x+3');
ezplot(f,[-2 2]);
[xstar1,iter]=bisect(f,-1.2,-0.5,1e-10)

% now if we chose this interval (less itterations ) 
[xstar1,iter]=bisect(@(x) myfun(x,a,b,c,d),-1.5,0.5,1e-10)

% To find the second root we change the interval
[xstar2,iter]=bisect(@(x) myfun(x,a,b,c,d),0.4,1.6,1e-10)

%What happens if we start with the wrong interval
[xstar3,iter]=bisect(@(x) myfun(x,a,b,c,d),-0.4,0.6,1e-10)

%the program tells us that there is an error 

%Test now with myfun2
ezplot(@(x) myfun2(x,3,-2,6),[-2 2]); 
% check by looking at the derivative that the function 
% has only one root
%wrong interval try again
ezplot(@(x) myfun2(x,3,-2,6),[-5 0]); 

[xstar,iter]=bisect(@(x) myfun2(x,3,-2,6),-5,0,1e-14)

