% example for finite difference approximation clear % example 1 % f''=1, f(0)=1, f(1)=1 % exact solution: f = 1-x/2+x^2/2 % this can also be treated as the simplest case of a heat equation % where source S(x)=1 (the right hand side of equation) % set up matrix L N = 50; %L = zeros(N+1,N+1); 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; cond(L) pause % enter to proceed dx = 1/N; b = ones(N+1,1)*dx*dx; b(1)=1; b(N+1)=1; F = L\b; X = linspace(0,1,N+1); plot(X,F,'x') hold on plot(X,ones(1,N+1)-X/2+X.^2/2,'r')