0% found this document useful (0 votes)
63 views11 pages

Lab 5 - Bisection Method Newton Raphson Method

Numerical Methods Lab 5

Uploaded by

Maheen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views11 pages

Lab 5 - Bisection Method Newton Raphson Method

Numerical Methods Lab 5

Uploaded by

Maheen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Department of Computing

MATH 352: Numerical Methods

Class: BESE-13AB

Lab 5: Bisection Method & Newton Raphson Method

Date: March 04 & 06, 2024


Time: 2:00 pm-5:00 pm

Instructor: Hina Munir Dutt


Lab Engineer: Junaid Sajid
Submitted By: Maheen Akhtar Khan, BESE-13A, 429419

MATH333: Numerical Analysis Page 1


Introduction
MATLAB, which stands for MATrix LABoratory, is a state-of-the-art mathematical software
package, which is used extensively in both academia and industry.

Objectives
The purpose of this lab is to get familiar with Bisection Method and Newton Raphson Method

Tools/Software Requirement
Matlab R2016a or Online Matlab

Bisection Method Using Function

Description

MATH333: Numerical Analysis Page 2


Take Input
a=input('Enter function’);
f=inline(a);
xl=input('Enter lower guess:') ;
xu=input('Enter upper guess:');
tol=input('Enter tolerance(recommended 0.001):');

Checking First Condition


%write code for the condition if f(xu)*f(xl) <0 is false then enter upper and
lower Guess again until the above condition becomes true, if true then break;

for i=2:1000

%write missing part of code by following and understanding the flow chart

% xnew(1)=0;
xnew(i)=xr;
if abs(xnew(i)-xnew(i-1)/xnew(i))<tol,break,end
end
str = ['The required root of the equation is: ', num2str(xr), '']

Newton Raphson Method

Description
For Newton Raphson method, the function must be differentiable. The formula for Newton
Raphson method is:

Main Steps

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 x 2
x2 = x1 – f(x1)/f’(x1)
5. Repeat the process for x3, x4… till the actual root of the function is obtained,
fulfilling the tolerance of error.

% Program Code of Newton-Raphson Method in MATLAB


MATH333: Numerical Analysis Page 3
a=input('Enter the function in the form of variable x:','s');
x(1)=input('Enter Initial Guess:');
error=input('Enter allowed Error:');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for i=1:100
(compute (x(i+1)), which is the value of root using the formula, note that the value of
differential is stored in d. Then compute err(i), which is the value of error
write if check to compare it with tolerance error value and break if the value is less than
error.)
end
root=x(i)

Lab Task # 1
The bisection method in mathematics is a root-finding method that repeatedly bisects an interval
and then selects a subinterval in which a root must lie for further processing. It is a very simple
and robust method, but it is also relatively slow.

Implement the bisection method using functions.

Code:

function outStr = bisection()

% Get user input for the function


a = input('Enter the function in terms of x (e.g., 3*x.^3 + 2*x.^2): ', 's');
fu = inline(a, 'x');

% Define your function f here


function result = f(x)
result = fu(x);
end

% Rest of your bisection method code here


xl = input('Enter the initial value for guess interval:');
xu = input('Enter the last value for guess interval:');
tol = input('Enter the error allowed:');

if f(xu) * f(xl) < 0


% Your code for the case when the condition is true
else
fprintf('The guess entered is incorrect. Please enter the new guess\n');
MATH333: Numerical Analysis Page 4
xl = input('Enter the initial value for guess interval:\n');
xu = input('Enter the last value for guess interval:\n');
end

for i = 2 : 1000
xr = (xu + xl) / 2;

if f(xu) * f(xr) < 0


xl = xr;
else
xu = xr;
end

if f(xl) * f(xr) < 0


xu = xr;
else
xl = xr;
end

xnew(1) = 0;
xnew(i) = xr;

if abs((xnew(i) - xnew(i-1)) / xnew(i)) < tol


break;
end
end

outStr = ['Required root for the input equation is: ', num2str(xr), ''];

end

MATH333: Numerical Analysis Page 5


Screenshots:

MATH333: Numerical Analysis Page 6


Output:

Lab Task #2
Implement Newton Raphson method as function. Take function, initial guess and tolerance as
input from user. Calculate its derivative and find its roots.

Note: For derivative, investigate “sym” and “diff” command.

MATH333: Numerical Analysis Page 7


Code:

function newtonRaphson()

% Take inputs from the user


a = input('Enter the function in the form of variable x:', 's');
x(1) = input('Enter Initial Guess:');
error = input('Enter allowed Error:');

% Convert the function string to a function handle


f = inline(a);

% Calculate the derivative symbolically and convert to a function handle


dif = diff(str2sym(a));
d = inline(char(dif));

% Newton-Raphson method
for i = 1:100
f1 = f(x(1));
f1_der = d(x(1));
Z = x(1) - f1 / f1_der;
er = abs(Z - x(1));

if er < error
break;
end

x(1) = Z;
end

% Display the result


fprintf('Required Root for the input function is: %f \n', Z);
fprintf('Number of Iterations: %d\n', i);

end

MATH333: Numerical Analysis Page 8


Screenshots:

MATH333: Numerical Analysis Page 9


MATH333: Numerical Analysis Page 10
Output:

Deliverables
Submit single word file with matlab code and screen shot of Output.

MATH333: Numerical Analysis Page 11

You might also like