{"id":26337,"date":"2017-10-26T21:05:03","date_gmt":"2017-10-26T15:35:03","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26337"},"modified":"2017-10-26T21:05:03","modified_gmt":"2017-10-26T15:35:03","slug":"c-programming-program-newton-raphson-method","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-program-newton-raphson-method\/","title":{"rendered":"C++ Programming &#8211; Program for Newton Raphson Method"},"content":{"rendered":"<p>Given a function f(x) on floating number x and an initial guess for root, find root of function in interval. Here f(x) represents algebraic or transcendental equation.<\/p>\n<p>For simplicity, we have assumed that derivative of function is also provided as input.<\/p>\n<p>Example:<\/p>\n<p>Input: A function of x (for example x3 \u2013 x2 + 2),<br \/>\nderivative function of x (3&#215;2 \u2013 3x for above example)<br \/>\nand an initial guess x0 = -20<br \/>\nOutput: The value of root is : -1.00<br \/>\nOR any other value close to root.<br \/>\nWe have discussed below methods to find root in set 1 and set 2<br \/>\nSet 1: The Bisection Method<br \/>\nSet 2: The Method Of False Position<\/p>\n<p>Comparison with above two methods:<\/p>\n<p>In previous methods, we were given an interval. Here we are required an initial guess value of root.<br \/>\nThe previous two methods are guaranteed to converge, Newton Rahhson may not converge in some cases.<br \/>\nNewton Raphson method requires derivative. Some functions may be difficult to<br \/>\nimpossible to differentiate.<br \/>\nFor many problems, Newton Raphson method converges faster than the above two methods.<br \/>\nAlso, it can identify repeated roots, since it does not look for changes in the sign of f(x) explicitly<br \/>\nThe formula:<br \/>\nStarting from initial guess x1, the Newton Raphson method uses below formula to find next value of x, i.e., xn+1 from previous value xn.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26342\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/newtonraphsonformula.png\" alt=\"nrm\" width=\"165\" height=\"48\" \/><\/p>\n<p><strong>Algorithm:<\/strong><br \/>\nInput: initial x, func(x), derivFunc(x)<br \/>\nOutput: Root of Func()<\/p>\n<ol>\n<li>Compute values of func(x) and derivFunc(x) for given initial x<\/li>\n<li>Compute h: h = func(x) \/ derivFunc(x)<\/li>\n<li>While h is greater than allowed error \u03b5\n<ol>\n<li type=\"a\">h = func(x) \/ derivFunc(x)<\/li>\n<li type=\"a\">x = x \u2013 h<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n[ad type=&#8221;banner&#8221;]\n<p>Below is C++ implementation of above algorithm.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program for implementation of Newton Raphson Method for<br\/>\/\/ solving equations<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>#define EPSILON 0.001<br\/>using namespace std;<br\/> <br\/>\/\/ An example function whose solution is determined using<br\/>\/\/ Bisection Method. The function is x^3 - x^2  + 2<br\/>double func(double x)<br\/>{<br\/>    return x*x*x - x*x + 2;<br\/>}<br\/> <br\/>\/\/ Derivative of the above function which is 3*x^x - 2*x<br\/>double derivFunc(double x)<br\/>{<br\/>    return 3*x*x - 2*x;<br\/>}<br\/> <br\/>\/\/ Function to find the root<br\/>void newtonRaphson(double x)<br\/>{<br\/>    double h = func(x) \/ derivFunc(x);<br\/>    while (abs(h) &gt;= EPSILON)<br\/>    {<br\/>        h = func(x)\/derivFunc(x);<br\/>  <br\/>        \/\/ x(i+1) = x(i) - f(x) \/ f&#039;(x)  <br\/>        x = x - h;<br\/>    }<br\/> <br\/>    cout &lt;&lt; &quot;The value of the root is : &quot; &lt;&lt; x;<br\/>}<br\/> <br\/>\/\/ Driver program to test above<br\/>int main()<br\/>{<br\/>    double x0 = -20; \/\/ Initial values assumed<br\/>    newtonRaphson(x0);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>The value of root is : -1.00<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Program for Newton Raphson Method &#8211; Mathematical Algorithms &#8211; Given a function f(x) on floating number x and an initial guess for root<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,74058,83622],"tags":[78396,77752,78282,78269,78390,78397,78406,78392,77771,78403,78395,78391,78387,78398,78409,78388,78410,78393,78416,78274,78405,78402,78389,78407,78386,78414,78400,77769,78408,78289,78411,78283,78404,77004,78266,78276,78288,78412,78284,78399,78401,78413,76990,78295,78394,77757,76998,77011,78272,78415],"class_list":["post-26337","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","category-newton-raphson-method","tag-applied-numerical-methods-with-matlab-pdf","tag-bisection-method-c-program","tag-c-program-for-bisection-method","tag-c-program-of-bisection-method","tag-find-square-root","tag-fortran-program-for-newton-raphson-method","tag-gauss-jacobi-method-c-program","tag-how-to-solve-newton-raphson-method","tag-introduction-to-numerical-analysis-pdf","tag-matlab-program-for-newton-raphson-method","tag-newton-formula","tag-newton-raphson","tag-newton-raphson-formula","tag-newton-raphson-fortran-code","tag-newton-raphson-matlab-program","tag-newton-raphson-method","tag-newton-raphson-method-code","tag-newton-raphson-method-example","tag-newton-raphson-method-example-solution","tag-newton-raphson-method-formula","tag-newton-raphson-method-fortran","tag-newton-raphson-method-problems-and-solutions","tag-newton-raphson-method-theory","tag-newtons-method-calculus","tag-newtons-method-calculator","tag-numerical-analysis-and-computer-programming-pdf","tag-numerical-analysis-book-pdf","tag-numerical-analysis-pdf","tag-numerical-analysis-programming-in-c-pdf","tag-numerical-methods-c-programs","tag-numerical-methods-in-c-pdf","tag-numerical-methods-in-c-programming","tag-numerical-methods-in-c-programming-pdf","tag-numerical-methods-pdf","tag-numerical-methods-programs-in-c","tag-numerical-methods-using-c-programming","tag-numerical-methods-with-programs-in-c","tag-numerical-programming","tag-numerical-programming-in-c","tag-numerical-recipes-in-c-pdf","tag-numerical-recipes-pdf","tag-program-for-newton-raphson-method","tag-program-of-bisection-method-in-c-language","tag-program-of-newton-raphson-method-in-c","tag-raphson-method","tag-sample-c-programs-pdf","tag-secant-method-example-solved","tag-secant-method-formula","tag-simple-c-program-for-bisection-method","tag-what-is-the-newton-raphson-method"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26337"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26337\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}