I would like to know what I can correct in this code to get the desired results.
h1 <- function(x,n)
Sum=0
# this loop isn't working
for (i in seq(from=0,to=n)) {
Sum = Sum x^i
}
}
This is the output:
h1(0.3,55)
Error in h1(0.3, 55)
object 'Sum' not found
CodePudding user response:
There are couple of mistakes in your code which has been solved in other answers. However, this can be solved without a loop.
h1 <- function(x,n) {
sum(x^seq(from=0,to=n))
}
h1(0.3,55)
#[1] 1.428571
CodePudding user response:
h1 <- function(x,n){
Sum=0
for (i in seq(from=0,to=n)) {#. ** This loop isnt working.**
Sum = Sum x^i
}
return(Sum)
}
h1(0.3,55)
1.428571
CodePudding user response:
A brace was missing:
h1 <- function(x,n)
{ # this brace was missing
Sum=0
for (i in seq(from=0,to=n)) {
Sum = Sum x^i
}
Sum
}
h1(0.3,55)
#> [1] 1.428571