Graphics Programs Reference
In-Depth Information
10.0
5.0
0.0
Figure 4.1. Plot of tan x .
-5.0
-10.0
0
1
2
3
4
5
6
x
rootsearch
The function rootsearch looks forazero of the function f ( x ) in the interval( a,b ).
The search starts at a and proceeds in steps dx toward b . Once a zero is detected,
rootsearch returns its bounds( x1,x2 ) to the calling program. If aroot was not
detected, x1 = x2 = NaN is returned (in MATLAB NaN standsfor“not a number”).
After the first root (the root closest to a )has beenbracketed, rootsearch can be
called again with a replacedby x2 in order to find the next root. Thiscan be repeated
aslong as rootsearch detects aroot.
function [x1,x2] = rootsearch(func,a,b,dx)
% Incremental search for a root of f(x).
% USAGE: [x1,x2] = rootsearch(func,a,d,dx)
% INPUT:
% func = handle of function that returns f(x).
%a,b =limitsofsearch.
% dx = search increment.
% OUTPUT:
% x1,x2 = bounds on the smallest root in (a,b);
%
set to NaN if no root was detected
x1 = a; f1 = feval(func,x1);
x2=a+dx;f2=feval(func,x2);
while f1*f2 > 0.0
if x1 >= b
x1 = NaN; x2 = NaN; return
end
x1=x2; f1=f2;
x2 = x1 + dx; f2 = feval(func,x2);
end
Search WWH ::




Custom Search