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

Lab 4

Nc lab reports with examples

Uploaded by

fa21-bee-030
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)
5 views6 pages

Lab 4

Nc lab reports with examples

Uploaded by

fa21-bee-030
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

EXPERIMENT NUMBER: 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

General Formula for Newton’s method:

Stopping Criteria:

Newton’s method condition for next iteration:

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

% Prompt user for function, initial point, and tolerance

func = input('Enter the function in terms of x (e.g., @(x) x^2 - 4): ');

x0 = input('Enter the initial point (x0): ');

tolerance = input('Enter the tolerance value: ');

max_iterations = input('Enter the maximum number of iterations: ');

% Create symbolic variable

syms x;

% Define the function and its derivatives

f = func(x);

f_prime = diff(f, x); % First derivative

f_double_prime = diff(f_prime, x); % Second derivative

% Convert symbolic functions to MATLAB functions

f_func = matlabFunction(f);

f_prime_func = matlabFunction(f_prime);

f_double_prime_func = matlabFunction(f_double_prime);

3|Page
EXPERIMENT NUMBER: 4

% Display the first and second derivatives

fprintf('First Derivative: %s\n', char(f_prime));

fprintf('Second Derivative: %s\n', char(f_double_prime));

% Initialize variables

fprintf('Iteration\tRoot (x_n)\t\tf(x_n)\n');

for iter = 1:max_iterations

% Calculate function value and first derivative at x0

f_x0 = f_func(x0);

f_prime_x0 = f_prime_func(x0);

% Check if the first derivative is zero to avoid division by zero

if abs(f_prime_x0) < 1e-12

error('Derivative is too close to zero. No convergence possible.');

end

% Calculate the next root using the Newton-Raphson formula

x1 = x0 - (f_x0 / f_prime_x0);

f_x1 = f_func(x1);

% Display the current iteration results

fprintf('%d\t\t%.6f\t\t%.6f\n', iter, x1, f_x1);

% Check for convergence

if abs(x1 - x0) < tolerance || abs(f_x1) < tolerance

fprintf('Converged to root: %.6f with f(x_n) = %.6f\n', x1, f_x1);

return; % Exit the loop if the tolerance is met

end

4|Page
EXPERIMENT NUMBER: 4
% Update x0 for the next iteration

x0 = x1;

end

% If the maximum number of iterations is reached without convergence

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

You might also like