EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Newton Raphson Matlab
 

Newton Raphson Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Newton Raphson Matlab

 

 

Introduction of 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

newton raphson matlab output 1

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:

newton raphson matlab output 2

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:

newton raphson matlab output 3

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.

Conclusion

Newton Raphson method is used to find the root of any polynomial function. As there is no direct function for Newton Raphson rule in MATLAB, we define the code or logic for it manually. Newton Raphson method is much faster in root-finding when compared with similar methods like bisection method or secant method

Recommended Articles

This is a guide to Newton Raphson Matlab. Here we discuss the introduction to Newton Raphson Matlab along with programming examples to understand better. You may also have a look at the following articles to learn more –

  1. Matlab Sine Wave
  2. Curve Fitting Matlab
  3. Matlab Plot Colors
  4. Matlab mesh()

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW