Download presentation
Presentation is loading. Please wait.
Published byBerniece Johns Modified over 9 years ago
1
1 Bracketing Methods
2
Finding the Roots of the Equation f(x)=0 Graphical method Plot of the function and observe where it crosses the x- axis Bracketing methods Make two initial guesses that “bracket” the root - that is, are on either side of the root Bisection: divide the interval in half False position: connect the endpoints of the interval with a straight line and determine the location of the intercept of the x-axis 2
3
Graphical Methods A simple method for obtaining the estimate of the root of the equation f(x)=0 is to make a plot of the function and observe where it crosses the x-axis Graphing the function can also indicate where roots may be and where some root-finding methods may fail: (a) Same sign, no roots (b) Different sign, one root (c) Same sign, two roots (d) Different sign, three roots 3
4
Bracketing Methods Bracketing methods are based on making two initial guesses that “bracket” the root - that is, are on either side of the root Brackets are formed by finding two guesses x l and x u where the sign of the function changes; that is, where f(x l ) f(x u ) < 0 The incremental search method tests the value of the function at evenly spaced intervals and finds brackets by identifying function sign changes between neighboring points 4
5
5 求根問題 數學定理 — 中間值定理 (Intermediate value theorem) 若 f(x) 在區間 [a,b] 連續,且 L 為 f(a) 與 f(b) 之間的值,則必 存在一點 c, 其中 a < c < b ( 即 c (a,b)) ,使得 f(c)=L
6
Incremental Search Hazards If the spacing between the points of an incremental search are too far apart, brackets may be missed due to capturing an even number of roots within two points Incremental searches cannot find brackets containing even-multiplicity roots regardless of spacing 6
7
7 1. 令 a 0 =a, b 0 =b, i=1; (i 為計算次數 ) 2. 計算 x i = ; 3. If (x i 滿足終止條件 ) , then x i 為所求, stop 4. If (f ( a i-1 ) f ( x i ) <0) , then a i = a i-1, b i = x i ; else a i = x i, b i = b i-1 ; 5. i = i+1 , goto step2 a i-1 +b i-1 2 演算法 求根可使用二分法 (Bisection Method) 來解
8
8 ︱ f(x i ) ︱ < ε ( 寫程式時使用 ) ︱ x i ﹣ x i-1 ︱ < ε ( 寫程式或用數值方法計算時使用 ) ︱ ︱ < ε' 其中 ε 及 ε' 均為極小值且 ε'<< ε ,例 ε=10 -5,ε'=10 -7 x i ﹣ x i-1 xixi 二分法之終止條件為下列三件之一
9
9 二分法計算範例 方程式 f(x) = x 3 – 3.25x 2 –2x + 6.5 = 0 的最小正根 令初始值 a 0 = 1 , b 0 = 2 代入二分法演算法 以 |X i+1 – X i | < ε 為終止條件, ε = 5 x 10 –5
10
10 二分法計算範例
11
11 二分法計算範例:解
12
Bisection Bisection method is a variation of the incremental search method in which the interval is always divided in half If a function changes sign over an interval, the function value at midpoint is evaluated Location of the root is then determined as lying within the subinterval where the sign change occurs Error reduced by a factor of 2 for each iteration 12
13
function [root,ea,iter] = bisect(func,xl,xu,es,maxit,varargin) % [root,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...): % uses bisection method to find the root of func if nargin<3, error(‘at least 3 input arguments required’), end test = func(xl,varargin{:})*func(xu,varargin{:}); if test>0, error(‘no sign change’), end if nargin<4|isempty(es), es=0.0001; end if nargin<5|isempty(maxit), maxit=50; end iter = 0; xr = xl; while (1) xrold = xr; xr = (xl + xu)/2; iter = iter + 1; if xr ~= 0, ea = abs((xr-xrold)/xr)*100; end test = func(xl,varargin{:})*func(xr,varargin{:}); if test < 0 xu = xr; elseif test > 0 xl = xr; else ea = 0; end if ea = maxit, break, end end root = xr; 此程式與書本 Fig. 5.7 稍有不同,且毋須用到 varargin
14
Bisection Error The absolute error of the bisection method is solely dependent on the absolute error at the start of the process (the space between the two guesses) and the number of iterations: The required number of iterations to obtain a particular absolute error can be calculated based on the initial guesses: 14 … 一開始時 ( 第 0 次迭代 ) ,左右邊界的距離
15
False Position ( 試位法 ) False position is another bracketing method Determine the next guess not by splitting the bracket in half but by connecting the endpoints with a straight line and determining the location of the intercept of the straight line (x r ) The value of x r then replaces whichever of the two initial guesses yields a function value with the same sign as f(x r ) 15
16
Bisection vs. False Position Bisection does not take into account the shape of the function Can be good or bad, depending on the function 試位法效能較二分法差的例子 16
17
補充說明 17
18
nargin, nargout nargin Return the number of input arguments specified for a function or -1 if the function has a variable number of input arguments nargout Return the number of output arguments specified for a function 18
19
19 function [x0, y0] = myplot(x, y, r, delta, angle1, angle2) % The first two input arguments are required; the others have % default values if nargin < 6, angle2 = 360; end if nargin < 5, angle1 = 0; end if nargin < 4, delta = 10; end if nargin < 3, r = 50; end npts = (angle2-angle1)/delta; x_pos(1) = x+r; y_pos(1) = y; for i = 1:npts+1 x_pos(i) = x+r*cos(delta/180*pi*i); y_pos(i) = y+r*sin(delta/180*pi*i); end if nargout == 0 plot(x_pos, y_pos); axis equal; grid on; else x0 = x; y0 = y; end >> myplot(0,0) >> myplot(10,20,30,5,0,90) 使用 nargin 設定輸入引數之預設值的範例
20
varargin and varargout Used only inside a function M-file to contain the optional arguments to the function Each must be declared as the last argument to a function, collecting all the inputs or outputs from that point onwards 例 collects all the inputs starting with the second input into the variable varargin myplot2 uses the comma-separated list varargin{:} to pass the optional parameters to plot The call myplot2(sin(0:.1:1),'color',[.5.7.3],'linestyle',':') results in varargin being a 1-by-4 cell array containing the values 'color', [.5.7.3], 'linestyle', and ':'. varargin, varargout function myplot2(x,varargin) plot(x,varargin{:}) >> myplot2(sin(0:.1:1),'color',[.5.7.3],'linestyle',':')
21
varargin, varargout 21 function [s,varargout] = mysize(x) nout = max(nargout,1)-1; s = size(x); for k=1:nout, varargout(k) = {s(k)}; end >> [s,row,col] = mysize(rand(4,5)) s = 4 5 row = 4 col = 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.