Question: R Programing 4. Following is a recursive function which takes a vector x and returns a scalar. Either by looking carefully at the function, or
R Programing
4. Following is a recursive function which takes a vector x and returns a scalar. Either by looking carefully at the function, or by a few simple input vectors, determine what the function is doing.
myfun=function(x){
if(length(x)==1){return(x)
} else
return(x[1]+myfun(x[-1]))
}
myfun(c(1:1000)) #here's what happens when it is applied to 1:1000
## [1] 500500
A).Modify the function slightly so that it returns
, and test with x=c(1,2,3), in which case the answer should be approximately 30.19287.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
