0% found this document useful (0 votes)
11 views

Bisection MATLAB

Uploaded by

Ucchash Dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Bisection MATLAB

Uploaded by

Ucchash Dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

% Clearing Screen

clc

% Setting x as symbolic variable


syms x;

% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
m = input('Maximum iteration number :');

% Finding Functional Value


fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));

% Implementing Bisection Method


if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
i = 1;
c = (a+b)/2;
fc = eval(subs(y,x,c));
tol = abs(b-a)/2;
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\t\t\ttol\n');
while tol > e
fprintf('%f\t%f\t%f\t%f\t%f\n',a,b,c,fc,tol);
iterationData(i, :) = [i, a, b, c, fc, tol];
if fa*fc< 0
b = c;
else
a = c;
end
c = (a+b)/2;
fc = eval(subs(y,x,c));
tol = abs(b-a)/2;
i = i + 1;
if i == m +1
fprintf('Iteration number is not sufficient.');
break
end
end
fprintf('\nRoot is: %f\n', c);
figure(1)
hold on
plot(iterationData(:,1), iterationData(:,6),'r-o')
xlabel('No. of Iterations');
ylabel('Error');
legend('Bisection Method');
hold off
end

You might also like