%time comparison for solving a linear equation

%size of the matrix
n=700;
n=1500;

%define off-diagonal part
B=zeros(n,n);
for i=1:n-1
  B(i,i+1)=1;
end


%define lower triangular part 
% when non-zero notice significant advantage of LU over inverting
for i=1:n-1
  B(i+1,i)=1;
end



%define diagonal part
lam=-0.01;
D=B-lam*eye(n);

%define first and last unit vector
en=[zeros(1,n-1) 1]';
e0=[1 zeros(1,n-1)]';


%measure time to compute the inverse and apply it to a vector
tic();
Dinv=inv(D);
Dinv*en;
computeinverse = toc()


%measure time to solve the equation using Gauss
tic();
D\en;
solveequation =toc()

%measure time to compute an LU decomposition
tic();
[L,U]=lu(D);
computelu=toc()

%measure time to solve an equation 
%given the LU decomposition
tic();
U\(L\D(:,n));
solveeqnlu= toc()

%measure time to solve the equation 
%exploiting that the matrix is sparse
Ds=sparse(D);
tic();
Ds\en;
solvesparseequation= toc()

