I try to simulate Brownian motion using Karhunen-Loéve expansion in R. I found that formula is:
$$W_t=\sqrt2\sum_{k \ge0} \gamma_k\frac{2}{(2k+1)\pi}\sin\bigg(\bigg(k+\frac{1}{2}\bigg)\pi t\bigg)$$
I tried this code:
n<-1000
T. <- 1
delta <-T./n
k<- c(1:1000)
W_kl <- matrix(NA, ncol = 10, nrow = n)
for (i in 1:10){
summands <- rnorm(n, 0, 1) * (2 / (2*k +1) * pi) * sin((k + 1/2) * pi * delta)
W_kl[,i] <- sqrt(2) * cumsum(summands)
}
matplot(W_kl, type = "l")
The beginning of the graph looks fine, but endings no, I got this:

By using another method which is really correct I got such a graph:

Maybe somebody can say what is wrong with my code?