Skip to content

Instantly share code, notes, and snippets.

@Protonk
Created December 28, 2010 22:40
Show Gist options
  • Select an option

  • Save Protonk/757844 to your computer and use it in GitHub Desktop.

Select an option

Save Protonk/757844 to your computer and use it in GitHub Desktop.
bisect <- function(f, u, v, eps){
if ( f(u) * f(v) > 0)
stop (" error, select another value for u or v...")
if ( f(u) < 0) {
u1 <- u
v1 <- v
} else {
u1 <- v
v1 <- u
}
cnt <- 1
step <- log2(abs(u1-v1)/eps)
while ( cnt < step ){
m <- (u1+v1)/2
if ( f(m) == 0)
break
if ( f(m) < 0 ) {
u1 <- m
} else {
v1 <- m
}
cnt <- cnt + 1
}
return (m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment