Graphics Programs Reference
In-Depth Information
for n , we get
ln (
|
x
|
)
n
=
(4.1)
ln2
bisect
Thisfunctionuses the method of bisection to compute the root of f ( x )
0that is
known to lie in the interval( x1,x2 ). The number of bisections n required to reduce
the intervalto tol iscomputed fromEq. (4.1). The input argument filter controls
the filtering of suspected singularities.Bysetting filter = 1 , weforce the routine
to check whether the magnitudeof f ( x ) decreases with each intervalhalving. If it does
not, the “root” may not be aroot at all, but a singularity, in which case root = NaN is
returned. Since thisfeature is not always desirable, the default value is filter = 0 .
=
function root = bisect(func,x1,x2,filter,tol)
%Findsabracketedzerooff(x)bybisection.
% USAGE: root = bisect(func,x1,x2,filter,tol)
% INPUT:
% func = handle of function that returns f(x).
% x1,x2 = limits on interval containing the root.
%filter=singularityfilter:0=off(default),1=on.
% tol
= error tolerance (default is 1.0e4*eps).
% OUTPUT:
% root
= zero of f(x), or NaN if singularity suspected.
if nargin < 5; tol = 1.0e4*eps; end
if nargin < 4; filter = 0; end
f1 = feval(func,x1);
if f1 == 0.0; root = x1; return; end
f2 = feval(func,x2);
if f2 == 0.0; root = x2; return; end
if f1*f2 > 0;
error('Root is not bracketed in (x1,x2)')
end
n = ceil(log(abs(x2 - x1)/tol)/log(2.0));
fori=1:n
x3 = 0.5*(x1 + x2);
f3 = feval(func,x3);
if(filter == 1) & (abs(f3) > abs(f1))...
& (abs(f3) > abs(f2))
Search WWH ::

Custom Search