2019年3月22日 星期五

Bisection Method – Algorithm, Flowchart and Code in C

Bisection Method – Algorithm, Flowchart and Code in C

源自於
Bisection Method Illustration
Bisection Method Illustration
The bisection methodis one of the simplest and most reliable of iterative methods for the solution of nonlinear equations. This method, also known as binary chopping or half-interval method, relies on the fact that if f(x) is real and continuous in the interval a < x < b, and f(a) and f(b) are of opposite signs, that is,
f(a)*f(b) < 0
then there is at least one real root in the interval between a and b.
Bisection method is a closed bracket method and requires two initial guesses. It is the simplest method with slow but steady rate of convergence.
Features of Bisection Method:
  • Type – closed bracket
  • No. of initial guesses – 2
  • Convergence – linear
  • Rate of convergence – slow but steady
  • Accuracy – good
  • Programming effort – easy
  • Approach – middle point

Bisection Method Algorithm and Flowchart:

Algorithm:
Start
  1. Decide initial values for x1 and x2 and stopping criterion, E.
  2. Compute f1 = f(x1) and f2 = f(x2).
  3. If f1 * f2>0, x1 and x2 do not bracket any root and go to step 7;
    Otherwise continue.
  4. Compute x0 = (x1+x2)/2 and compute f0 = f(x0)
  5. If f1*f0 < 0 then
    set x2 = x0
    else
    set x1 = x0
    set f1 = f0
  6. If absolute value of (x2 – x1)/x2 is less than error E, then
    root = (x1 + x2)/2
    write the value of root
    go to step 7
    else
    go to step 4
  7. Stop.
Flow Chart
Bisection Method 1- Flowchart
Bisection Method 1- Flowchart
Above given Algorithm and Flowchart of Bisection Methods Root computation is a simple and easier way of understanding how the bracketing system works, algorithm and flowchart may not follow same procedure, yet they give the same outputs.

Tabular Example of Bisection Method Numerical Computation

Consider f(x) = x3 + 3x – 5, where [x1 = 1, x2 = 2] and E = 0.001.
ix1x0x2f(x1)f(x0)f(x2)
111.52–1 2.875 9
211.251.5–1 0.703125 2.875
311.1251.25–1–0.201171875 0.703125
41.1251.18751.25–0.201171875 0.237060546875 0.703125
51.1251.156251.1875–0.201171875 0.014556884765625 0.237060546875
61.1251.1406251.15625–0.201171875–0.0941429138183594  0.014556884765625
71.1406251.14843751.15625–0.0941429138183594–0.0400032997131348 0.014556884765625
81.14843751.152343751.15625–0.0400032997131348–0.0127759575843811 0.014556884765625
91.152343751.1542968751.15625–0.0127759575843811 0.000877253711223602 0.014556884765625
Note: Bisection method guarantees the convergence of a function f(x) if it is continuous on the interval [a,b] (denoted by x1 and x2 in the above algorithm. For this, f(a) and f(b) should be of opposite nature i.e. opposite signs. The slow convergence in bisection method is due to the fact that the absolute error is halved at each step. Due to this the method undergoes linear convergence, which is comparatively slower than the Newton-Raphson, secant or false-position method.

Bisection Method – Code in C Programming

METHOD 1:
This program in C is used to demonstrate bisection method. Bisection method is one of the many root finding methods.
In this method we are given a function f(x) and we approximate 2 roots a and b for the function such that f(a).f(b)<0
Then we find another point
c=(a+b)/2
if f(c)==0
then root=c;
else
if f(a).f(c)<0
b=c;
if f(b).f(c)<0
a=c;
and we repeat these steps for the given number of iterations.
METHOD 2:
Source Code for Bisection Method
for equation f(x) = x^3 – 4*x – 9

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...