2013-03-10 1 views
0

Comment puis-je définir la tolérance (1e-08) et l'itération maximale (40) pour le code suivant?Configurer la tolérance et l'itération maximale pour la méthode de Newton

newton<-function(fun, grad, x_0) { 
xold<-x_0 
xnew<-xold+1000 
while(1! = 0){ 
    f <- fun(xold) 
    g <- grad(xold) 
    xnew <- xold-f/g 
    cat("xold, xnew, f, g:",xold,' ',xnew,' ',f,' ',g,"\n") 
    if ((1000+xold) == (1000+xnew)) return(xnew) 
    tmp<-readline("go on?") 
    xold<-xnew 
} 
} 

Répondre

3
newton<-function(fun, grad, x_0,maxiter=40,tol=1e-8) { 
xold<-x_0 
xnew<-xold+100 # to avoid stopping 
i<-0 #make counter 
while(i<maxiter){ #do until i=maxiter 
    f <- fun(xold) 
    g <- grad(xold) 
    xnew <- xold-f/g 
    cat("xold, xnew, f, g:",xold,' ',xnew,' ',f,' ',g,"\n") 
    #if the absolute difference between old and new value < tol, stop 
    if (abs(old-xnew)<tol) return(xnew) 
    tmp<-readline("go on?") 
    xold<-xnew 
    i <- i+1 
} 
} 
+0

Merci pour votre aide à nouveau. –

+0

Pouvons-nous changer "100" pour d'autres nombres? Et que signifie "tmp"? –

+1

Vous n'avez pas vraiment besoin de la ligne 'xnew <-xold + 100', et aussi' tmp <-readline ("allez-y?") 'Est inutile ici. –

Questions connexes