Fractional Calculus Course, we are instructed to create an $n \times n$ Tri-Diagonal matrix in the form of:
\begin{array} a A &= \begin{bmatrix} 2 & -1 & 0 & 0 & ... & 0 & 0 & 0\\ -1 & 2 & -1 & 0 & 0 & 0&... & 0 \\ 0 & -1 & 2 & -1 & 0& 0... & 0 & 0\\ 0 & 0 &-1 & 2 & -1 & ... & 0 & 0\\ &&&\vdots&&& \\ 0 & 0 & 0&... & 0 & -1 & 2 & -1\\ 0 & 0 & 0 & 0&... & 0 & -1 & 2 \\ \end{bmatrix} \end{array}
This is where my dilemma begins. I am not sure how to create this Tri-Diagonal Matrix. I came across the "SparseArray" command upon my research and it help me create a Tri-Diagonal Matrix, but I am having a hard time manipulating it to get the $-1, 2, -1$ pattern I am looking for.
mat = SparseArray[ {i_, j_} /; Abs[i - j] <= 1 :> 1, {10, 10}];
mat // MatrixForm
Above is the command I used for a $10 \times 10$ matrix. But the tri-diagonal entries were all 1's.
Thus my question is, how would I create the tri-diagonal matrix $n \times n$ I desire? Is there a way to create a function so I can simply manipulate the value of $n$ to get a new matrix without typing (or copy-pasting) the entire code again?
Band
in the help files. $\endgroup$Fractional Calculus Course
? $\endgroup$