Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

function root = metode_biseksi(f,a,b,epsilon)

% Nama Program : metode_biseksi.m


%
% Tujuan Program : mencari akar persamaan dengan menggunakan metode biseksi
% Pengembang asli : Aamir Alaud Din
% Tanggal : 17.09.2013
% Edit Ulang : Reza K. Setyansah
% Tanggal : 27.03. 2018
%
%
%
% Sintaks : root = metode_biseksi(f,a,b)
% dimana terlebih dahulu membuat persamaan fungsi untuk f(x)
% contoh perhatikan: f = inline('x+exp(x)','x');
% a dan b merupakan bentuk interval
%
% Contoh: root = metode_biseksi(f,-1,0)

if (nargin < 3)
error('Wrong number of input arguments.');
elseif (nargin == 3)
if (f(a)*f(b) >= 0)
disp('The function has positive value at the end points of interval.');
disp('The root can''t be found using bisection method, use some other
method.');
root = 'Root can''t be found using bisection method';
return;
else
fprintf('Iteration\t\t\ta\t\t\tb\t\t\tm\t\t\tf(a)\t\t\tf(b)\t\t\tf(m)\n');
fprintf('=========\t\t ======\t\t ======\t\t ======\t\t ======\t\t
======\t\t ======\n');
m = (a + b)/2;
if (abs(f(m)) <= 1e-6)
root = m;
return;
else
Iter = 0;
while (abs(f(m)) >= 1e-6)
Iter = Iter + 1;
m = (a + b)/2;
if(f(a)*f(m) > 0)
a = m;
else
b = m;
end
fprintf('%3d',Iter);
fprintf('%20.4f',a);
fprintf('%12.4f',b);
fprintf('%12.4f',m);
fprintf('%14.4f',f(a));
fprintf('%16.4f',f(b));
fprintf('%16.4f',f(m));
fprintf('\n');
end
root = m;
end
end
elseif (nargin == 4)
if (f(a)*f(b) >= 0)
disp('The function has positive value at the end points of interval.');
disp('The root can''t be found using bisection method, use some other
method.');
root = 'Root can''t be found using bisection method';
return;
else
fprintf('Iteration\t\t\ta\t\t\tb\t\t\tm\t\t\tf(a)\t\t\tf(b)\t\t\tf(m)\n');
fprintf('=========\t\t ======\t\t ======\t\t ======\t\t ======\t\t
======\t\t ======\n');
m = (a + b)/2;
if (abs(f(m)) <= epsilon)
root = m;
return;
else
Iter = 0;
while (abs(f(m)) >= epsilon)
Iter = Iter + 1;
m = (a + b)/2;
if(f(a)*f(m) > 0)
a = m;
else
b = m;
end
fprintf('%3d',Iter);
fprintf('%20.4f',a);
fprintf('%12.4f',b);
fprintf('%12.4f',m);
fprintf('%14.4f',f(a));
fprintf('%16.4f',f(b));
fprintf('%16.4f',f(m));
fprintf('\n');
end
root = m;
end
end
end

You might also like