You are on page 1of 1

% BISection Method in MATLAB

clear all;
clc;

prompt1 = 'enter lower limit=';


prompt2 = 'enter upper limit=';
prompt3 = 'enter max no. of iterations=';

a = input(prompt1);
b = input(prompt2);
n = input(prompt3);
error = 0.0001;

fa = BIS(a);
fb = BIS(b);

if fa*fb>0;
fprintf('the interval, a=%d, and b=%d, do not give f(%d) and f(%d)
having different, signs, no solution exixts \n', a, b, fa, fb)
return
end
fprintf('iteration * f(x) tolerance \n')
for j = 1:n
p = a + 0.5*(b-a);
fp = BIS(p);
t = 0.5*(b-a);
fprintf(' %d %f %f \n', j, p, fp, t);
if fp==0 || t<error
fprintf('the solution is %f \n', p)
return
end
if fa*fp>0;
a=p;
else
b=p;
end
fprintf('could not converge in %d iteration with a tolerance of %d
\n', n, error)
end

You might also like