MATLAB Code For Bisection Method

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

% Solution of nonlinear equations

% Bisection method
clear all, close all, clc
format short
f = @(x) x.^3+4*x.^2-10;
a=1; b=2;
max_I=20;% maximum number of iterations
tol=10^(-5);
fa=feval(f,a); % Initial guess
fb=feval(f,b);
if fa*fb>0,

disp('Wrong choice')
end

for k=1:max_I
c=(a+b)/2;
C(k)=c;
fc=feval(f,c);
Fc(k)=fc;
if fc==0,break
end
if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end
if abs(fc)<tol,break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values


fc\n')
[(1:k);C(1:k);Fc(1:k)]'

You might also like