clear all close all % example 2 % f''-f = -1, f(-1)=0, f(1)=2 % in class the right hand side is +1 but the % idea to solve the system is the same N = 50; X = linspace(-1,1,N+1); dx = 2/N; L = diag(-2*ones(1,N+1)) + diag(ones(1,N),1) + diag(ones(1,N),-1); L(1,1)=1; L(1,2)=0; L(N+1,N+1)=1; L(N+1,N)=0; Q = -diag(ones(1,N+1))*dx^2; Q(1,1)=0; Q(N+1,N+1)=0; r = -ones(N+1,1)*dx^2; % use the notation in class (in fact it is the right hand side vector b) r(1)=0; r(N+1)=2; F = (L+Q)\r; plot(X,F)