N = 50;
xmesh = linspace(0,2*pi,N+1);
dx = 1/N;
C = zeros(N+1,N+1);
b = zeros(N+1,1);
for k = 1:N+1
if k==1
C(1,1) = 1;
b(k) = 1;
elseif k==N+1
C(end,end) = 1;
b(k) = 1;
else
C(k,k-1:k+1) = [1,-(2-dx^2),1];
end
end
y_fdm = C\b
plot(xmesh,y_fdm)
ylabel(‘y’)
xlabel(‘x’)
legend(‘Exact’,’Finite Difference’)