Newton Raphson by using MATLAB

The Newton-Raphson method is a numerical method used for finding the roots of a differentiable function. It is an iterative method that starts with an initial guess of the root and refines the guess with each iteration until the desired level of accuracy is achieved.

The method is based on the following iterative formula:

``x_{n+1} = x_n - f(x_n) / f'(x_n)``

where ‘`x_n`‘ is the current approximation of the root, ‘`f(x_n)`‘ is the function evaluated at ‘`x_n`‘, and ‘`f'(x_n)`‘ is the derivative of the function evaluated at ‘`x_n`‘.

To use the method, one starts with an initial guess `x_0` and applies the above formula to generate successive approximations ‘`x_1`‘, ‘`x_2`‘, ‘`x_3`‘, and so on, until the desired level of accuracy is achieved. The method may converge to a root or diverge to infinity, depending on the function and the initial guess.

The method has many applications in various fields, including physics, engineering, finance, and computer science. It is used for solving equations that cannot be solved analytically, such as finding the roots of a polynomial or a transcendental equation.

Newton Raphson Method MATLAB Code

In MATLAB, you can implement the Newton-Raphson method using a loop that iteratively applies the formula described above. Here is an example implementation for finding the root of a function ‘`f(x)`‘:

``````function x = newton_raphson(f, df, x0, tol, max_iter)
% Newton-Raphson method for finding the root of a function f(x)
% Inputs:
%   f: the function to find the root of
%   df: the derivative of the function f(x)
%   x0: the initial guess for the root
%   tol: the tolerance for the root (i.e., how close x_n is to the actual root)
%   max_iter: the maximum number of iterations
% Outputs:
%   x: the approximated root of the function f(x)

% Initialize x_n to be the initial guess x0
x = x0;
iter = 0;

% Loop until the desired level of accuracy is achieved or maximum iterations reached
while abs(f(x)) > tol && iter < max_iter
% Compute the next approximation using the Newton-Raphson formula
x = x - f(x) / df(x);

% Increment the iteration count
iter = iter + 1;
end

% Check if the maximum number of iterations was reached without achieving desired level of accuracy
if iter == max_iter
disp('Maximum iterations reached without finding a root within tolerance.')
end
``````

To use this function, you would define your function ‘`f(x)`‘ and it’s derivative ‘`df(x)`‘, specify an initial guess ‘`x0`‘, a tolerance ‘`tol`‘, and a maximum number of iterations ‘`max_iter`‘. For example, if we want to find the root of ‘`f(x) = x^2 - 2`‘ starting from an initial guess of ‘`x0 = 1`‘, with a tolerance of ‘`tol = 1e-6`‘, and a maximum of ‘`max_iter = 100`‘ iterations, we would call the function like this:

``````f = @(x) x^2 - 2;
df = @(x) 2*x;
x0 = 1;
tol = 1e-6;
max_iter = 100;
root = newton_raphson(f, df, x0, tol, max_iter);
``````

After running this code, the variable ‘`root`‘ will contain the approximated root of the function ‘`f(x)`‘.

Discover more from ReadsBlog

Subscribe to get the latest posts to your email.