Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

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,

share|improve this question
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
1  
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

1 Answer

up vote 1 down vote accepted

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)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.