m=4,x=linspace(0,1,m)'; m = 4 x x = 0 0.3333 0.6667 1.0000 h=diff(x) h = 0.3333 0.3333 0.3333 1./(h(1:m-2)+h(2:m-1)) ans = 1.5000 1.5000 d=1./(h(1:m-2)+h(2:m-1)) d = 1.5000 1.5000 spdiags([[-d;0;0],[0;0;d]],[-1,1],4,4) ans = (2,1) -1.5000 (3,2) -1.5000 (2,3) 1.5000 (3,4) 1.5000 full(spdiags([[-d;0;0],[0;0;d]],[-1,1],4,4)) ans = 0 0 0 0 -1.5000 0 1.5000 0 0 -1.5000 0 1.5000 0 0 0 0 toeplitz([0,-1,0,0],[0,1,0,0]) ans = 0 1 0 0 -1 0 1 0 0 -1 0 1 0 0 -1 0 h=0.3 h = 0.3000 toeplitz([0,-1/(2*h),0,0],[0,1/(2*h),0,0]) ans = 0 1.6667 0 0 -1.6667 0 1.6667 0 0 -1.6667 0 1.6667 0 0 -1.6667 0 toeplitz(sparse([0,-1/(2*h),0,0]),sparse([0,1/(2*h),0,0])) ans = (2,1) -1.6667 (1,2) 1.6667 (3,2) -1.6667 (2,3) 1.6667 (4,3) -1.6667 (3,4) 1.6667 toeplitz([-2,1,0,0]) ans = -2 1 0 0 1 -2 1 0 0 1 -2 1 0 0 1 -2 y=@(x) sin(x) y = @(x)sin(x) x=[0,1] x = 0 1 u=y(x)+1e-4 u = 0.0001 0.8416 y(x)-u ans = 1.0e-04 * -1.0000 -1.0000 xx=linspace(0,1,100); uu=y(x)+1e-5; norm(y(x)-u) ans = 1.4142e-04 norm(y(xx)-uu) ??? Error using ==> minus Matrix dimensions must agree. uu=y(xx)+1e-5; norm(y(xx)-uu) ans = 1.0000e-04 norm(y(x)-u,inf) ans = 1.0000e-04 norm(y(xx)-uu,inf) ans = 1.0000e-05 diary off