Math IV Lab: Different Methods To Find Roots
Math IV Lab: Different Methods To Find Roots
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:
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:
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
end
end
Result:
This formula is used in the program code for Newton Raphson method in MATLAB to find
new guess roots.
Matlab code:
function NewtonRaphsonMethod
i = 1;
syms 'x'
while i <= N
return
end
i = i + 1;
p0 = p; %update p0
end
end
Result:
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;
i = 2; %iterations
maxIterations = 32;
p0 = 2; %initial value
p1 =4; %value after initial value
q0 = f(p0);
q1=f(p1);
p = p1 - ((q1*(p1-p0)/(q1-q0)));
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
end
Result:
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:
f=@(x) x^3-2*x-5;
a=2; b=3;
for i=1:10
x0=a; x1=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
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:
Matlab code:
function FixedPointIteration
i = 1;
syms 'x'
g(x) = x^3-x-1; %i.e. what we are trying to solve
while i <= N
if abs((p - p0)) < error %stopping criterion using the required precision
return
end
i = i + 1;
end
end
Result: