Lab 4
Lab 4
LAB TITLE
Implementation of Newton Raphson Method
Root Finding Technique (Newton Raphson Method)
The Newton-Raphson method (also known as Newton's method) is a way to quickly find a good approximation for the
root of a real-valued function f ( x ) = 0 f(x) = 0 f(x)=0. It uses the idea that a continuous and differentiable function can
be approximated by a straight line tangent to it.
The Newton-Raphson method, or Newton Method, is a powerful technique for solving equations numerically. Like so
much of the differential calculus, it is based on the simple idea of linear approximation. The Newton Method, properly
used, usually homes in on a root with devastating efficiency.
Newton’s (or Newton-Raphson) method can be used to approximate the roots of any linear or nonlinear equation of any
degree. This is an iterative (repetitive procedure) method and it is derived with the aid of Figure:
We assume that the slope is neither zero nor infinite. Then, the slope (first derivative) at x=x 1 is:
The slope crosses the x-axix at x=x2 and y=0 . Since this point lies on the slope line. By substitution,
1|Page
EXPERIMENT NUMBER: 4
Stopping Criteria:
2|Page
EXPERIMENT NUMBER: 4
LAB TASK
Implement Newton Raphson method by using for loop method. The code should be generalized and well commented, take
Non-linear function, initial guess, tolerance, No. of iterations from the user and find out the approximated root of any given
function? Also draw flow chart for given code.
MATLAB Code:
function newton_raphson_method
func = input('Enter the function in terms of x (e.g., @(x) x^2 - 4): ');
syms x;
f = func(x);
f_func = matlabFunction(f);
f_prime_func = matlabFunction(f_prime);
f_double_prime_func = matlabFunction(f_double_prime);
3|Page
EXPERIMENT NUMBER: 4
% Initialize variables
fprintf('Iteration\tRoot (x_n)\t\tf(x_n)\n');
f_x0 = f_func(x0);
f_prime_x0 = f_prime_func(x0);
end
x1 = x0 - (f_x0 / f_prime_x0);
f_x1 = f_func(x1);
end
4|Page
EXPERIMENT NUMBER: 4
% Update x0 for the next iteration
x0 = x1;
end
fprintf('Maximum iterations reached. Approximated root: %.6f with f(x_n) = %.6f\n', x1, f_x1);
Console Window
Flow Chart
The flowchart for the Newton-Raphson method follows these key steps:
Start: Initialize the process by taking inputs (function, initial guess, tolerance, and maximum iterations).
Calculate Function and Derivative: Compute the function value and its derivative at the current guess.
Check Derivative: If the derivative is too close to zero, output an error (to prevent division by zero).
Update Root (x₁): Apply the Newton-Raphson formula to compute the next approximation of the root.
Convergence Check: If the difference between the new and previous approximations (or function value) is
within the tolerance, stop the process.
Iterate: If not converged and iterations remain, update the guess and repeat the process.
End: Output the final approximation after convergence or reaching the maximum iterations.
5|Page
EXPERIMENT NUMBER: 4
6|Page