4.
1 Exp No: 04
4.2 Exp name: Solving non-linear equations using Newton Raphson Method & Secant Method in
MATLAB
4.3 Objectives:
1. To understand and implement the Newton-Raphson and Secant methods for solving non-
linear equations.
2. To analyze the convergence behavior and efficiency of both methods using MATLAB.
3. To compare the accuracy and computational performance of the two numerical techniques.
4.4 Theory:
There are two types of methods to solve non-linear equations:
1. Bracketing Method. (Ex. Bisection Method)
2. Non-bracketing Method. (Ex. Newton Raphson Method, Fixed point iteration Method,
Gaussian Method)
An iterative numerical technique for determining the roots of a real-valued function is the Newton-
Raphson method, sometimes referred to as Newton's method. The Newton's Method, often known
as the Method, is a method that uses a guess for the first iteration (x0) to approximate the roots of
zeros of real-valued functions. The second iteration (x1) is then approximated to be close to the
roots.
In numerical analysis, the secant technique is a process for locating a root of a function f by using
a sequence of roots of secant lines. A finite-difference approximation of Newton's method is what
the secant method is.
The Newton Raphson Method & Secant Method will be applied in this experiment to solve a non-
linear equation given below,
𝑓(𝑥) = 𝑥 − 𝑥 − 1
4.5 Algorithm:
4.5.1 Newton Raphson Method:
Step 1: Define the symbolic variable 𝑥 and the function 𝑓(𝑥).
Step 2: Define first derivative of f(x) as df
Step 3: Initialize the initial guess for the root, x=1.
𝑓(𝑥𝑖 )
Step 4: Set, 𝑥1 = 𝑥 − ′
𝑓 (𝑥𝑖 )
Step 5: Calculate the error as |𝑥 - x|
Step 6: Set x = 𝑥
Step 7: Repeat steps 4-6 until |𝑥 - x| < 0.001
4.5.2 Secant Method:
Step 1: Define the symbolic variable 𝑥 and the function 𝑓(𝑥).
Step 2: Initialize the initial guesses for the root, x0 = 1 and 𝑋 = 1.1.
Step 3: Compute the values of 𝑓(𝑥) at the current guesses x0 and x1.
𝑓(𝑋1 )(𝑋1 −𝑋0 )
Step 4: Set, 𝑋2 = 𝑋1 −
𝑓(𝑋0 )−𝑓(𝑋1 )
Step 5: Calculate the error as |𝑥 - 𝑋 |
Step 6: Set, , 𝑋 =𝑋 , 𝑋 =𝑋
Step 7: Repeat steps 3-6 until |𝑥 - 𝑋 |< 0.001
4.6 MATLAB Code & Output:
4.6.1 Newton Raphson Method:
clc
clear
syms x;
f=@(x)(𝑥 − 𝑥 − 1);
df=diff(f);
x=1;
fprintf('Iteration \t x \t\t f(x)\t\t \t\t
Error\n')
for i=1:100;
xdf=vpa(subs(df));
x1=x-(vpa(subs(f))/vpa(subs(df)));
if i==1
error=inf;
else
error=abs(x1-x);
end
x=x1;
fprintf('\t%d \t %f \t %f \t %f \n',i,x,f(x), error);
if error<=0.001;
break;
end
Output:
Iteration x f(x) Error
1 1.500000 0.875000 Inf
2 1.347826 0.100682 0.152174
3 1.325200 0.002058 0.022626
4 1.324718 0.000001 0.000482
4.6.2 Secant Raphson Method:
clc;
clear all;
syms x;
f(x) =(x^3-x-1);
x0 = 1;
x1 = 1.1;
fprintf('Iteration \t x \t\t f(x)\t\t \t\t
Error\n')
for i = 1:100
fx0 = double(subs(f, x, x0));
fx1 = double(subs(f, x, x1));
x2 = x1 - ((x1 - x0) / (fx1 - fx0)) * fx1;
if i == 1
error = inf;
else
error = abs(x2 - x1);
end
x0 = x1;
x1 = x2;
fprintf('\t%d \t %f \t %f \t %f \n', i, x2, double(subs(f,
x, x2)), error);
if error <= 0.001
break;
end
end
Output:
Iteration x f(x) Error
1 1.432900 0.509136 Inf
2 1.300292 -0.101811 0.132608
3 1.322391 -0.009904 0.022099
4 1.324772 0.000230 0.002381
5 1.324718 -0.000000 0.000054
4.7 Discussion & Conclusion:
The Newton-Raphson and Secant methods were successfully used in MATLAB to solve the non-
linear equation f(x) = x^3-x-1. Both approaches eventually reached convergence to the root within
a tolerance of 0.001 after multiple iterations. Rapid convergence was achieved by iteratively
improving the initial estimations using function derivatives in the Newton-Raphson approach. In
the Secant technique, the root was approximated using successive function values, which led to a
slightly slower convergence. Notwithstanding their differences, both approaches' applicability in
numerically resolving non-linear equations was demonstrated. Ease of implementation and
computing efficiency were two aspects that affected the comparison of approaches. Overall, this
experiment showed how flexible and effective numerical techniques are for resolving non-linear
equations in MATLAB.