How to create a tridiagonal matrix?
What is a tridiagonal matrix?
It typically looks like this:
$$ \begin{array}{cccccccc} a_{11} & a_{12} & 0 & \cdots & \cdots & \cdots & \cdots & 0 \ a_{21} & a_{22} & a_{23} & \ddots & & & & \vdots \ 0 & a_{32} & a_{33} & a_{34} & \ddots & & & \vdots \ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & & \vdots \ \vdots & & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \ \vdots & & & \ddots & a_{76} & a_{77} & a_{78} & 0 \ \vdots & & & & \ddots & a_{87} & a_{88} & a_{89} \ 0 & \cdots & \cdots & \cdots & \cdots & 0 & a_{98} & a_{99} \end{array} $$
You can use the toeplitz
function from the linear algebra library.
from scipy.linalg import toeplitz
toeplitz([2,-1,0,0,0],[0,-1,0,0,0])
The matrix you will obtain is a 5x5
Matrix with the following structure: