I'm trying to make a function witch recieves a function "f", "a" and "b", and the error "e". When I run the algorithm without labeling it a function and typing the values it works, but when I just try to save it in a function It doesn't ' bug but don't do anything. Here's the code, anyone know whats happening? raiz=function(f,a,b,e){ repeat{ if(i>50){break} if(abs(a-b)<e){m=(a+b)/2 raiz1=m} if(abs(a-b)>e){ m=(a+b)/2 af=f(a) if((af*f(m))>0){a=m} if((af*f(m))<0){b=m}} i=i+1}} -- View this message in context: http://r.789695.n4.nabble.com/Help-in-Bissection-algorithm-tp4651295.html Sent from the R help mailing list archive at Nabble.com.
On 29-11-2012, at 16:18, finehko wrote:> I'm trying to make a function witch recieves a function "f", "a" and "b", and > the error "e". When I run the algorithm without labeling it a function and > typing the values it works, but when I just try to save it in a function It > doesn't ' bug but don't do anything.I don't understand what you mean with this: "doesn't ' bug but don't do anything"> Here's the code, anyone know whats > happening? > raiz=function(f,a,b,e){ > repeat{ > if(i>50){break} > if(abs(a-b)<e){m=(a+b)/2 > raiz1=m} > if(abs(a-b)>e){ > m=(a+b)/2 > af=f(a) > if((af*f(m))>0){a=m} > if((af*f(m))<0){b=m}} > i=i+1}} >Initialize i before starting the repeat loop. i <- 1 Berend
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:> I'm trying to make a function witch recieves a function "f", "a" and "b", and > the error "e". When I run the algorithm without labeling it a function and > typing the values it works, but when I just try to save it in a function It > doesn't ' bug but don't do anything. Here's the code, anyone know whats > happening? > raiz=function(f,a,b,e){ > repeat{ > if(i>50){break} > if(abs(a-b)<e){m=(a+b)/2 > raiz1=m} > if(abs(a-b)>e){ > m=(a+b)/2 > af=f(a) > if((af*f(m))>0){a=m} > if((af*f(m))<0){b=m}} > i=i+1}} > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-in-Bissection-algorithm-tp4651295.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
What is 'i'? I don't see it as a parameter? is it something in the global environment that you forgot to pass? On Thu, Nov 29, 2012 at 10:18 AM, finehko <j9436 at hotmail.com> wrote:> I'm trying to make a function witch recieves a function "f", "a" and "b", and > the error "e". When I run the algorithm without labeling it a function and > typing the values it works, but when I just try to save it in a function It > doesn't ' bug but don't do anything. Here's the code, anyone know whats > happening? > raiz=function(f,a,b,e){ > repeat{ > if(i>50){break} > if(abs(a-b)<e){m=(a+b)/2 > raiz1=m} > if(abs(a-b)>e){ > m=(a+b)/2 > af=f(a) > if((af*f(m))>0){a=m} > if((af*f(m))<0){b=m}} > i=i+1}} > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-in-Bissection-algorithm-tp4651295.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.