Newton-Raphson Method in MATLAB

This tutorial will discuss finding the roots of a function using the Newton-Raphson method in MATLAB.

Newton-Raphson Method in MATLAB

We use the Newton-Raphson method to find the roots of a function. The method uses a formula to approximate a continuous function with a tangent line to find an approximation for the roots of a given function.

The formula used to find the roots with the Newton-Raphson method is below.

Newton Method

The formula uses the previous value, function and its derivative to find the next root for the given function. To find the derivative of a function, we can use the diff() function of MATLAB.

We need to use a loop to get the root using the above formula. For example, if we need to find four roots, we repeat the loop four times.

We will store the given function in a variable, take its derivative using the diff() function, and store it inside a variable. We also must store all the root values inside an array to easily access the previous root value.

After that, we need to use a loop to find the roots, and we will update the values using the array of roots. We can use the subs() function to update the values in the loop and store the root value in the root array.

For example, let’s define a function and use a random function and find its first five roots using the above formula in MATLAB. See the code below.

clc
clear
fun = @(x) sin(x);
roots = mynewton(fun, 2, 5)

function output = mynewton(fun,a,n)
syms x;
z = fun(x);
derZ = diff(z);
out = zeros(1,n+1);
out(1) = a;

for idx = 1 : n
    numeratorZ = subs(z,x,out(idx));
    denominatorZ = subs(derZ,x,out(idx));
    out(idx+1) = out(idx) - double(numeratorZ)/double(denominatorZ);
end
output = out;
end

Output:

roots =

    2.0000    4.1850    2.4679    3.2662    3.1409    3.1416

Note that if the above code shows an error, save the function mynewton() in a separate .m file with the same name as the function name like mynewton.m and then create another .m file and write the top four lines of code in it to call and the function or call it from the command window of MATLAB. The test file and mynewton.m should be in the same directory.

In the above code, we defined the mynewton() function in which we have to pass the given function whose roots we want to find, the initial guess for the root, and the number of roots that we want to find.

We used the zeros() function to initialize the root array with zeros so that we can put the values after we evaluate them using the formula.

We used the subs() function to update the value of the numerator and denominator in the above formula of the Newton-Raphson method. The first argument of the subs() function is the given function whose roots we want to find.

The second argument is the old value that we want to change, and the third is the new value that we want to put in place of the old value inside the given function.

After the subs() function, the output will be in function form like cos(number), so we used the double() function to convert the values to numeric values and then apply the Newton-Raphson formula to these values to find the value of the root.

The output shows that the roots and the initial guess have been stored inside the output array. The above code is the basic representation of the Newton-Raphson method, but we can add other things like tolerance or a method to check if the algorithm converges.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.