Bisection Method
Bisection Method
116) Given lower and upper bounds, xl and xu which bracket the root: f(x1) f(xu) < 0 1) Estimate the Root by midpoint: x r 2) Revise the bracket: f(xl) f(xr) < 0, f(x1) f(xr) > 0, xr > xu, xr > xl
xrnew x old r
new xr
xl xu 2
3) Repeat steps 1-2 until: (a) | f(xr) | < (c) x u xl f(x) (b) a < s , with a
100%
f(x)
xr
xl xu 2
f(xu)
f(x1)
(xr (xu) x )
Summary of Bisection Method Advantages: 1. Simple 2. Good estimate of maximum error x xu E max 1 2 3. Convergence guaranteed
i 1 i Emax 0.5 Emax
Disadvantages: 1. Slow 2. Requires initial interval around root: Use graph of function, incremental search, or trial & error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
format long; x = sym('x'); expr = input('Please enter the equation : '); fx = sym(expr); x1 = input('Please enter value of X1 : '); x2 = input('Please enter value of X2 : '); iter = input('Please enter number of Iterations : '); fx1 = subs(fx,x,x1); fx2 = subs(fx,x,x2); x3 = 0; counter = 0; fid=fopen('c:\bisection.csv','w'); fprintf(fid,'%s\n','Iteration,x1,x2,x3,F(x1),F(x2),F(x3),Max Errors'); if fx1*fx2 >= 0 message = 'x1 and x2 values are not valid for desired root'; disp(message) else
while ((counter == 0 || abs(x1-x2) > 0.000001 || subs(fx,x,x3) == 0) && c <= iter) counter = counter + 1; x3 = (x1 + x2) / 2; fprintf(fid,'%d,',counter); fprintf(fid,'%5.8f,',x1);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fprintf(fid,'%5.8f,',x2); fprintf(fid,'%5.8f,',x3); fprintf(fid,'%5.8f,',subs(fx,x,x1)); fprintf(fid,'%5.8f,',subs(fx,x,x2)); fprintf(fid,'%5.8f,',subs(fx,x,x3)); fprintf(fid,'%5.8f\n',abs((x2-x1)/(2^counter))); x3 = (x1 + x2) / 2; if subs(fx,x,x3) * subs(fx,x,x1)< 0 x2 = x3; else x1 = x3; end end end sprintf('The root of the equation using Bisection method is :: %d', x3) fclose(fid);