echo on %So we can see the commands.

format short e %Display 5 digits and an exponent.

%This script can be used to test the two versions of the bisection
%algorithm that were discussed in class. 

%The script runs in matlab, but not in octave. 

%The first argument of bisection and bisection2 is a function
%handle. This is denoted @f where f is the name of an m-file or
%builtin function.

%In the example below, sin(x) has no root in [1,2].

bisection(@sin, 1,2, 2^(-10))

fprintf('Type any key to continue.\n')


pause

%Here, the root should be pi/2

r = bisection2(@cos, 1,4, 25)
error = abs(pi/2-r)

fprintf('Type any key to continue.\n')
pause 

%We may also define functions inline--

F = @(x) x*exp(x)-2;
bisection (F, 0,3, 2^(-25))

x= [0:0.1:2];
plot(x, x.*exp(x)-2)

