function [y] = mult3diag(d,l,u,x)
% function [y] = mult3diag(d,l,u,x)
%   Multiplication of vector x by 3-diagonal matrix A
%   d - main diagonal of A
%   l - subdiagonal of A
%   u - superdiagonal of A
%   x - the vector
%   y = A*x
%
%   The variables d,l,u may be constant, it implies constant diagonals.
%   The variable x must be a vector because of the size of the system.
%

n=length(x);    % size of the system

% The test of sizes follows.
if length(d)==1, d=d*ones(1,n); end
if length(l)==1, l=l*ones(1,n-1); end
if length(u)==1, u=u*ones(1,n-1); end
if length(d)~=n || length(l)~=n-1 || length(u)~=n-1
    error('Incompatible size of input variables!');
end

x=x(:);   % column vector

% extension of the variables because of easier computation
l=[0 l];
u=[u 0];
x=[0; x; 0];

y=zeros(n,1);

% multiplication
for k=1:n
    y(k)=[l(k) d(k) u(k)]*[x(k:k+2)];
end

end     % That's all, Folks!


