n=20; h=1/20; % Define the tridiagonal matrix A D = sparse(1:n,1:n,-2*ones(1,n),n,n); E = sparse(2:n,1:n-1,ones(1,n-1),n,n); A = E+D+E'; A(1,1)=-1; % Define the right-hand side b1 x=[0:h:1-h]; b=h^2*exp(x); b1=b; b1(1)=0; % Solve for u where Au=b1 u1=b1/A; % Try a different right-hand side b2 b2=b; b2(1)=h^2/2*exp(x(1)); % Solve for u in Au=b2 u2=b2/A; % Compute the exact solution uexact=exp(x)-x+1-exp(1); % Plot the exact solution with the computed solution figure(1) clf plot(x,uexact) axis([0 1 -0.8 0]) hold on plot(x,u1,'+') plot(x,u2,'o') hold off myh=legend('Exact solution ','Solution with 1-sided at bdy', ... 'Solution with centered at bdy',0); xlabel('x') ylabel('u') Ha_ax=gca;set(Ha_ax,'Fontsize',12);