If you had a roots problem in the days befbrc computing, you'd often be told to use "trial and error" to come up with the root. Thiit is, you'd repcatcdly make guesses until the function was sufTiciently close to zero. The process was grea(ly lacilitated by the advent of sofiware
www.elsolucionario.net
t t 8 ROOTS: BRACKETING METHODS
xl xu
( d )
F I G U R E 5 . I
I lustrolion of cr number of generol woys thoi o rool moy occur rn on intervol prescribed by o lower bound .r, ond on upper bound x,,. Poris (oJ ond (c) indicoie thct if both ,/ (rr) ond ./(r,,) hove the some sign, either there wili be no roots or there will be on even number of rools w i t h i n t h e in t e r v o l P o r t s ( b ) o n d ( d ) in d i c o t e t h o t l f lh e f u n c l i o n h o s d i f f e r e n t s i g n s o i t h e e n d p o i n t s , i h e r e w i l l be on odd number of rools in the intervol.
( b l F I G U R E 5 . 2
lllustrotion of some exceptions lo lhe generol coses depicted in Fig. 5. 1 (o) Multiple rootsfhot,occur when the function is tongentic to the -r oxis. For this cose,
o l ' h . , ' n h l n c e " d n n i n l s n r ' p r f n n n o . i l e s ; . n < i h o ' c o r e
on even number of oxis inlercepiions for the inlervol.
(b) Discontinuous funciions where end points of opposiie s i g ^ o ' o c [ e t o n e v e n n u r r b e r , o [ r o o ' s . , S p e c i o l s t r o ' e g i e s
^ r o , o ^ ,
" , o l l n r n o l a r m r n , n n J h o , ^ ^ r . [ ^ , t h o a ^ . - . o .
www.elsolucionario.net
5.3 BRACKETING METHODS AND INITIAL GUESSES l r 9
rre site lies
tools such as spreadsheets. By allowing you to make many guesses rapidly, such tools can actually make the trial-and-error approach attractive fbr some problems.
But, for many other problems, it is preferable to have methods that come up with the correct answer automatically. lnterestingly, as with trial and error, these approaches require an initial "guess" to get started. Then they systernatically home in on the loot in an itera- tive fashion.
The two major classes of methods available are distinguished by the type of initial guess. They are
. Brttcketing method.s. As the namc implies, thesc are based on two initial guesses that
"bracket" the root-that is. are on either sidc of the root.
. Opert methotls. These methods can involve one or more initial guesses, but there is no need for them to bracket the root.
For well-posed problems, the bracketing methods always wrlrk but converge slclwly (i.e., they typically take more iterations to home in on the answer). In contrast, the open rnethods do not always work (i.e., they can divergc), but when they do they usually con- verge quicker.
In both cases, initial guesses are required. These may naturally arise liom the physical context you are analyzing. However, in other cases, good initial guesses may not be obvi- ous. In such cases, automated approaches to obtain guesses would be useful. The following section describes one such approach, the incremental search.
5 . 3 . 1 I n c r e m e n l o l S e o r c h
When applying the graphical technique in Example -5.1, you observed that /'(.r) changed sign on opposite sides of the root. In general, if ./(x) is real and continut'rus in the interval from x7 to,r,, and .l (,r7) and f (x,,) have oppositc signs, that is,
/ ( - r 1 ) / ' ( x , , ) < 0 ( 5 . 3 )
then there is at least one real root between .r1 afld,r,,.
Ircrenrental seun:h methods capitalize on this observation by locating an intcrval where the function changes sign. A potential problcm with an incremental scarch is the cl.roice ol'the incrcment length. If the length is too srnall, the search can be very time con- suming. On tho other hand, if the length is too great, there is a possibility that closely spaced roots might be misscd (Fig. 5.3). The problem is compounded by the possible exis- tcnce of multiple roots.
An M-file can be developedr that implements an incremental search to locate the roots of a firnction f L.rnc within the range from xmin to xmax (Fig. 5.4). An optional argument ns allows the user to specify the number of intervals within the range. lf ns is ornitted, it is automatically set to 50. A f or: loop is used to step through each interval. In the event that a sign change occurs! the upper and lower bounds are stored in an array xb.
' This lirnction rs a rnoditled version of an M-lilc originally prcscntcd by Recktenwald (2000).
www.elsolucionario.net
F I G U R E 5 . 3
Coses where roots could be missed becouse the incremenlol length of t o o lo r g e N o t e th o t t h e lo s t r o o t o n l h e ri g h t i s m u l t i p e o n d w o u i d b e i n c r e m e n l l e n g l h .
lhe seorch procedure is missed regordless of the
f u n c t i o n x b = i n c s e a r c h ( f u n c , x m i n , x m a x , n s )
? i n c s e a r c h : i n c r e m e n t a l s e a r c h r o o t f o . - a L o r
% x b - i n c s e a r c h ( f u n c , x m i n , x m a x , n s ) :
% f i n d s b r a c k e t s o f x t h a t c o n t a i n s i g n c h a t t g e s
? o f a f u n c t i o n o n a n i n t e r v a l .
? i n p u L :
% f u r L c - n a m e o f f u n c t - i o r r
Y x n i - , x m a x c n d p o i t r ( s o I i n r e r v a ]
? n s = n u m b e r o f s u b i n t e r v a l s ( d e f a u l t = 5 0 )
% o u t p u L :
% x b ( k , 1 ) i s t h e l o w e r b o u n d o f L h e k t h s i g t n c h a t r g e 2 x b ( k , 2 ) i s t i r e u p p e r b o u n d o f t - h e k t h s i g n c h a n g e
% I f n o b r a c k e f . s f o u n d , x b - L l .
i f n a r g i n < 4 , n s = 5 0 ; e n c l % r f n s b l a n k s e t t o 5 0 Z I n c r e m € n t a l s e a r c h
x = I i n s p a c e ( x m r - n , x m a x , n s ) ; f - f u n c ( x ) ;
n b = 0 r : { b - i I ; e . x b i s : n u l l u n l e s s s i g n c | r a n q e f o r k = 1 ; l e n g t h ( x ) - 1
i f s i g n ( f ( k ) ) ^ - - s i g n ( f ( k + - L ) ) l i c h e c k f o r s i g n n b = n b + 1 ;
x b r n l , l ) . x { k l ; x b ( t ' t b , 2 ) = x ( k + 1 ) ; e n d
e n d
j f i s e m p t ) , , ( x b ) ? d i s p l z r - v t h a t n o b r a c k e t s w e r e f o u n d d i s p ( ' n o b r a c k e t s f o r i n d ' )
d i s p ( ' c h e c k r n t e r v a l o r i n c r e a s e n s ' )
d i s p ( ' n r : m b e r o l b r a c k e t s : ' ) % d r s p L a y n u m b e r o f b r a c k e t s d i s p ( n b )
e n d
FIGURE 5.4
An M-file fo irnplemenl on incrernelrtcl seorch
d e t e c t e d
c h a n q e
r20
EXAMPLE 5.2
www.elsolucionario.net
5.3 BRACKETING METHODS AND INITIAL GUESSES t2l E X A M P L E 5 . 2 I n c r e m e n t q l S e o r c h
Problem Stotement. Use the M-flle i ncsearch (Fig. 5.a) to identify brackets within the interval [3, 6] fbr the function:
J G ) : s i n ( l 0 . r ) * c o s ( 3 . r )
Solution. The MATLAB session using the detault number of intervals (50) is
> > i n c s c a r c h ( @ r s i n ( 1 0 * x ) + c o s ( 3 * x ) , 3 , 6 ) n u m b c r o f p o s s i b l c r o o L s :
5 a f n S -
3 . 2 1 , 4 9 t . 3 0 6 1 3 . 3 0 6 1 3 . 3 6 ' . t 3 3 . / 3 4 - t 3 . t 9 \ 9 4 . 6 5 3 1 4 . ' - t 1 4 j ,
\ . { : i 2 l a . 6 9 3 9
A plot ol'Eq. (5.4) along with thc root Iocations is shown here.
(s.4)
J ? E 5 . 5
Although five sign changes are dctccted, because the subintervals are too wide, the func- tion rnisses possible roots at .r j 4.25 and -5.2. These possible mots look like they might be doLrble nrots. However, by using the zoom in tool, it is clear that each represents two real roots thal are very closc together. The f'unction can be run again with more subintervals with thc result that all nine sign changcs arc located
> : . i n c s e a r c h ( t l x s i n ( ' l O * r ) + c o s ( 3 * x ) , 3 , b , 1 0 0 ) n u m b e r o f p o s s i b l c r o o L s :
9
3 . 2 4 2 4 3 . 2 1 2 . 7 3 . 3 5 3 6 3 . 3 9 3 9
4 . 5
www.elsolucionario.net
122 ROOTS: BRACKETING METHODS < 1
FIG A c f,oi
Ex p l c l e t ' Th
N c lat
Nr
wl thr
,, t,
3 . - t 2 1 3 3 . t 5 7 i a 4 . 2 1 2 1 4 . 2 4 2 4 4 . 2 4 2 4 4 . 2 ' , t 2 1 4 . 5 9 1 0 4 . 1 ? , 1 3 , r . 1 5 1 5 5 . 1 B 1 B 5 . 1 3 1 8 5 . 2 r 2 ) . 5 . 5 6 6 J ' : . 5 9 1 A
2
The lbregoing example illustrates that brute-lbrce nrethods such as incremental search are not lbolproof. You would be wise to supplement such automatic techniques with any other inlbrnration that provides insight into the location ol'the roots. Such intbrrnation can be fbuncl by pkrtting lhe function and through Lrnderstandinc the physical problern frorn w h i c h th e e q u a t i o n o r i g i n a t e d .