Numerical Lab 4
Numerical Lab 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 = 𝑥 − ′
𝑓 (𝑥𝑖 )
f=@(x)(𝑥 − 𝑥 − 1);
df=diff(f);
x=1;
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;
if error<=0.001;
break;
end
Output:
syms x;
f(x) =(x^3-x-1);
x0 = 1;
x1 = 1.1;
for i = 1:100
fx0 = double(subs(f, x, x0));
fx1 = double(subs(f, x, x1));
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);
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