% Fixed-Fixed 1D Finite Difference Model using n nodes and f=1
% David Bloore, July 2011

clc;
clear all;

n=input('N?');
h=1/(n+1);
xx=linspace(0,1,1000);

K=sparse(toeplitz([2 -1 zeros(1,n-2)]));
H=K;
H(n,n)=1;

f=h^2*ones(n,1);

u=K\f;
yy=-(1/2)*xx.^2+(1/2)*xx;
x=linspace(0,1,n+2);
y=zeros(n+2,1);
y(2:n+1)=u;

plot(x,y,'--',xx,yy,'-',x,y,'o');
legend('Solution to Ku=f','f(x)=-(1/2)x^2+(1/2)x');
grid on;