{"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) < 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) < 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=\u201dbanner\u201d]\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) < 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) < 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) < 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[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20for%20implementation%20of%20Bisection%20Method%20for%0A%2F%2F%20solving%20equations%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%23define%20MAX_ITER%201000000%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%20Prints%20root%20of%20func(x)%20in%20interval%20%5Ba%2C%20b%5D%0Avoid%20regulaFalsi(double%20a%2C%20double%20b)%0A%7B%0A%20%20%20%20if%20(func(a)%20*%20func(b)%20%3E%3D%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22You%20have%20not%20assumed%20right%20a%20and%20b%5Cn%22%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20double%20c%20%3D%20a%3B%20%20%2F%2F%20Initialize%20result%0A%20%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%20%3C%20MAX_ITER%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20the%20point%20that%20touches%20x%20axis%0A%20%20%20%20%20%20%20%20c%20%3D%20(a*func(b)%20-%20b*func(a))%2F%20(func(b)%20-%20func(a))%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20the%20above%20found%20point%20is%20root%0A%20%20%20%20%20%20%20%20if%20(func(c)%3D%3D0)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Decide%20the%20side%20to%20repeat%20the%20steps%0A%20%20%20%20%20%20%20%20else%20if%20(func(c)*func(a)%20%3C%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20b%20%3D%20c%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20a%20%3D%20c%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%20%3C%3C%20%22The%20value%20of%20root%20is%20%3A%20%22%20%3C%3C%20c%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Initial%20values%20assumed%0A%20%20%20%20double%20a%20%3D-200%2C%20b%20%3D%20300%3B%0A%20%20%20%20regulaFalsi(a%2C%20b)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++ Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}