I am attempting this simple problem of solving f(x)=x^2 - 2*x - 2 = 0 by using Newton Raphson method and hence counting the number of iterations(it) required. {using looping by function recurssion}
it<-1
NR <- function(x) {
f <- x^2-2*x-2
fd<- 2*x-2
x1<- x -f/fd
f1 <- x1^2-2*x1-2
if(abs(f1)>=0.0001){
it<- it+1
NR(x1)
}
else{
print("The solution by NR method is :")
print(x1)
print("The number of iterations are :")
print(it)
}
}
NR(3)
I am getting the correct solution for x but the iteration is not incrementing even though it is declared as global.Please share if any conceptual mistake I am doing in using the object declaration.