{"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\u00d72 \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=\u201dbanner\u201d]\n<p>Below is C++ implementation of above algorithm.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20for%20implementation%20of%20Newton%20Raphson%20Method%20for%0A%2F%2F%20solving%20equations%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0A%23define%20EPSILON%200.001%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20An%20example%20function%20whose%20solution%20is%20determined%20using%0A%2F%2F%20Bisection%20Method.%20The%20function%20is%20x%5E3%20-%20x%5E2%20%20%2B%202%0Adouble%20func(double%20x)%0A%7B%0A%20%20%20%20return%20x*x*x%20-%20x*x%20%2B%202%3B%0A%7D%0A%20%0A%2F%2F%20Derivative%20of%20the%20above%20function%20which%20is%203*x%5Ex%20-%202*x%0Adouble%20derivFunc(double%20x)%0A%7B%0A%20%20%20%20return%203*x*x%20-%202*x%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20find%20the%20root%0Avoid%20newtonRaphson(double%20x)%0A%7B%0A%20%20%20%20double%20h%20%3D%20func(x)%20%2F%20derivFunc(x)%3B%0A%20%20%20%20while%20(abs(h)%20%3E%3D%20EPSILON)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20h%20%3D%20func(x)%2FderivFunc(x)%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20x(i%2B1)%20%3D%20x(i)%20-%20f(x)%20%2F%20f'(x)%20%20%0A%20%20%20%20%20%20%20%20x%20%3D%20x%20-%20h%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22The%20value%20of%20the%20root%20is%20%3A%20%22%20%3C%3C%20x%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%0Aint%20main()%0A%7B%0A%20%20%20%20double%20\u00d70%20%3D%20-20%3B%20%2F%2F%20Initial%20values%20assumed%0A%20%20%20%20newtonRaphson(x0)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++ Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>The value of root is : -1.00<\/pre>\n[ad type=\u201dbanner\u201d]\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}]}}