250 likes | 447 Views
Lecture 4. Bisection method for root finding Binary search fzero. 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. Perturb current guess if zero derivative is detected. Failure. f=inline('x.^3-2*x+2');
E N D
Lecture 4 • Bisection method for root finding • Binary search • fzero 數值方法2008 Applied Mathematics, NDHU
Drawbacks of Newton method x=linspace(-5,5);plot(x,x.^2-2*x-2); hold on;plot(x,-3,'r') Tangentline with zero slope 數值方法2008 Applied Mathematics, NDHU
Perturb current guess if zero derivative is detected 數值方法2008 Applied Mathematics, NDHU
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 why Newton's method oscillates between these values for some starting points. 數值方法2008 Applied Mathematics, NDHU
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
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
L M R 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 R M L M L M R 數值方法2008 Applied Mathematics, NDHU
Bisection method • 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
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
Flow chart M=bisection(f,L,R) if f(L)f(R) > 0 return M=0.5*(L+R) abs(f(M)) < epslon T return M=0.5*(L+R) f(L)f(M)<0 T R=M L=M 數值方法2008 Applied Mathematics, NDHU
Flow chart M=bisection(f,L,R) if f(L)f(R) > 0 return M=0.5*(L+R) abs(f(M)) > epslon return T f(L)f(M)<0 T R=M L=M M=0.5*(L+R) 數值方法2008 Applied Mathematics, NDHU
Halting condition abs(f(M)) < epslon • Absolute f(M) is small enough to approach zero. 數值方法2008 Applied Mathematics, NDHU
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
Binary search • Binary search is a general searching approach • Let x represent a non-decreasing sequence • x(i) is less or equal thanx(j) if i < j • Let y be an instance in sequence x • Determine isuch that x(i) = y 數值方法2008 Applied Mathematics, NDHU
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
Searching for non-decreasing sequence y x = 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 M=8 R=15 L=1 數值方法2008 Applied Mathematics, NDHU
Searching for non-decreasing sequence y x = 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 M=12 L=8 R=15 數值方法2008 Applied Mathematics, NDHU
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
Searching • Ex. y=76 • The answer i=10 indicates an index where x(i) equals y >> x(10) ans = 76 數值方法2008 Applied Mathematics, NDHU
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
Flow chart c=bi_search(x,y) L=1;R=length(x) M=ceil(0.5*(L+R)) Halting condition T return x(M)<y T L=M R=M M=ceil(0.5*(L+R)) 數值方法2008 Applied Mathematics, NDHU
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
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
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
Exercise 4 due to 10/22 • 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