Presentation is loading. Please wait.

Presentation is loading. Please wait.

數值方法 2008 Applied Mathematics, NDHU1  Bisection method for root finding  Binary search  fzero Lecture 4.

Similar presentations


Presentation on theme: "數值方法 2008 Applied Mathematics, NDHU1  Bisection method for root finding  Binary search  fzero Lecture 4."— Presentation transcript:

1 數值方法 2008 Applied Mathematics, NDHU1  Bisection method for root finding  Binary search  fzero Lecture 4

2 數值方法 2008 Applied Mathematics, NDHU2 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

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

4 數值方法 2008 Applied Mathematics, NDHU4 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.

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, NDHU5

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, NDHU6

7 7 Bisection Method LRM=(L+R)/2 f(L) and f(M) have the same sign: L  M f(R) and f(M) have the same sign: R  M LRM L R M L  M R  M

8 數值方法 2008 Applied Mathematics, NDHU8 Bisection method 1. Create an inline function, f 2. Input two guesses, L < R 3. If f(L)f(R) > 0, return 4. Set M to the middle of L and R 5. If f(L)f(M) < 0, R = M 6. If f(R)f(M) < 0, L = M 7. If the halting condition holds, exit, otherwise goto step 4.

9 數值方法 2008 Applied Mathematics, NDHU9 Flow chart Input L,R,f with f(L)f(R) < 0 R=M abs(f(M)) < epslon T F f(L)f(M)<0 L=M M=0.5*(L+R) c=bisection(f,L,R) T

10 數值方法 2008 Applied Mathematics, NDHU10 Flow chart if f(L)f(R) > 0 return M=0.5*(L+R) R=M f(L)f(M)<0 L=M M=0.5*(L+R) abs(f(M)) < epslon M=bisection(f,L,R) T T return

11 數值方法 2008 Applied Mathematics, NDHU11 Flow chart if f(L)f(R) > 0 return M=0.5*(L+R) R=M f(L)f(M)<0 L=M M=0.5*(L+R) abs(f(M)) > epslon M=bisection(f,L,R) T T return

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

13 數值方法 2008 Applied Mathematics, NDHU13 Non-decreasing sequence x = 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 y L R

14 數值方法 2008 Applied Mathematics, NDHU14 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

15 數值方法 2008 Applied Mathematics, NDHU15 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

16 數值方法 2008 Applied Mathematics, NDHU16 Searching for non-decreasing sequence x = 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 y L=1 R=15 M=8

17 數值方法 2008 Applied Mathematics, NDHU17 Searching for non-decreasing sequence x = 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 y R=15 L=8 M=12

18 數值方法 2008 Applied Mathematics, NDHU18 Searching for non-decreasing sequence x = 2 18 23 44 46 49 61 62 74 76 79 82 89 92 95 y L=8 R=12 M=10 Halt since x(c) equals y

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

20 數值方法 2008 Applied Mathematics, NDHU20 Flow chart Input x,y L=1;R=length(x) L=M Halting condition T F x(M)<y R=M M=ceil(0.5*(L+R))

21 數值方法 2008 Applied Mathematics, NDHU21 Flow chart L=1;R=length(x) M=ceil(0.5*(L+R)) L=M x(M)<y R=M M=ceil(0.5*(L+R)) Halting condition c=bi_search(x,y) T T return

22 數值方法 2008 Applied Mathematics, NDHU22 Discussions What is a reasonable halting condition? Index L must be less than index R Halt if L >= R or x(M) == y

23 數值方法 2008 Applied Mathematics, NDHU23 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')

24 數值方法 2008 Applied Mathematics, NDHU24 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')

25 Exercise 4 due to 10/22 1. Draw a flow chart to illustrate the bisection method for root finding 2. Implement the flow chart 3. Give two examples to verify the matlab function 數值方法 2008 Applied Mathematics, NDHU25


Download ppt "數值方法 2008 Applied Mathematics, NDHU1  Bisection method for root finding  Binary search  fzero Lecture 4."

Similar presentations


Ads by Google