I am using R. I have made a random function (using monte carlo integration) that approximates the arctan function (equal to the integral of 1/(1+x^2)). It seems to work well and gives accurate approximations. For example 3*arctan(sqrt(3)) should give pi, and below I get a close approximation.
> f=function(x)
+ {y=runif(10^6,0,x); return(x*mean((1/(1+y^2))))}
> f(sqrt(3))*3
[1] 3.140515
However, when I use the function on a sequence of numbers, the answers seem to come out wrong. Below the correct values are given by the atan function, but the f function gives the wrong values when used on the same vector.
> atan(c(1,1.5,2))
[1] 0.7853982 0.9827937 1.1071487
> f(c(1,1.5,2))
[1] 0.6648275 0.9972412 1.3296550
> f(1)
[1] 0.7852855
> f(1.5)
[1] 0.9828134
> f(2)
[1] 1.107888
Notice how when I use f on 1, 1.5 and 2 individually, it works, but not in vector form? What's going on here? I have tried running f a few times on the vector and the wrong values are pretty consistent to 2 decimal places.