1 / 25

Lecture 4

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');

chanel
Télécharger la présentation

Lecture 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 • Bisection method for root finding • Binary search • fzero 數值方法2008 Applied Mathematics, NDHU

  2. 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

  3. Perturb current guess if zero derivative is detected 數值方法2008 Applied Mathematics, NDHU

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Halting condition abs(f(M)) < epslon • Absolute f(M) is small enough to approach zero. 數值方法2008 Applied Mathematics, NDHU

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Searching • Ex. y=76 • The answer i=10 indicates an index where x(i) equals y >> x(10) ans = 76 數值方法2008 Applied Mathematics, NDHU

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

More Related