Professional Documents
Culture Documents
Metode Biseksi M-File
Metode Biseksi M-File
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