2

Given:

h_i=t_(i+1)-t_i for i=1,...,n-1 where n is a positive integer.

The matrix Q is an n by (n-2) matrix with entries q_(i,j) with i=1,...,n and j=2,...n-1 given by:

q_(j-1,j)=1/h_(j-1)
q_(j,j)=-(1/h_(j-1)+1/h_j)
q_(j+1,j)=1/h_j
q_(i,j)=0 for |i-j|>=2

I want to get a matrix Q. How do i write a program for this matrix in R? Many thanx in advance.

1 Answer 1

1

If I've figured out the subscripts correctly, I think this will do it:

n <- 100
t <- sort(runif(n))
h <- diff(t)
Q <- matrix(0,n,n-2)
Q[row(Q)==col(Q)-1] <- 1/h[1:(n-3)]
Q[row(Q)==col(Q)+1] <- 1/h[1:(n-2)]
diag(Q) <- c(NA,-1/h[1:(n-3)] - 1/h[2:(n-2)])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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