Created
December 28, 2010 22:40
-
-
Save Protonk/757844 to your computer and use it in GitHub Desktop.
root finding by bisection from http://ygc.cwsurf.de/2008/11/25/bisect-to-solve-equation/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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