%First name: Eva
%Last Name: Brunner
%Date: 17.2.05
%Homework number: 7

%Problem description:

%Find the solution of a 2-D Laplace Equation, on unit square, with
%boundary conditions equal to 1 on left and right boundaries and 0 on 
%upper and lower boundaries. Use h=0.2 in both dimensions.
%Use any system solver available. Plot the solution.

echo on
disp(sprintf('**** Laplace Equation ****\n'));

% The vector b and the Matrix LA from the original File are adapted for
% this problem

format short;

% b_i is defined as 1, if node i is next to left or right boundary.

b=[1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1]'; %boundary conditions (left and right edge = 1)
T1=diag([4 4 4 4],0)+ diag([-1 -1 -1],1)+ diag([-1 -1 -1],-1)
I=eye(4)
Z=zeros(4,4)
LA=[T1 -I Z Z; -I T1 -I Z; Z -I T1 -I; Z Z -I T1;]

%It would be enough to save the diagonal elements because they are all the
%same.
%All diagonal elements are the same. Matrix LA can be saved by diagonals,
%or not at all because their structure is known.
%The system dimension (number of inner points, elements of vector x) is 16.
%An array 6x6 is used for the representation of the system domain with 
%initial and boundary conditions. 
%Using Jacobi iteration and boundary conditions

U=[ones(1,6);zeros(4,6);ones(1,6)]'	% copy for the solution
u=LA\b	%exact solution
l1=u(1:4)';
l2=u(5:8)';
l3=u(9:12)';
l4=u(13:16)';
U=[U(1,:); 1,l4,1; 1,l3,1; 1,l2,1; 1,l1,1;U(6,:)]
surfc(U) %approximate solution