% h1_2

% the matrix A
% this could be done in a more generalisable way... see problem 3
m=5;n=m-1;
B=4*diag(ones(1,n))-diag(ones(1,n-1),1)-diag(ones(1,n-1),-1);
O=zeros(n);
mI=diag(ones(n,1));
A=[B,mI,O,O;mI,B,mI,O;O,mI,B,mI;O,O,mI,B];

% rhs
left=[0,20,40,60,80,0];top=[50,20,10,0];bot=zeros(1,4);right=left;
C=[left(2:n-1)' zeros(n-2,n-2) right(2:n-1)'];
C=[bot;C;top];
C(1,1)=C(1,1)+left(1);
C(1,n)=C(1,n)+right(1);
C(n,1)=C(n,1)+left(n);
C(n,n)=C(n,n)+right(n);

%C
C=C'
rhs=C(:)

sol=inv(A)*rhs

solmat=reshape(sol,n,n)

solmat=solmat'

solmat=[bot;solmat;top]

solmat=[left' solmat right']

% averages to corners... could be generalised as well
solmat(n+2,1)=65; solmat(n+2,n+2)=40;

% THE GENERAL CASE IS GIVEN IN EXERCISE 3
