0% found this document useful (0 votes)
18 views6 pages

Lab 4

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)
18 views6 pages

Lab 4

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/ 6

Rajshahi University of Engineering & Technology

Department of Electrical & Electronic Engineering


Course No: EEE 3110

Course Title: Computational Methods in Engineering Sessional

Experiment No: 04

Experiment Name: Solving non-linear equations using Newton Raphson Method and
Secant Method.

SUBMITTED BY SUBMITTED TO

Name: Al-Momin Hosen Sushanto Bosak


Roll: 2101102 Lecturer
Section: B Electrical & Electronic Engineering
Dept.: Electrical & Electronic Engineering Rajshahi University of Engineering &
Session: 2021-2022 Technology (RUET)

Date of Experiment: 24/02/2025

Date of Submission: 21/04/2025

Exp no : 04
Name of the Experiment : Solving non-linear equations using Newton Raphson Method and
Secant Method.

Objectives:

1. To implement the Newton-Raphson and Secant methods for solving nonlinear equations.
2. To iteratively refine initial approximations until a desired level of accuracy is reached.
3. To compare both methods by observing the convergence behavior and error in each
iteration.
4. To present the approximated roots and errors for each iteration in a clear tabular format
using MATLAB.

Theory (Newton-Raphson Method):

The Newton-Raphson method is a widely used iterative technique for finding the real root of a
nonlinear equation f(x)=0.It is a non-bracketing method, meaning it requires only one initial
guess. The method is based on the Taylor series expansion of the function and generally
converges faster than bracketing methods.

Starting from an initial approximation x0, the next approximation is obtained using the formula:
f ( xn )
Xn+1¿ Xn−
f ( xn )

This formula is derived by neglecting higher-order terms in the Taylor series expansion of f(x).
The process is repeated until the error becomes smaller than a pre-defined tolerance, indicating
convergence to the actual root. A graphical depiction of the Newton Rapshon method is shown in
Fig 4.1

Fig-4.1 :
Graphical depiction of the Newton Rapshon Method .

Theory (Secant Method):


The Secant method is a numerical technique used to find the root of a nonlinear equation f(x)=0.It is
a non-bracketing method and a simplified form of the Newton-Raphson method. Unlike Newton-
Raphson, it does not require the derivative of the function, making it more convenient in some
cases.Instead of using the derivative, it approximates it using two previous points. The formula for
the Secant method is:

f ( X )( Xi−Xi−1)
Xi+1 = Xi -
f ( Xi )−f (Xi−1)

This method requires two initial guesses x0 and x1. Successive approximations are computed
using the formula until the desired accuracy is reached. Although its convergence is generally
slower than the Newton-Raphson method, it is useful when the derivative is difficult or
impossible to obtain. A graphical depiction of the secant method is shown in Fig 4.2

Fig-4.2 : Graphical depiction of the Secant Method


MATLAB Program (Newton-Raphson Method):

clc;
clear;

syms x;
f_sym = x^3 - x - 1;
df_sym = diff(f_sym, x);

f = matlabFunction(f_sym);
df = matlabFunction(df_sym);

Xinitial = input('Enter your initial value: ');

if df(Xinitial) == 0
fprintf('Derivative is zero at this point, choose another initial value.\
n');
Xinitial = input('Enter a new initial value: ');
end

fprintf('Iteration\t x\t\t f(x)\t\t Error\n');

for i = 1:100
fx = f(Xinitial);
dfx = df(Xinitial);

if dfx == 0
fprintf('Derivative became zero, stopping iteration.\n');
break;
end

Xr = Xinitial - fx / dfx;
error = abs(Xr - Xinitial);

fprintf('%d\t %.6f\t %.6f\t %.6f\n', i, Xinitial, fx, error);

if error <= 0.001


fprintf('Converged to root: %.6f in %d iterations.\n', Xr, i);
break;
end
Xinitial = Xr;
end

Results Shown in the command window(Newton-Raphson Method):

Iteration x f(x) Error


1 2.000000 5.000000 0.454545
2 1.545455 1.145755 0.185840
3 1.359615 0.153705 0.033814
4 1.325801 0.004625 0.001082
5 1.324719 0.000005 0.000001
Converged to root: 1.324718 in 5 iterations.
MATLAB Program ( Secant Method):

clc;
clear;

f = @(x) x.^3 - 2*x - 5;


df = @(x) 3*x.^2 - 2;

for i = -100:100
if f(i) * f(i + 1) < 0
x0 = i;
break;
end
end

tol = 0.0001;
N = 100;
initialvalue = x0;

fprintf('Iteration\tValue of x\t\tf(x)\t\t\tError\n');

for k = 1:N
fx = f(x0);
dfx = df(x0);

if dfx == 0
fprintf('Derivative is zero. Stopping to avoid division by zero.\
n');
break;
end
x1 = x0 - fx / dfx;
err = abs(x1 - initialvalue);
fprintf('%d\t\t%.6f\t\t%.6f\t\t%.6f\n', k, x0, fx, err);

if err <= tol


fprintf('Root is x = %.6f up to error = %.6f\n', x1, err);
break;
end

initialvalue = x0;
x0 = x1;
end

Results Shown in the command window ( Secant Method):

Iteration Value of x f(x) Error


1 2.000000 -1.000000 0.100000
2 2.100000 0.061000 0.094568
3 2.094568 0.000186 0.005449
4 2.094551 0.000000 0.000017
Root is x = 2.094551 up to error = 0.000017
Discussion : In this experiment, Newton-Raphson and Secant methods were implemented in
MATLAB to find the roots of nonlinear equations. Newton-Raphson showed faster convergence
with fewer iterations when the initial guess was close to the root but required derivative calculation,
which can be complex or undefined. The Secant method, though slightly slower, proved more
flexible and robust by relying only on function evaluations and requiring two initial guesses. Both
methods were tested for accuracy and efficiency, and results were tabulated for comparison. Overall,
Newton-Raphson is ideal when derivatives are known, while the Secant method is better suited for
cases where derivatives are difficult to compute.

You might also like