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

International Islamic University

Islamabad
Mathematics IV (LAB)
Assignment No. 03
Submitted by:
Talal Azfar

908-FET/BSME/F20(A)

Submitted To:
Engr. Muhammad Naeem Khan

Department Of Mechanical

Engineering IIUI
Sr No. Content Page No.
01 Introduction 01
02 Newton Raphson Method 01
03 Iterative Method 03
04 Binary bisection method 05

05 Secant method 07

06 Regula-Falsi method 08

IntroductIon
Numerical methods are particularly useful while solving the intensive polynomial for their roots.
These are the following numerical methods to find roots of an equation:
1. Newton–Raphson method
2. Iteration method
3. Binary bisection method
4. Secant method
5. Regula-Falsi method
Each of the above-mentioned methods is discussed at length in the subsequent sections with their
drawbacks and advantages

1. Newton–Raphson method
Newton Raphson method is an efficient technique to solve the equations numerically. It gives us
better approximations in terms of solutions.

Formula:
𝒇(𝑿𝒏)
𝑿𝒏+𝟏 = 𝑿𝒏 − 𝒇′(𝑿𝒏)

Steps to find root using Newton’s Method:


1. Check if the given function is differentiable or not. If the function is not differentiable,
Newton’s method cannot be applied.
2. Find the first derivative f’(x) of the given function f(x).
3. Take an initial guess root of the function, say x1.
4. Use Newton’s iteration formula to get new better approximate of the root, say x2
5. 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.

Example:
Find the Roots of the given function x3-x-1 = 0

MATLAB Code:
function root=Nram(func,dfunc,xr)
func=@(x) 3*x^4-2*x-1;
dfunc=@(x) 13*x^4-1;
xr=3;
while abs(func(xr))>0.001
xr=xr-func(xr)/dfunc(xr)
end
fprintf('The root is %g\n', xr)
Solution:
>> NR
xr =1.0268
xr =1.0059
xr =1.0011
xr =1.0002
xr =1.0000
The root is 1.00003
2. Iteration method
Iterations and modifications are successively continued with the updated approximations of the
guess. Iterative method gives good accuracy overall just like the other methods.This method is
linearly convergent with somewhat slower rate of convergence, similar to the bisection method.
It is based on modification approach to find the fixed point. It is commonly referred to as simple
enclosure method or open bracket method.

Features of Iteration Method:


• Type – open bracket
• No. of initial guesses – 1
• Convergence – linear
• Rate of convergence – fast
• Accuracy – good
• Programming effort – easy
• Approach – modification

Formula:
Xn+1 = g(Xn)
Example:
Find the Roots of the equation x3-5x-7 = 0
MATLAB Code:
function [root,iteration] = fixedpoint(a,f) %input intial approiximation and

if nargin<1
fprintf('Error! Atleast one input argument is required.');
return;
end
if nargin<2
a=0;
end
x(1) = f(a) ;
i =1 ;
temp = false;
while temp == false
x(i+1) = f(x(i));
if x(i+1) == x(i)
temp = true;
root = x(i-1);
iteration = i-1;
return;
3. Binary 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.

Formula:
Find two points, say a and b such that a < b and f(a)* f(b) < 0

Find the midpoint of a and b, say “t”


t is the root of the given function if f(t) = 0; else follow the next step
Divide the interval [a, b] – If f(t)*f(a) <0, there exist a root between t and a
– else if f(t) *f (b) < 0, there exist a root between t and b Repeat

above three steps until f(t) = 0.

Example:
Find the roots of the equation x2 - 6 = 0

MATLAB Code:
myFunction=@(x) x^15-8
x_lower=1
x_upper=3
x_mid=(x_lower+x_upper)/2
while abs(myFunction(x_mid))>0.001
if myFunction(x_mid)*myFunction(x_upper)<0
x_lower=x_mid
else
x_upper=x_mid
end
x_mid=(x_lower+x_upper)/2
end
fprintf('The root is %g\n', x_mid)

Solution:
>> BB
myFunction = @(x)x^15-8
x_lower =1
x_upper =3
x_mid =2
x_upper = 2
x_mid =1.1487
x_upper =1.1487
x_mid =1.1487
x_upper =1.1487
x_mid =1.1487
The root is 1.1487
4. 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

Formula:

Example:
Find the roots of the given Equation cos (x)

MATLAB Code:
% Secant Algorithm
% Find the root of y = cos(x) from 0 to pi.
f = @(x) (cos(x));
p0 = input('Enter 1st approximation, p0: ');
p1 = input('Enter 2nd approximation, p1: ');
n = input('Enter no. of iterations, n: ');
tol = input('Enter tolerance, tol: ');
i = 2;
f0 = f(p0);
f1 = f(p1);
while i <= n
p = p1-f1*(p1-p0)/(f1-f0);
fp = f(p);
if abs(p-p1) < tol
fprintf('\nApproximate solution p = %11.8f\n\n',p);
break;
else
i = i+1;
p0 = p1;
f0 = f1;
p1 = p;
f1 = fp;
end
end

Solution:
Enter 1st approximation, p0: 2
Enter 2nd approximation, p1: 4
Enter no. of iterations, n: 25
Enter tolerance, tol: 0.0005
Approximate solution p = -1.57079633

5. Regula-Falsi method
Regula Falsi Method is use to find the root of non-linear equation in numerical method

Formula
Find points a and b such that a < b and f(a) * f(b) < 0.
Take the interval [a, b] and determine the next value of x1.
If f(x1) = 0 then x1 is an exact root, else if f(x1) * f(b) < 0 then let a = x1, else if f(a) * f(x1) < 0 then
let b = x1.
Repeat steps 2 & 3 until f(xi) = 0 or |f(xi)| £ DOA, where DOA stands for degree of accuracy.

Example
Find the roots of the given Function sin(x)+cos(x)+exp(x)-8

MATLAB Code
% 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: ');
% 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
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
fb = eval(subs(y,x,b));
else
a =c;
fa = eval(subs(y,x,a));
end
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end

Solution

Enter non-linear equations: sin(x)+cos(x)+exp(x)-8


Enter first guess: 2
Enter second guess: 3
Tolerable error: 0.0005
a b c f(c)
2.000000 3.000000 2.010374 -0.054516
2.010374 3.000000 2.015152 -0.025119
2.015152 3.000000 2.017349 -0.011551
2.017349 3.000000 2.018358 -0.005306
2.018358 3.000000 2.018821 -0.002437
2.018821 3.000000 2.019034 -0.001119
2.019034 3.000000 2.019132 -0.000514
Root is: 2.019177>>

You might also like