Edit: I changed my answer including a reference and the second order derivative
The coefficients for central differences of different order of accuracy with uniform spacing can be found on wikipedia here. I assume you need a second order derivative. If it is the case, you can build the matrix using spdiags:
N = 10;
C = [ones(N,1)/12 4*ones(N,1)/3 -5*ones(N,1)/2 4*ones(N,1)/3 ones(N,1)/12];
idiag = -2:2;
A = spdiags(C,idiag,N,N);
Remember to divide the matrix by the step size dx^2.
The matrix created in this way is sparse (as it is usually done with these problems).
2 Comments
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/505823-finite-difference-matrix-help#comment_798149
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/505823-finite-difference-matrix-help#comment_798149
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/505823-finite-difference-matrix-help#comment_799872
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/505823-finite-difference-matrix-help#comment_799872
Sign in to comment.