x=linspace(0,1,11)'; x x = 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 x(2:10)=x(2:10)+2*(rand(9,1)-0.5)/100 x = 0 0.1063 0.2081 0.2925 0.4083 0.5026 0.5920 0.6956 0.8009 0.9092 1.0000 h=x(2:11)-x(1:10) h = 0.1063 0.1018 0.0844 0.1157 0.0944 0.0893 0.1036 0.1054 0.1082 0.0908 h=diff(x) h = 0.1063 0.1018 0.0844 0.1157 0.0944 0.0893 0.1036 0.1054 0.1082 0.0908 spdiags([[1;2;3],[4;5;6],[7;8;9]],[-1,0,1],3,3) ans = (1,1) 4 (2,1) 1 (1,2) 8 (2,2) 5 (3,2) 2 (2,3) 9 (3,3) 6 full(spdiags([[1;2;3],[4;5;6],[7;8;9]],[-1,0,1],3,3)) ans = 4 8 0 1 5 9 0 2 6 x=linspace(0,1,4)'; x(2:3)=x(2:3)+2*(rand(2,1)-0.5)/100 x = 0 0.3426 0.6598 1.0000 h=diff ??? Error using ==> diff Not enough input arguments. h=diff(x) h = 0.3426 0.3172 0.3402 A=spdiags([[-1./h;0],[1/h(1);1./h(1:2)+1./h(2:3);1/h(3)],[0;-1./h]],[-1,0,1],4,4); full(A) ans = 2.9186 -2.9186 0 0 -2.9186 6.0713 -3.1527 0 0 -3.1527 6.0923 -2.9396 0 0 -2.9396 2.9396 h' ans = 0.3426 0.3172 0.3402 1./h' ans = 2.9186 3.1527 2.9396 full(A) ans = 2.9186 -2.9186 0 0 -2.9186 6.0713 -3.1527 0 0 -3.1527 6.0923 -2.9396 0 0 -2.9396 2.9396