Lab04: Newton Raphson and Secant Method
Lab04: Newton Raphson and Secant Method
Problem Statement:
1. Implementation of Newton Rapson Method using MATLAB
2. Implementation of Secant Method using MATLAB
Problem-01:
Theory: The Newton-Raphson method, introduced by Isaac Newton and Joseph Raphson, is a
powerful and widely used algorithm for finding the roots of a real-valued function, i.e., points
where f(x) = 0. The method employs a process of successive approximations, leveraging the fact
that a function's behavior near a root can be well-approximated by its tangent line. Starting with
an initial guess x0, the method computes the next approximation x n+1 using the formula:
𝑓(𝑥 )
𝑥 =𝑥 −
𝑓 (𝑥 )
Here, f'(xn) represents the derivative of the function at xn, and the goal is to bring f(xn) closer to
zero with each iteration. The method is renowned for its quadratic convergence, meaning that the
number of correct digits in the approximation roughly doubles with each step when the guess is
close to the true root and the function is well-behaved. This makes it particularly efficient for
functions that are continuous and differentiable near the root. Despite its strengths, the Newton-
Raphson method does have limitations. If the derivative f'(x) is zero or near zero at some point,
the method may fail or produce large errors, causing divergence. Additionally, if the initial guess
is too far from the actual root, the method may not converge or might converge to a different root
than intended, especially in cases where the function has multiple roots. Nonetheless, with a good
initial guess, the Newton-Raphson method is one of the fastest root-finding techniques available,
making it a preferred choice in many scientific and engineering applications, including
optimization problems, fluid dynamics, and electrical circuit analysis.
Algorithm:
1. Start
2. Define function as f(x)
3. Define first derivative of f(x) as g(x)
4. Input initial guess (x0), tolerable error (e)
and maximum iteration (N)
5. Initialize iteration counter i = 1
6. If g(x0) = 0 then print "Mathematical Error"
and goto (12) otherwise goto (7)
( )
7. Calculate 𝑥 = 𝑥 − ( )
Tools:
1. MATLAB (Version R2024a)
Code:
func_str = input('Enter the function f(x) (e.g. x^2 - 4): ', 's');
f_sym = str2sym(func_str);
f = matlabFunction(f_sym);
df_sym = diff(f_sym, x);
df = matlabFunction(df_sym);
x0 = input('Enter the initial guess x0: ');
tolerance = input('Enter the tolerance: ');
max_iterations = input('Enter the maximum number of iterations: ');
iter_count = 0;
x_old = x0;
fprintf('\n%-10s %-15s %-15s %-15s\n', 'Iteration', 'x_old', 'x_new', 'Difference');
fprintf('-------------------------------------------------------------\n');
while iter_count < max_iterations
iter_count = iter_count + 1;
x_new = x_old - f(x_old) / df(x_old);
difference = abs(x_new - x_old);
fprintf('%-10d %-15.6f %-15.6f %-15.6f\n', iter_count, x_old, x_new, difference);
if difference < tolerance
break;
end
x_old = x_new;
end
if iter_count == max_iterations
fprintf('\nMaximum number of iterations reached.\n');
end
fprintf('\nThe approximate root is: %f\n', x_new);
Output:
Discussion: The provided code implements the Newton-Raphson method for finding the root of
a nonlinear function in MATLAB. It starts by taking the function f(x) as a string input, which is
then converted into a symbolic expression and subsequently into a MATLAB function for numeric
evaluation. The derivative of the function is also computed symbolically and converted similarly.
The code prompts the user for the initial guess, tolerance for convergence, and the maximum
number of iterations. An iteration loop runs up to the specified maximum, calculating a new
approximation for the root and checking the difference between the new and old estimates for
convergence. Results are displayed in a tabular format, including the iteration number, previous
estimate, new estimate, and the difference. If the difference falls below the tolerance, the loop
breaks; if the maximum iterations are reached without convergence, a message is displayed.
Finally, the approximate root is printed, providing clear feedback on the convergence process.
Problem-02:
Theory: The secant method is a numerical technique employed to find the roots of a real-valued
function, especially when the derivative is difficult to compute. It utilizes two initial
approximations of the root, x0 and x1, and approximates the derivative by calculating the slope of
the secant line that connects the points (x0, f(x0)) and (x1, f(x1)). The iterative formula for updating
the approximation is given by
(𝑥 − 𝑥 ) × 𝑓(𝑥 )
𝑥 =𝑥 −
𝑓(𝑥 ) − 𝑓(𝑥 )
The method begins by selecting initial guesses and computing the corresponding function values.
It then iteratively updates the approximations while checking for convergence, defined by whether
the difference between successive approximations falls below a predetermined tolerance. The
secant method generally exhibits a convergence order of approximately \( \phi \) (the golden ratio),
which is faster than the linear convergence of methods like bisection. However, it requires careful
selection of initial guesses to avoid divergence. This method finds applications across various
fields, including engineering, physics, and finance, where it is essential for solving nonlinear
equations and analyzing practical problems.
Algorithm:
1. Start
2. Define function as f(x)
3. Input initial guesses (x0 and x1),
tolerable error (e) and maximum iteration (N)
4. Initialize iteration counter i = 1
5. If f(x0) = f(x1) then print "Mathematical Error"
and goto (11) otherwise goto (6)
( )× ( )
6. Calcualte 𝑥 = 𝑥 − ( ) ( )
Code:
func_str = input('Enter the function f(x) (e.g. x^2 - 4): ', 's');
f_sym = str2sym(func_str);
f = matlabFunction(f_sym);
x0 = input('Enter the initial guess x0: ');
x1 = input('Enter the second initial guess x1: ');
tolerance = input('Enter the tolerance: ');
max_iterations = input('Enter the maximum number of iterations: ');
iter_count = 0;
fprintf('\n%-10s %-15s %-15s %-15s\n', 'Iteration', 'x0', 'x1', 'Difference');
fprintf('-------------------------------------------------------------\n');
while iter_count < max_iterations
iter_count = iter_count + 1;
f_x0 = f(x0);
f_x1 = f(x1);
x_new = x1 - f_x1 * ((x1 - x0) / (f_x1 - f_x0));
difference = abs(x_new - x1);
fprintf('%-10d %-15.6f %-15.6f %-15.6f\n', iter_count, x0, x1, difference);
if difference < tolerance
break;
end
x0 = x1;
x1 = x_new;
end
if iter_count == max_iterations
fprintf('\nMaximum number of iterations reached.\n');
end
fprintf('\nThe approximate root is: %f\n', x_new);
Output:
Discussion: This code uses the one-point iterative method (fixed-point iteration) to find the root
of a function. The user provides a function g(x) in the form x = g(x), along with an initial guess
x0, a tolerance for accuracy, and a maximum number of iterations. Starting from x 0, the code
calculates new approximations of the root using the formula x n+1 = g(xn) until the difference
between consecutive approximations is smaller than the specified tolerance or the maximum
iterations are reached. If the approximations converge, the code outputs the root; if not, it indicates
that the maximum number of iterations has been reached. The method's effectiveness relies on the
function's characteristics and the initial guess chosen by the user.
References:
[1] “False Position Method (Plot) - MATLAB Answers - MATLAB Central.” Accessed: Oct.
01, 2024. [Online]. Available:
https://www.mathworks.com/matlabcentral/answers/780752-false-position-method-plot
[2] “Regula Falsi (False Position) Method Using MATLAB.” Accessed: Oct. 01, 2024.
[Online]. Available: https://www.codesansar.com/numerical-methods/regula-falsi-or-false-
position-method-using-matlab-output.htm
[3] “fixed point Iterative method for finding root of an equation - MATLAB Answers -
MATLAB Central.” Accessed: Oct. 01, 2024. [Online]. Available:
https://www.mathworks.com/matlabcentral/answers/602329-fixed-point-iterative-method-
for-finding-root-of-an-equation
[4] “(3851) Fixed Point Iteration Method Example and MATLAB code | Fixed Point Iteration
Converges & Diverges - YouTube.” Accessed: Oct. 01, 2024. [Online]. Available:
https://www.youtube.com/watch?v=URNykaoKsKE