Finance/numerical methods

Bisection method(이분법)

Gu Youn 2009. 9. 30. 23:21
너무 간단한 로직이어서 뭘로 구현하든 큰 차이가 없으니 매트랩 소스만 를 올린다.

function root = Bisection(start_point, end_point)

% Exit Condition %
tolerance = 1.0e-20;
numofsteps = 100;

mid_point = start_point + (end_point - start_point) / 2.0;   

error = abs(mid_point - start_point);
value = target_func(mid_point);

i = 0;
while (error > tolerance) && i < numofsteps

    if( value * target_func(end_point) > 0)
        end_point = mid_point;
    elseif ( value * target_func(end_point) < 0 )
        start_point = mid_point;
    end;
   
    mid_point = start_point + (end_point - start_point) / 2.0;   
    value = target_func(mid_point);
    error = abs(mid_point - start_point);
   
    i = i + 1;
end

% outpout %
i

format long
root = mid_point
error

end

function value = target_func(x)
    value = x^5 - 5.5*x^4 + 12.1*x^3 - 13.31*x^2 + 7.3205*x - 1.61051;
end