Categories

# Newton Raphson Matlab

Newton Raphson’s method is used to find the root of an equation in mathematics & numerical problems. This method is also referred to as the secant method’s limiting case. The Newton Raphson method uses an initial couple of terms of Taylor’s series. It finds its utility in polynomials where the 1st derivative is a large term. It is also very useful in optimizing the root found using other root-finding techniques/methods. Please keep in mind that the Newton Raphson method can be applied only if the input function/polynomial is differentiable. In MATLAB, we do not have a pre-defined Newton Raphson method, so we create one to get the roots using this method. In this topic, we are going to learn about Newton Raphson Matlab.

## Examples of Newton Raphson Matlab

Below are the examples mentioned:

## Example 1:

In this example, we will take a polynomial function of degree 3 and will find its root using the Newton Raphson method.

For our first example, we will input the following values:

1. Pass the decimal places as 4
2. Pass the first guess as 1.5
3. Pass the allowed error as 0.001

## Code:

``````syms x
[Initializing the variable ‘x’] Fx = x.^3 - x - 1
[Entering the input function] Di = diff (Fx)
[Differentiating the input function] decimalPlaces = input ('Enter the required decimal places in the output:');
[Asking user to enter decimal places required in the output] allowedError = input ('Enter the error allowed:');
[Asking user to enter tolerable error] A (1) = input ('Enter the initial guess value:');
[Asking user to enter ‘Initial value’ which is used as the guess value for 1stiteration] for i = 1 : 100
f1 = vpa(subs(Fx, x, A1));
[Calculating value of input function at A1] f1_der =vpa(subs(Di, x, A1));
[Calculating the function derivative value at A1] Z = A1 - f1/f1_der;
[Formula for Newton Raphson method] er=abs(Z-A1);
if er<allowedError
[Logic to restrict the error to allowedError] break
end
A1=Z;
end
Z = Z - rem(Z,10^-decimalPlaces);
[Restricting the output to required number of decimal places] fprintf('Required Root for the input function is : %f \n', Z);
fprintf('Number of Iterations : %d\n',i);``````

## Output:

As we can see in the output, we have obtained the root of our input function as 1.3247. The result is obtained after 3 iterations.

## Example 2:

In this example, we will take another polynomial function of degree 3 and will find its root using the Newton Raphson method.

For this example, we will input the following values:

1. Pass the decimal places as 4
2. Pass the first guess as -1
3. Pass the allowed error as 0.001

## Code:

``````syms x
[Initializing the variable ‘x’] Fx = x.^3 – x.^2 - 1
[Entering the input function] Di = diff (Fx)
[Differentiating the input function] decimalPlaces = input ('Enter the required decimal places in the output:');
[Asking user to enter decimal places required in the output] allowedError = input ('Enter the error allowed:');
[Asking user to enter tolerable error] A (1) = input ('Enter the initial guess value:');
[Asking user to enter ‘Initial value’ which is used as the guess value for 1stiteration] for i = 1 : 100
f1 = vpa(subs(Fx, x, A1));
[Calculating value of input function at A1] f1_der =vpa(subs(Di, x, A1));
[Calculating the function derivative value at A1] Z = A1 - f1/f1_der;
[Formula for Newton Raphson method] er=abs(Z-A1);
if er<allowedError
[Logic to restrict the error to allowedError] break
end
A1=Z;
end
Z = Z - rem(Z,10^-decimalPlaces);
[Restricting the output to required number of decimal places] fprintf('Required Root for the input function is : %f \n', Z);
fprintf('Number of Iterations : %d\n',i);``````

## Output:

As we can see in the output, we have obtained the root of our input function as 1.4655. The result is obtained after 15 iterations.

## Example #3

In this example, we will take a polynomial function of cos and sine and will find its root using the Newton Raphson method.

For this example, we will input the following values:

1. Pass the decimal places as 4
2. Pass the first guess as 0
3. Pass the allowed error as 0.001

## Code:

``````syms x
[Initializing the variable ‘x’] Fx = cos(x) + 2*sin(x) + x^2
[Entering the input function] Di = diff (Fx)
[Differentiating the input function] decimalPlaces = input ('Enter the required decimal places in the output:');
[Asking user to enter decimal places required in the output] allowedError = input ('Enter the error allowed:');
[Asking user to enter tolerable error] A (1) = input ('Enter the initial guess value:');
[Asking user to enter ‘Initial value’ which is used as the guess value for 1stiteration] for i = 1 : 100
f1 = vpa(subs(Fx, x, A1));
[Calculating value of input function at A1] f1_der =vpa(subs(Di, x, A1));
[Calculating the function derivative value at A1] Z = A1 - f1/f1_der;
[Formula for Newton Raphson method] er=abs(Z-A1);
if er<allowedError
[Logic to restrict the error to allowedError] break
end
A1=Z;
end
Z = Z - rem(Z,10^-decimalPlaces);
[Restricting the output to required number of decimal places] fprintf('Required Root for the input function is : %f \n', Z);
fprintf('Number of Iterations : %d\n',i);``````

## Output:

As we can see in the output, we have obtained the root of our input function as -0.6592. The result is obtained after 4 iterations.