Vous êtes sur la page 1sur 3

bisection method

Question 2
Approximate the root of f(x) = x2 - 10 with the bisection method starting with the interval [3, 4] and use step = 0.1 and abs = 0.1 . Answer: 3.15625 (you need a few extra steps for
abs)

Applications to Engineering
To be completed.

Matlab
The bisection method in Matlab is quite straight-forward. Assume a file f.m with contents
function y = f(x) y = x.^3 - 2;

exists. Then:
>> >> >> >> >> >> >= format long eps_abs = 1e-5; eps_step = 1e-5; a = 0.0; b = 2.0; while (b - a >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) eps_abs ) ) c = (a + b)/2; if ( f(c) == 0 ) break; elseif ( f(a)*f(c) < 0 ) b = c; else a = c; end end >> [a b] ans = 1.259918212890625 1.259925842285156 >> abs(f(a)) ans = 0.0000135103601622 >> abs(f(b)) ans = 0.0000228224229404

Thus, we would choose 1.259918212890625 as our approximation to the cube-root of 2, which has an actual value (to 16 digits) of 1.259921049894873. An implementation of a function would be
function [ r ] = bisection( f, a, b, N, eps_step, eps_abs ) % Check that that neither end-point is a root % and if f(a) and f(b) have the same sign, throw an exception. if ( f(a) == 0 ) r = a; return; elseif ( f(b) == 0 ) r = b; return; elseif ( f(a) * f(b) > 0 ) error( 'f(a) and f(b) do not have opposite signs' ); end % We will iterate N times and if a root was not % found after N iterations, an exception will be thrown. for k = 1:N % Find the mid-point c = (a + b)/2; % Check if we found a root or whether or not % we should continue with: % [a, c] if f(a) and f(c) have opposite signs, or % [c, b] if f(c) and f(b) have opposite signs. if ( f(c) == 0 ) r = c; return; elseif ( f(c)*f(a) < 0 ) b = c; else a = c; end % If |b - a| < eps_step, check whether or not % |f(a)| < |f(b)| and |f(a)| < eps_abs and return 'a', or % |f(b)| < eps_abs and return 'b'. if ( b - a < eps_step ) if ( abs( f(a) ) < abs( f(b) ) && abs( f(a) ) < eps_abs ) r = a; return; elseif ( abs( f(b) ) < eps_abs ) r = b; return; end end end error( 'the method did not converge' );

end

Maple
The bisection method in Maple is quite straight-forward:
> epsilon[abs] := 1e-5: > epsilon[step] := 1e-5: > f := x -> x^3 - 2: > a := 0.0: > b := 2.0: > while b - a >= epsilon[step] or ( abs( f(a) ) >= epsilon[abs] and abs( f(b) ) >= epsilon[abs] ) do c := (a + b)/2; if f(c) = 0 then break; elif f(a)*f(c) < 0 then b := c; else a := c; end if; end do: > [a, b]; [1.259918214, 1.259925843] > abs( f(a) ); 0.000013505 > abs( f(b) ); 0.000022826

Thus, we would choose 1.259918214 as our approximation to the cube-root of 2, which has an actual value (to 10 digits) of 1.259921050.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Vous aimerez peut-être aussi