You are on page 1of 5

Numerical Methods

CHAPTER ONE
ROOTS OF EQUATIONS

1- Bisection Method
2- False position Method
3- Fixed point Method
4- Newton Raphson

Page 1 of 5
%===========================================
% Bisection Algorithm
f = @(x) (cos(x)) ;
a = input('Please enter lower limit, a: ') ;
b = input('Please enter upper limit, b: ') ;
n = input('Please enter no. of iterations, n: ') ;
tol = input('Please enter tolerance, tol: ') ;

fa = f(a); fb = f(b) ;
i=1;

while i <= n
c = (b - a) / 2.0 ;
p=a+c;
fp = f(p) ;
if abs(fp) < 1.0e-20 | c < tol

fprintf('\n Approximate solution p = %11.8f \n \n',p) ;


break;
else
i = i+1 ;
if fa*fp > 0
a = p;
fa = fp;
else
b = p;
fb = fp;
end
end
end

Page 2 of 5
%========================================================
% False_Position_Method
clc
clear
format short
format compact
f = @(x) 0.5+5.*cos(x);
f = @(x) 1000*x^3 + 3000*x - 15000;
x_l = 0;
x_u = 4;
if f(x_l)*f(x_u) > 0
fprintf('There is no solution in the given interval');
return
elseif f(x_l) == 0
fprintf('%f is the solution',x_l);
elseif f(x_u) == 0
fprintf('%f is the solution', x_u);
end
fprintf('i xl xu xm\n');
for i = 1:1000
xm = x_u - (x_l-x_u)*f(x_u)/(f(x_l)-f(x_u));
fprintf('%i %f %f %f\n',i,x_l,x_u,xm)
if abs(f(xm)) < 0.0001
return
end
if f(x_l)*f(xm) < 0
x_u = xm;

elseif f(x_u)*f(xm) < 0


x_l = xm;
end

end
Page 3 of 5
%===========================================
% Fixed-point Algorithm
% Find the fixed point of y = cos(x).

g = @(x) cos(x);
p0 = input('Please enter initial approximation, p0: ');
n = input('Please enter no. of iterations , n: ');
tol = input('Please enter tolerance, tol : ');

i = 1;
while i <= n
p = g(p0);
if abs(p-p0) < tol
fprintf('\n Approximate solution p = %11.8f \n\n', p)
break;
else
i = i+1;
p0 = p;
end
end

Page 4 of 5
%===========================================================
% Newton-Raphson Algorithm Method
clc
clear
format short
format compact
% function definition
f = @(x) cos(x) ;
fd = @(x) (-sin(x)) ;
p0 = input('Enter initial approximation : ') ;
n = input('Enter no. of iterations, n: ') ;
tol = input('Enter tolerance, tol: ') ;

i = 1;
while i <= n
d= f(p0) / fd(p0);
p0 = p0 - d;
if abs(d) < tol
fprintf(' \n Approximate solution xn= %11.8f \n\n',p0);

break;
else
i = i+1;
end
end

Page 5 of 5

You might also like