Implementation of Bisection Method written in R.

Enter beginning of interval: 0
Enter end of interval: 2
Enter precision: 0.0000001
X:  1
X:  0.5
X:  0.25
X:  0.375
X:  0.4375
X:  0.40625
X:  0.421875
X:  0.4140625
X:  0.41796875
X:  0.416015625
X:  0.4150390625
X:  0.41455078125
X:  0.414306640625
X:  0.4141845703125
X:  0.41424560546875
X:  0.414215087890625
X:  0.414199829101562
X:  0.414207458496094
X:  0.414211273193359
X:  0.414213180541992
X:  0.414214134216309
X:  0.41421365737915
X:  0.414213418960571
X:  0.414213538169861
X:  0.414213597774506
# © 2018 TheFlyingKeyboard and released under MIT License
# theflyingkeyboard.net

f <- function(x) {
  return(x * (x + 2) - 1)
}	

intervalBegin <- as.double(readline(prompt = "Enter beginning of interval: "))
intervalEnd <- as.double(readline(prompt = "Enter end of interval: "))
precision <- as.double(readline(prompt = "Enter precision: "))

if ((f(intervalBegin) * f(intervalEnd)) > 0.0) {
  print("Function has same signs at ends of interval")
} else {
  while (abs(intervalBegin - intervalEnd) > precision) {
    middle = (intervalBegin + intervalEnd) / 2.0
    
    print(paste("X: ", middle))
    
    if ((f(intervalBegin) * f(middle)) < 0.0) {
      intervalEnd = middle
    } else {
      intervalBegin = middle
    }
  }
}

 



R Bisection Method
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close