How to resolve the algorithm Arithmetic-geometric mean step by step in the R programming language

Published on 12 May 2024 09:40 PM
#R

How to resolve the algorithm Arithmetic-geometric mean step by step in the R programming language

Table of Contents

Problem Statement

Write a function to compute the arithmetic-geometric mean of two numbers.

The arithmetic-geometric mean of two numbers can be (usefully) denoted as

a g m

( a , g )

{\displaystyle \mathrm {agm} (a,g)}

, and is equal to the limit of the sequence: Since the limit of

a

n

g

n

{\displaystyle a_{n}-g_{n}}

tends (rapidly) to zero with iterations, this is an efficient method. Demonstrate the function by calculating:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic-geometric mean step by step in the R programming language

Source code in the r programming language

arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }

arithmeticGeometricMean <- function(a, b) {
  rel_error <- abs(a - b) / pmax(a, b) 
  if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
    agm <- a
    return(data.frame(agm, rel_error));
  }
  Recall(arithmeticMean(a, b), geometricMean(a, b))  
}

agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))


a <- c(1, 1, 1)
b <- c(1/sqrt(2), 1/sqrt(3), 1/2)
agm <- arithmeticGeometricMean(a, b)
print(format(agm, digits=16))


  

You may also check:How to resolve the algorithm Compiler/code generator step by step in the zkl programming language
You may also check:How to resolve the algorithm IBAN step by step in the Sidef programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Ruby programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Ecstasy programming language