{"id":26325,"date":"2017-10-26T21:00:50","date_gmt":"2017-10-26T15:30:50","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26325"},"modified":"2017-10-26T21:00:50","modified_gmt":"2017-10-26T15:30:50","slug":"c-programming-program-method-false-position","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-program-method-false-position\/","title":{"rendered":"C++ Programming &#8211; Program for Method Of False Position"},"content":{"rendered":"<p>Given a function f(x) on floating number x and two numbers \u2018a\u2019 and \u2018b\u2019 such that f(a)*f(b) &lt; 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0).<\/p>\n<pre>Input: A <strong>function of x<\/strong>, for example x<sup>3<\/sup> \u2013 x<sup>2<\/sup> + 2.     \r\n       And two values: <strong>a<\/strong> = -200 and <strong>b<\/strong> = 300 such that\r\n       f(a)*f(b) &lt; 0, i.e., f(a) and f(b) have\r\n       opposite signs.\r\nOutput: The value of root is : -1.00\r\n        OR any other value close to root.\r\n<\/pre>\n<p>We strongly recommend to refer below post as a prerequisite of this post.<\/p>\n<p>In this post The Method Of False Position is discussed. This method is also known as Regula Falsi or The Method of Chords.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Similarities with Bisection Method:<\/strong><\/p>\n<ol>\n<li>Same Assumptions: This method also assumes that function is continuous in [a, b] and given two numbers \u2018a\u2019 and \u2018b\u2019 are such that f(a) * f(b) &lt; 0.<\/li>\n<li>Always Converges: like Bisection, it always converges, usually considerably faster than Bisection\u2013but sometimes very much more slowly than Bisection.<\/li>\n<\/ol>\n<p><strong>Differences with Bisection Method:<\/strong><br \/>\nIt differs in the fact that we make a chord joining the two points [a, f(a)] and [b, f(b)]. We consider the point at which the chord touches the x axis and named it as c.<\/p>\n<p><strong>Steps:<\/strong><\/p>\n<ol>\n<li>Write equation of the line connecting the two points.\n<pre>y \u2013 f(a) =  ( (f(b)-f(a))\/(b-a) )*(x-a)\r\n\r\nNow we have to find the point which touches x axis. \r\nFor that we put y = 0.\r\n\r\nso x = a - (f(a)\/(f(b)-f(a))) * (b-a)\r\n   x = (a*f(b) - b*f(a)) \/ (f(b)-f(a)) \r\n\r\nThis will be our c that is c = x.<\/pre>\n<\/li>\n<li><strong>If<\/strong> f(c) == 0, then c is the root of the solution.<\/li>\n<li><strong>Else<\/strong> f(c) != 0\n<ol>\n<li><strong>If<\/strong> value f(a)*f(c) &lt; 0 then root lies between a and c. So we recur for a and c<\/li>\n<li><strong>Else If<\/strong> f(b)*f(c) &lt; 0 then root lies between b and c. So we recur b and c.<\/li>\n<li><strong>Else<\/strong> given function doesn\u2019t follow one of assumptions.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>Since root may be a floating point number and may converge very slow in worst case, we iterate for a very large number of times such that the answer becomes closer to the root.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26327\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/regularFalsi.png\" alt=\"Falsi\" width=\"527\" height=\"473\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/regularFalsi.png 527w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/regularFalsi-300x269.png 300w\" sizes=\"(max-width: 527px) 100vw, 527px\" \/><\/p>\n<p>Following is C++ implementation.<\/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 Bisection Method for<br\/>\/\/ solving equations<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/>#define MAX_ITER 1000000<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\/>\/\/ Prints root of func(x) in interval [a, b]<br\/>void regulaFalsi(double a, double b)<br\/>{<br\/>    if (func(a) * func(b) &gt;= 0)<br\/>    {<br\/>        cout &lt;&lt; &quot;You have not assumed right a and b\\n&quot;;<br\/>        return;<br\/>    }<br\/> <br\/>    double c = a;  \/\/ Initialize result<br\/> <br\/>    for (int i=0; i &lt; MAX_ITER; i++)<br\/>    {<br\/>        \/\/ Find the point that touches x axis<br\/>        c = (a*func(b) - b*func(a))\/ (func(b) - func(a));<br\/> <br\/>        \/\/ Check if the above found point is root<br\/>        if (func(c)==0)<br\/>            break;<br\/> <br\/>        \/\/ Decide the side to repeat the steps<br\/>        else if (func(c)*func(a) &lt; 0)<br\/>            b = c;<br\/>        else<br\/>            a = c;<br\/>    }<br\/>    cout &lt;&lt; &quot;The value of root is : &quot; &lt;&lt; c;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    \/\/ Initial values assumed<br\/>    double a =-200, b = 300;<br\/>    regulaFalsi(a, b);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>The value of root is : -1<\/pre>\n<p>This method always converges, usually considerably faster than Bisection. But worst case can be very slow.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Program for Method Of False Position &#8211; Mathematical Algorithms &#8211; Given a function f(x) on floating number x and two numbers \u2018a\u2019 and \u2018b\u2019 <\/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],"tags":[74848,76423,76420,69964,77752,77009,77000,77001,78366,69926,78363,69924,76434,74830,76424,78362,78361,70809,78359,77766,70822,69966,78352,78355,78356,78358,69959,74090,70827,74069,69957,78365,78353,78354,78350,78351,77755,77005,77004,77767,77765,76990,70834,76998,77011,69956,78364,69952,78360,78357],"class_list":["post-26325","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","tag-basic-c-programming-tutorial","tag-basic-c-programs","tag-basic-c-programs-for-beginners","tag-basics-of-c-language","tag-bisection-method-c-program","tag-bisection-method-example","tag-bisection-method-solved-examples","tag-bisection-method-solved-examples-pdf","tag-c-language-basic-programs","tag-c-language-basics-notes","tag-c-language-programs","tag-c-language-tutorial","tag-c-programming-basics","tag-c-programming-basics-notes","tag-c-programming-language-examples","tag-c-programming-language-tutorial","tag-c-programming-language-tutorial-for-beginners","tag-c-programming-tutorial","tag-c-programming-tutorial-for-beginners","tag-c-programs-using-functions","tag-c-programs-with-output","tag-c-language-basics","tag-cambridge-books-pdf","tag-cambridge-university-press-books-free-download","tag-cambridge-university-press-uk","tag-ccc-full-form-in-computer-course","tag-define-c-language","tag-how-to-write-a-program-in-c-language","tag-how-to-write-c-program","tag-if-c-programming","tag-importance-of-c-language","tag-inc","tag-learn-c-programming-step-by-step","tag-matlab-programming","tag-matlab-programming-for-engineers","tag-matlab-programming-pdf","tag-numerical-analysis-book-pdf-free-download","tag-numerical-methods-bisection-method","tag-numerical-methods-pdf","tag-numerical-recipes","tag-numerical-recipes-in-c","tag-program-of-bisection-method-in-c-language","tag-programming-in-c-language","tag-secant-method-example-solved","tag-secant-method-formula","tag-uses-of-c-language","tag-what-is-c-language","tag-what-is-c-language-definition","tag-what-is-c-programming-used-for","tag-what-is-in-c-language"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26325","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=26325"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26325\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}