Skip to content
Prev 312394 / 398506 Next

Help in Bissection algorithm

Hello,

Actually, it would throw an error, you forgot to assign 'i'.
And in the end your function didn't have a return value. I've edited and 
simplified it a bit.
(And what to do if abs(a - b) == e ? The second condition was deleted.)

raiz <- function(f,a,b,e){
     i <- 0
     repeat{
         if(i > 50) break
         m <- (a + b)/2
         if(abs(a-b) < e) break
         af <- f(a)
         if(af*f(m) > 0) a <- m
         if(af*f(m) < 0) b <- m
         i=i+1
     }
     #raiz1 <- m # not needed
     m
}

f <- function(x) x^2 - 2
raiz(f, 0, 2, 1e-5)
[1] 1.414211


Hope this helps,

Rui Barradas

Em 29-11-2012 15:18, finehko escreveu: