Math IV Lab
Different Methods to find Roots
Name: Muhammad Usama
Registration: 796-FET/BSME/F18
INTERNATIONAL ISLAMIC UNIVERSITY,
ISLAMABAD
FACULTY OF ENGINEERING AND TECHNOLOGY
There are many methods to find out the roots of equation so the most common methods are
following:
1. Bisection Method
2. Newton Raphson Method
3. Secant Method
4. Regula Falsi Method
5. Fixed point iteration method
Bisection Method:
Bisection method is a popular root finding method of mathematics and numerical methods.
This method is applicable to find the root of any polynomial equation f(x) = 0, provided that
the roots lie within the interval [a, b] and f(x) is continuous in the interval.This method is
closed bracket type, requiring two initial guesses. The convergence is linear and it gives good
accuracy overall. Compared to other rooting finding methods, bisection method is considered
to be relatively slow because of its slow and steady rate of convergence.
Here’s how the iteration procedure is carried out in bisection method (and the MATLAB
program):
The first step in iteration is to calculate the mid-point of the interval [ a, b ]. If c be the mid-
point of the interval, it can be defined as:
c = ( a+b)/2
The function is evaluated at ‘c’, which means f(c) is calculated. Now, three cases may arise:
1. f(c) = 0 : c is the required root of the equation.
2. f(b) * f(c) > 0 : if the product of f(b) and f(c) is positive, the root lies in the interval [a,
c].
3. f(b) * f(c) < 0 : if the product of f(b) and f(c) is negative, the root lies in the interval
[ b, c].
Matlab code:
function Bisection Method
%Bisection Method example.
%Solve for the root of f(x), change f(x) inside the function for whatever
%you're using.
a = 0; %define interval where a solution should exist
b = 3; %define interval where a solution should exist
N = 100; %maximum number of iterations we will use
error = 0.001; %define a tolerence which we'd be happy with for our solution
i = 1; %start iterations with i = 1.
syms 'x'
f(x) = exp(x)-x -2; %this is the function we are solving
if f(a)*f(b) > 0; %need to make sure a solution actually exists, i.e. check f(a) and f(b)
have different signs
fprintf('The interval, a=%d, and b=%d, do not give f(%d) and f(%d) having different signs,
no solution exists \n',a,b,a,b)
return
end
while i <= N
p = a + 0.5*(b-a); %do the bisection i.e. halve distance from a to b
FP = f(p); %evaluate f(x) at p to see if it is zero
pstore(i) = p; %store the values of p as a vector
if FP == 0 || 0.5*(b-a) < error %this is our stopping criterion, either f(p) == 0 or the bisection
is within our tolerance
fprintf('The solution is %f \n', p) %outputs our solution, p.
return %break out of program, outputting only our p.
end
if f(a)*f(p) > 0; %now we check which side the p is within, this is checking if p is on
the left, if it is then a = p
a = p;
else
b = p; %otherwise, we know it must be on the right as we have already checked
if f(p) = 0.
end
i = i + 1; %continue the iterations.
end
fprintf('Could not converge in %d iterations with a tolerance of %d \n', N, error)
%unsucessful error message
end
Result:
1.14620 is the root of equation exp(x)-x -2
Newton Raphson Method:
Newton-Raphson method, named after Isaac Newton and Joseph Raphson, is a popular
iterative method to find the root of a polynomial equation. Newton-Raphson method is more
used when the first derivation of the given function/equation is a large value. It is often used
to improve the value of the root obtained using other rooting finding methods in Numerical
Methods.
xn+1 = xn – f(xn)/f’(xn)
This formula is used in the program code for Newton Raphson method in MATLAB to find
new guess roots.
Steps to find root using Newton’s Method:
Check if the given function is differentiable or not. If the function is not
differentiable, Newton’s method cannot be applied.
Find the first derivative f’(x) of the given function f(x).
Take an initial guess root of the function, say x1.
Use Newton’s iteration formula to get new better approximate of the root, say x2
x2 = x1 – f(x1)/f’(x1)
Repeat the process for x3, x4… till the actual root of the function is obtained, fulfilling
the tolerance of error.
Matlab code:
function NewtonRaphsonMethod
%Implmentaton of Newton-Raphson method to determine a solution.
%to approximate solution to x = cos(x), we let f(x) = x^3 – x-1
i = 1;
p0 = 0.5*pi; %initial conditions
N = 100; %maximum number of iterations
error = 0.0001; %precision required
syms 'x'
f(x) = x^3 - x-1; %function we are solving
df = diff(f); %differential of f(x)
while i <= N
p = p0 - (f(p0)/df(p0)); %Newton-Raphson method
if (abs(p - p0)/abs(p)) < error %stopping criterion when difference between
iterations is below tolerance
fprintf('Solution is %f \n', double(p))
return
end
i = i + 1;
p0 = p; %update p0
end
fprintf('Solution did not coverge within %d iterations at a required precision of %d \n', N,
error) %error for non-convergence within N iterations
end
Result:
1.324718 is the root of equation x^3 – x-1
Secant Method:
Secant method is an iterative tool of mathematics and numerical methods to find the
approximate root of polynomial equations. During the course of iteration, this method
assumes the function to be approximately linear in the region of interest.
This is the required formula which will used in the program for secant method in Matlab.
Matlab code:
function secant
clc;
error = 0.000001; %error rate
i = 2; %iterations
maxIterations = 32;
p0 = 2; %initial value
p1 =4; %value after initial value
syms 'x' %creating symbolic variable
f(x) = x^3-2*x^2-5; %declaring function
q0 = f(p0);
q1=f(p1);
fprintf('values are \n');
while (i <= maxIterations)
p = p1 - ((q1*(p1-p0)/(q1-q0)));
fprintf('p(%d) = %.12f \n', i,double(p));
if abs(p-p1) < error %checking error rate
fprintf('\nAs the value repeated on p(%d) so solution is value of p(%d) => %.12f \n',i,i-
1, double(p1))
return
end
i = i+1;
p0=p1;
p1=p;
q0=q1;
q1=f(p);
end
printf('solution did not converge within %d iterations \n', maxIterations)
fprintf('solution after 30 iterations is %.12f \n', double(p))
end
Result:
2.690647985590 is a root of equation x^3-2*x^2-5
Regula Falsi Method:
Regula Falsi Method, also known as the false position method, is an iterative method of
finding the real roots of a function. This method works by substituting test values for
unknown quantities, and is the oldest approach to solve equations in mathematics, numerical
methods, and engineering. It is a closed bracket-type method with slow rate of convergence.
False position method requires two initial guesses, and uses interpolation approach to find
roots. Like the bisection method, the process starts with two guess values, say a and b such
that f(a) and f(b) are of opposite sign which confirms that the root lies in the interval [a, b].
c1 = b1 – f(b1)/[ {f(b1) – f(a1)}/{ b1– a1 }]
if f(c1) = 0, the iteration is stopped, and c 1 = r. This is the required formula; the code for
Regula Falsi method in MATLAB will be based on these formula and stopping criteria.
In real practice, it is very difficult and takes large number of iteration steps to exactly get f(r)
= 0. That is why, we must initially define certain tolerance or allowed error in any program,
whether it’s in MATLAB
Matlab Code:
% False Position Method
% Find the roots of x^3-2*x-5=0 by using false position method.
f=@(x) x^3-2*x-5;
a=2; b=3;
for i=1:10
x0=a; x1=b;
fprintf('\n Hence root lies between (%.4f,%.0f)',a,b)
x2(i)=x0-(x1-x0)/(f(x1)-f(x0))*f(x0);
if f(x2(i))>0
b=x2(i);
else a=x2(i);
end
fprintf('\n Therefore, x2=%.4f \n Here, f(x2)=%.4f',x2(i),f(x2(i)))
p=x2(i);
end
for i=1:10
error(i)=p-x2(i);
end
Answer=p
plot (error)
grid on;
title('Plot of error');
xlabel('iterations');
ylabel('Error');
Result:
2.0945 is the root of equation x^3-2*x-5
Fixed point iteration method:
Fixed point iteration method is commonly known as the iteration method. It is one of the
most common methods used to find the real roots of a function. The Matlab program for
fixed point iteration method is more particularly useful for locating the real roots of an
equation given in the form of an infinite series. So, this method can be used for finding the
solution of arithmetic series, geometric series, Taylor’s series and other forms of infinite
series.
Matlab code:
function FixedPointIteration
%Fixed point iteration example.
%now we can use fixed point iteration.
i = 1;
p0 = 1.5; %our guess is p = 1.5.
error = 0.0000001; %the precision we want to find our solution in.
N = 50; %max number of iterations.
syms 'x'
g(x) = x^3-x-1; %i.e. what we are trying to solve
while i <= N
p = g(p0); %the next p value
if abs((p - p0)) < error %stopping criterion using the required precision
fprintf('Solution is %f \n', double(p)) %the output p is symbolic so need ot force it
into a decimal
return
end
i = i + 1;
p0 = p; %update the value of i and p to continue the loop with
end
fprintf('Solution did not converge to within precision = %d in %d iterations \n', error, N)
end
Result:
1.324718 is the root of equation x^3 – x-1