{"id":26312,"date":"2017-10-26T20:59:20","date_gmt":"2017-10-26T15:29:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26312"},"modified":"2017-10-26T20:59:20","modified_gmt":"2017-10-26T15:29:20","slug":"c-programming-program-bisection-method","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-program-bisection-method\/","title":{"rendered":"C++ Programming &#8211; Program for Bisection Method"},"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> - 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.0025\r\n        OR any other value with allowed\r\n        deviation from root.\r\n<\/pre>\n<p><strong>What are Algebraic and Transcendental functions?<\/strong><br \/>\nAlgebraic function are the one which can be represented in the form of polynomials like f(x) = a<sub>1<\/sub>x<sup>3<\/sup> + a<sub>2<\/sub>x<sup>2<\/sup> + &#8230;.. + e where aa<sub>1<\/sub>, a<sub>2<\/sub>, &#8230; are constants and x is a variable.<br \/>\nTranscendental function are non algebraic functions, for example f(x) = sin(x)*x &#8211; 3 or f(x) = e<sup>x<\/sup> + x<sup>2<\/sup> or f(x) = ln(x) + x &#8230;.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>What is Bisection Method?<\/strong><br \/>\nThe method is also called the interval halving method, the binary search method or the dichotomy method. This method is used to find root of an equation in a given interval that is value of &#8216;x&#8217; for which f(x) = 0 .<\/p>\n<p>The method is based on <strong>The Intermediate Value Theorem<\/strong> which states that if f(x) is a continuous function and there are two real numbers a and b such that f(a)*f(b) &lt; 0. Then f(x) has at least one zero between a and b. If for a function (f(a) &lt; 0 and f(b) &gt; 0) or (f(a) &gt; 0 and f(b) &lt; 0), then it is guaranteed that it has at least one root between them.<\/p>\n<p><strong>Assumptions:<\/strong><\/p>\n<ol>\n<li>f(x) is a continuous function in interval [a, b]<\/li>\n<li>f(a) * f(b) &lt; 0<\/li>\n<\/ol>\n<p><strong>Steps:<\/strong><\/p>\n<ol>\n<li>Find middle point <strong>c<\/strong>= (a + b)\/2 .<\/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&#8217;t follow one of assumptions.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>Since root may be a floating point number, we repeat above steps while difference between a and b is less than a value \u03b5 (A very small value).<img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26313\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/bisection.png\" alt=\"Bisection\" width=\"800\" height=\"933\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/bisection.png 800w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/bisection-257x300.png 257w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/bisection-768x896.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Below is C++ implementation of above steps.<\/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 EPSILON 0.01<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) with error of EPSILON<br\/>void bisection(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;<br\/>    while ((b-a) &gt;= EPSILON)<br\/>    {<br\/>        \/\/ Find middle point<br\/>        c = (a+b)\/2;<br\/> <br\/>        \/\/ Check if middle point is root<br\/>        if (func(c) == 0.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\/>    bisection(a, b);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>The value of root is : -1.0025\r\n<\/pre>\n<p><strong>Time complexity :-<\/strong> Time complexity of this method depends on the assumed values and the function.<\/p>\n<p><strong>What are pros and cons?<\/strong><br \/>\nAdvantage of the bisection method is that it is guaranteed to be converged. Disadvantage of bisection method is that it cannot detect multiple roots.<\/p>\n<p>In general, Bisection method is used to get an initial rough approximation of solution. Then faster converging methods are used to find the solution.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Program for Bisection Method &#8211; Mathematical Algorithms &#8211; The method is also called the interval halving method, the binary search method<\/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":[78271,78273,78304,77752,78298,77009,78270,78293,78286,77015,78300,78297,78268,78281,78291,78278,78294,78290,77000,77001,78267,78305,78282,78269,78280,78279,78306,78285,78296,78274,78301,77005,78303,78289,78277,78283,78266,78276,78288,78284,76990,78295,78292,78302,78275,78299,76998,77011,78272,78287],"class_list":["post-26312","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","tag-algorithm-of-bisection-method","tag-bisection-method","tag-bisection-method-algorithm-pdf","tag-bisection-method-c-program","tag-bisection-method-code-in-c","tag-bisection-method-example","tag-bisection-method-example-ppt","tag-bisection-method-fortran-90-code","tag-bisection-method-fortran-code","tag-bisection-method-in-c","tag-bisection-method-in-c-code","tag-bisection-method-in-c-using-for-loop","tag-bisection-method-in-fortran","tag-bisection-method-in-java","tag-bisection-method-java","tag-bisection-method-ppt","tag-bisection-method-program-in-matlab","tag-bisection-method-python-code","tag-bisection-method-solved-examples","tag-bisection-method-solved-examples-pdf","tag-bisection-program-in-c","tag-c-code-for-bisection-method","tag-c-program-for-bisection-method","tag-c-program-of-bisection-method","tag-convergence-of-bisection-method","tag-flowchart-for-bisection-method","tag-flowchart-of-bisection-method","tag-fortran-program-for-bisection-method","tag-matlab-program-for-bisection-method","tag-newton-raphson-method-formula","tag-numerical-bisection-method","tag-numerical-methods-bisection-method","tag-numerical-methods-bisection-method-pdf","tag-numerical-methods-c-programs","tag-numerical-methods-in-c","tag-numerical-methods-in-c-programming","tag-numerical-methods-programs-in-c","tag-numerical-methods-using-c-programming","tag-numerical-methods-with-programs-in-c","tag-numerical-programming-in-c","tag-program-of-bisection-method-in-c-language","tag-program-of-newton-raphson-method-in-c","tag-programs-in-c-language-solved","tag-python-bisection-method","tag-regula-falsi-method","tag-regula-falsi-method-program-in-c","tag-secant-method-example-solved","tag-secant-method-formula","tag-simple-c-program-for-bisection-method","tag-write-an-algorithm-for-the-bisection-method"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26312","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=26312"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26312\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}