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 R Programing 4. Following is a recursive function which takes a vector , 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!