0

I want to apply Newton's Method for square root through iterations in RStudio, but I keep getting error

"Error: C stack usage 7969204 is too close to the limit"

when I put a wrong sqrt in the 'g'. Instead, the code works fine when I write directly the right number (example: sqriter(2,4) --> 2)

Below is the code I wrote for it.

thank you for your help!

sqriter <- function(g,x){
  ifelse(goodguess(g,x), g, sqriter(improve(g,x), x))
}

goodguess <- function(g,x){
  abs(g*g-x)<0.001
}

average <- function(g,x){
  ((g+x)/2)
}

improve <- function(g,x){
  average(g, (g/x))
}
shizhen
  • 12,251
  • 9
  • 52
  • 88

0 Answers0