Learn R Programming

NLRoot (version 1.0)

BFfzero: Bisection Method

Description

Bisection Method to Find the Root of Nonlinear Equation

Usage

BFfzero(f, a, b, num = 10, eps = 1e-05)

Arguments

f
the objective function which we will use to solve for the root
a
mininum of the interval which cantains the root from Bisection Method
b
maxinum of the interval which cantains the root from Bisection Method
num
the number of sections that the interval which from Bisection Method
eps
the level of precision that |x(k+1)-x(k)| should be satisfied in order to get the idear real root. eps=1e-5 when it is default

Value

Details

Be careful to choose a & b. If not we maybe fail to find the root

References

Luis Torgo (2003) Data Mining with R:learning by case studies. LIACC-FEP, University of Porto

See Also

NDHfzero,NIMfzero,SMfzero

Examples

Run this code

    BFfzero(f,0,2)
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (f, a, b, num = 10, eps = 1e-05) 
{
    h = abs(b - a)/num
    i = 0
    j = 0
    a1 = b1 = 0
    while (i <= num) {
        a1 = a + i * h
        b1 = a1 + h
        if (f(a1) == 0) {
            print(a1)
            print(f(a1))
        }
        else if (f(b1) == 0) {
            print(b1)
            print(f(b1))
        }
        else if (f(a1) * f(b1) < 0) {
            repeat {
                if (abs(b1 - a1) < eps) 
                  break
                x <- (a1 + b1)/2
                if (f(a1) * f(x) < 0) 
                  b1 <- x
                else a1 <- x
            }
            print(j + 1)
            j = j + 1
            print((a1 + b1)/2)
            print(f((a1 + b1)/2))
        }
        i = i + 1
    }
    if (j == 0) 
        print("finding root is fail")
    else print("finding root is successful")
  }

Run the code above in your browser using DataLab