creating a tridiagonal matrix in scilab/matlab

2.2k Views Asked by At

I want to create a tridiagonal matrix in scilab/matlab such that it uses for loops. I dont know how to create the matrix but here is what i have started

for i=2:n

   a_{1,1}=2 

   a_{i,i}=2

   a_{i,i-1}=1

   a_{i,i+1}=1

end

Thats the components of the matrix and if possible the other components which are zero will not be stored for efficient memory usage.

1

There are 1 best solutions below

0
On

MATLAB code:

n = 10; % order of the matrix
M = zeros(n,n);
for k=1:n
    M(k,k) = 2; % main diagonal
    if k<n
        M(k+1,k) = 1; % diagonal below main
        M(k,k+1) = 1; % diagonal above main
    end
end

I don't know how to define the matrix without storing the zeros, but I know it's called sparse matrix. These links might be helpful: