agm.R

Gauss arithmetic-geometric mean, simple iteration with convergence rate estimates
language: R
license: GPL 2

Code for Snippet:

                
# Gauss arithmetic-geometric mean
 
 
agm<-function(a,b,eps=1e-8,itmax=1000,verbose=TRUE)
{
xold<-max(a,b); yold<-min(a,b); dold<-xold-yold; itel<-1
repeat {
    xnew<-(xold+yold)/2; ynew<-sqrt(xold*yold)
    dnew<-xnew-ynew; rat1<-dnew/dold; rat2<-dnew/(dold^2)
    if (verbose) cat(
        "Iteration: ",formatC(itel,width=3, format="d"),
        "old: ",formatC(c(xold,yold,dold),digits=8,width=12,format="f"),
        "old: ",formatC(c(xnew,ynew,dnew),digits=8,width=12,format="f"),
        "rat: ",formatC(c(rat1,rat2),digits=8,width=12,format="f"),
        "\n")  
    if ((dnew < eps) || (itel == itmax)) return(c(xnew,ynew))
    xold<-xnew; yold<-ynew; dold<-dnew; itel<-itel+1
    }
}
comments powered by Disqus

Info

Source Site:

Jan

Tags: R Arithmetic-Geometric Mean

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 687
Date Added: 2013-03-06 23:16:38
Last Modified: 2013-04-17 22:12:19

Web Analytics