Professional Documents
Culture Documents
908 Assignment 03 MATLAB
908 Assignment 03 MATLAB
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:
𝒇(𝑿𝒏)
𝑿𝒏+𝟏 = 𝑿𝒏 − 𝒇′(𝑿𝒏)
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.
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
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