Implementation of Bisection Method written in Matlab.

% TheFlyingKeyboard
% theflyingkeyboard.net

intervalBegin = 0.0;
intervalEnd = 2.0;

precision = 0.0001;
middle = (intervalBegin + intervalEnd) / 2;

%bisection method
while(abs(Function(middle)) > precision)
  middle = (intervalBegin + intervalEnd) / 2;
  
  if(Function(intervalBegin) * Function(middle) < 0)
    intervalEnd = middle;
  else
    intervalBegin = middle;
  end
end

%plot data
plotDataX = [];
plotDataY = [];

for i = 1:(abs(intervalBegin + intervalEnd) / precision)
  plotDataX(i) = intervalBegin + precision * i;
  plotDataY(i) = Function(intervalBegin + precision * i);
end

plot(plotDataX, plotDataY, 'b');
grid on;
hold on, plot(middle, Function(middle), 'r', 'MarkerSize', 12);

 

% TheFlyingKeyboard
% theflyingkeyboard.net

function y = Function(x)
  y = x * (x + 1) - 2;
end

 

Matlab Bisection Method
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *