Lecture 4 Bisection method for root finding Binary search fzero 數值方法2008 Applied Mathematics, NDHU 1 Drawbacks of Newton method x=linspace(-5,5);plot(x,x.^2-2*x-2); hold on;plot(x,-3,'r') Tangent line with zero slope 數值方法2008 Applied Mathematics, NDHU 2 Perturb current guess if zero derivative is detected 數值方法2008 Applied Mathematics, NDHU 3 Failure f=inline('x.^3-2*x+2'); x=linspace(-2,2);plot(x,f(x));hold on plot(x,0,'g') plot([0 1],[0 f(1)],'r'); plot([1 0],[0 f(0)],'r'); The tangent lines of x^3 2x + 2 at 0 and 1 intersect the x-axis at 1 and 0, respectively, illustrating wh y Ne wt on's method oscillates between these values for some starting points. 數值方法2008 Applied Mathematics, NDHU 4 Bisection method A root is within an interval [L,R] The bisection method Cut [L,R] into equal-size subintervals. such as [L,R] to [L,M] U [M,R] Determine which interval contains a root Then select it as the searching interval Repeat the same process until halting condition holds. 數值方法2008 Applied Mathematics, NDHU 5 Different signs Let f be continuous in the interval [L,R] These exists at least one root in [L,R] if f(L) and f(R) have different signs, equivalently f(L) f(R) < 0 數值方法2008 Applied Mathematics, NDHU 6 Bisection Method L M=(L+R)/2 R f(L) and f(M) have the same sign: L M f(R) and f(M) have the same sign: R M RM LM L M R L 數值方法2008 Applied Mathematics, NDHU M R 7 Bisection method 1. 2. 3. 4. 5. 6. 7. Create an inline function, f Input two guesses, L < R If f(L)f(R) > 0, return Set M to the middle of L and R If f(L)f(M) < 0, R = M If f(R)f(M) < 0, L = M If the halting condition holds, exit, otherwise goto step 4. 數值方法2008 Applied Mathematics, NDHU 8 Flow chart c=bisection(f,L,R) Input L,R,f with f(L)f(R) < 0 M=0.5*(L+R) f(L)f(M)<0 T R=M L=M F abs(f(M)) < epslon T 數值方法2008 Applied Mathematics, NDHU 9 Flow chart M=bisection(f,L,R) if f(L)f(R) > 0 return M=0.5*(L+R) T return abs(f(M)) < epslon M=0.5*(L+R) f(L)f(M)<0 T R=M 數值方法2008 Applied Mathematics, NDHU L=M 10 Flow chart M=bisection(f,L,R) if f(L)f(R) > 0 return M=0.5*(L+R) return abs(f(M)) > epslon T f(L)f(M)<0 T R=M L=M M=0.5*(L+R) 數值方法2008 Applied Mathematics, NDHU 11 Halting condition abs(f(M)) < epslon Absolute f(M) is small enough to approach zero. 數值方法2008 Applied Mathematics, NDHU 12 Non-decreasing sequence y x= 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 R L 數值方法2008 Applied Mathematics, NDHU 13 Binary search Binary search is a general searching approach Let x represent a non-decreasing sequence x(i) is less or equal than x(j) if i < j Let y be an instance in sequence x Determine i such that x(i) = y 數值方法2008 Applied Mathematics, NDHU 14 Random sequence >> x=round(rand(1,15)*100) x= Columns 1 through 9 95 23 61 49 89 76 46 2 82 Columns 10 through 15 44 62 79 92 74 18 數值方法2008 Applied Mathematics, NDHU 15 Searching for non-decreasing sequence y x= 2 18 L=1 23 44 46 49 61 62 74 76 M=8 數值方法2008 Applied Mathematics, NDHU 79 82 89 92 95 R=15 16 Searching for non-decreasing sequence y x= 2 18 23 44 46 49 61 62 74 76 L=8 數值方法2008 Applied Mathematics, NDHU 79 82 89 M=12 92 95 R=15 17 Searching for non-decreasing sequence y x= 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 R=12 L=8 M=10 Halt since x(c) equals y 數值方法2008 Applied Mathematics, NDHU 18 Searching Ex. y=76 The answer i=10 indicates an index where x(i) equals y >> x(10) ans = 76 數值方法2008 Applied Mathematics, NDHU 19 Flow chart Input x,y L=1;R=length(x) M=ceil(0.5*(L+R)) x(M)<y L=M R=M F Halting condition T 數值方法2008 Applied Mathematics, NDHU 20 Flow chart c=bi_search(x,y) L=1;R=length(x) M=ceil(0.5*(L+R)) T return Halting condition x(M)<y T L=M R=M M=ceil(0.5*(L+R)) 數值方法2008 Applied Mathematics, NDHU 21 Discussions What is a reasonable halting condition? Index L must be less than index R Halt if L >= R or x(M) == y 數值方法2008 Applied Mathematics, NDHU 22 MATLAB: fzero x=linspace(-5,5);plot(x,sin(x.*x)); hold on; plot(x,0,'r') x = fzero(@(x)sin(x*x),-0.1); plot(x,0,'or') 數值方法2008 Applied Mathematics, NDHU 23 fzero x=linspace(-5,5);plot(x,sin(x.*x)); hold on; plot(x,0,'r') x = fzero(@(x)sin(x*x),0.1); plot(x,0,'or') 數值方法2008 Applied Mathematics, NDHU 24 Exercise 4 due to 10/22 1. 2. 3. Draw a flow chart to illustrate the bisection method for root finding Implement the flow chart Give two examples to verify the matlab function 數值方法2008 Applied Mathematics, NDHU 25