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 數值方法 2008 Applied Mathematics, NDHU5 Bisection Method a b c=(a+b)/2 f(a) and f(c) at the same side: a  c f(b) and f(c) at the same side: b  c a b c a bc a  c b  c

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

7 數值方法 2008 Applied Mathematics, NDHU7 Flow chart Input a,b,f with f(a)f(b) < 0 b=c abs(f(c)) < epslon T F f(a)f(c)<0 a=c c=0.5*(a+b) c=bisection(f,a,b) T

8 數值方法 2008 Applied Mathematics, NDHU8 Flow chart if f(a)f(b) > 0 return c=0.5*(a+b) b=c f(a)f(c)<0 a=c c=0.5*(a+b) abs(f(c)) < epslon c=bisection(f,a,b) T T return

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

10 數值方法 2008 Applied Mathematics, NDHU10 Binary search Binary search is a general searching approach Let x represent a non-decreasing sequence x(i) <= x(j) if i < j Let y be an instance in sequence x Determine i * such that x(i * ) = y

11 數值方法 2008 Applied Mathematics, NDHU11 Flow chart Input x,y a=1;b=length(x) a=c Halting condition T F x(c)<y b=c c=ceil(0.5*(a+b))

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

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


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

Similar presentations


Ads by Google