# Inverse of the central difference operator

The central difference is a method to approximate numerically the derivative of a function sampled at discrete intervals. In R, one would do:

n<-100
y<-cumsum(rnorm(n))
y_p[1]<-diff(y[1:2])
for(i in 2:(n-1)) y_p[i]<-diff(y[c(i-1,i+1)])/2
y_p[n]<-diff(y[c(n-1,n)])


My question is, what is the comonly accepted inverse to this operator? (preferably in pseudo code form to avoid ambuiguity)

Best,

-
I don't think there is a reason for migrate, but this would be also on topic on Computational Science SE. – mbq Apr 11 '12 at 10:34
I think away from the edges you might want to divide the difference by 2. – Henry Apr 11 '12 at 10:42
@Henry: indeed, corrected. – user603 Apr 11 '12 at 10:44

Up to a constant difference (equal to y[1]) you can reconstruct this with

z <- rep(NA,n)
z[1] <- 0
z[2] <- z[1] + y_p[1]
for (i in 2:(n-1)) { z[i+1] <- z[i-1] + 2*y_p[i] }


though you might prefer for (i in 3:length(y_p)) { z[i] <- z[i-2] + 2*y_p[i-1] } as the last line.

You can see the constant difference with

plot(y-z)

-