The bisection method in mathematics is a root-finding method that repeatedly bisects an interval and then selects a subinterval in which a root
must lie for further processing. It is a very simple and robust method,
but it is also relatively slow. Because of this, it is often used to
obtain a rough approximation to a solution which is then used as a
starting point for more rapidly converging methods. The method is also called the interval halving method, the binary search method, or the dichotomy method.
The input for the method is a continuous function f, an interval [a, b], and the function values f(a) and f(b). The function values are of opposite sign (there is at least one zero crossing within the interval). Each iteration performs these steps:
Matlab Script
clear all
close all
x=-10:.01:10
func=@(x) sin(x) - 3*x^2 + exp(x + 1); %Function
l=-500;
u=600;
es=1e-12; %Error
maxit=1000;
iter=0;
xr=0;
ea=0;
A=[];
while(1)
xrold = xr;
xr=(l+u)/2;
iter=iter+1;
if xr~=0
ea=abs((xr-xrold)/xr)*100;
end
test=func(l)*func(xr);
if test<0
u=xr;
elseif test>0
l=xr;
else
ea=0;
end
fx=func(xr);
A=[A;iter,xr,fx,ea];
if ea<=es|iter>=maxit
fprintf('');
fprintf('%d %e %e %e',A);
break
end
end
The input for the method is a continuous function f, an interval [a, b], and the function values f(a) and f(b). The function values are of opposite sign (there is at least one zero crossing within the interval). Each iteration performs these steps:
- Calculate c, the midpoint of the interval, c = a + b2.
- Calculate the function value at the midpoint, f(c).
- If convergence is satisfactory (that is, a - c is sufficiently small, or f(c) is sufficiently small), return c and stop iterating.
- Examine the sign of f(c) and replace either (a, f(a)) or (b, f(b)) with (c, f(c)) so that there is a zero crossing within the new interval.
Matlab Script
clear all
close all
x=-10:.01:10
func=@(x) sin(x) - 3*x^2 + exp(x + 1); %Function
l=-500;
u=600;
es=1e-12; %Error
maxit=1000;
iter=0;
xr=0;
ea=0;
A=[];
while(1)
xrold = xr;
xr=(l+u)/2;
iter=iter+1;
if xr~=0
ea=abs((xr-xrold)/xr)*100;
end
test=func(l)*func(xr);
if test<0
u=xr;
elseif test>0
l=xr;
else
ea=0;
end
fx=func(xr);
A=[A;iter,xr,fx,ea];
if ea<=es|iter>=maxit
fprintf('');
fprintf('%d %e %e %e',A);
break
end
end