Let $\lambda_1=\frac{k D_u}{2h^2}$ a constant value. How to generate a matrix in MATLAB with the next entries:
$A= \begin{pmatrix} 1+\lambda_1 & -\lambda_1 & 0 & 0 & \cdots & 0 & 0\\ -\lambda_1 & 1+2\lambda_1 & -\lambda_1 & 0 & \cdots & 0 & 0\\ 0 & -\lambda_1 & 1+2\lambda_1 & -\lambda_1 & \cdots & 0 & 0\\ 0 & 0 & -\lambda_1 & 1+2\lambda & \cdots & 0 & 0 \\ \vdots & \vdots & \vdots & \vdots &\ddots & \vdots & \vdots\\ 0 & 0 & 0 & 0 & \cdots & 1+2\lambda_1 & -\lambda_1\\ 0 & 0 & 0 & 0 & \cdots & -\lambda_1 & 1+\lambda_1\end{pmatrix}$
Then generate a matrix of the next form:
$B=\begin{pmatrix} 1-\lambda_1 & \lambda_1 & 0 & 0 & \cdots & 0 & 0\\ \lambda_1 & 1-2\lambda_1 & \lambda_1 & 0 & \cdots & 0 & 0\\ 0 & \lambda_1 & 1-2\lambda_1 & \lambda_1 & \cdots & 0 & 0\\ 0 & 0 & \lambda_1 & 1-2\lambda & \cdots & 0 & 0 \\ \vdots & \vdots & \vdots & \vdots &\ddots & \vdots & \vdots\\ 0 & 0 & 0 & 0 & \cdots & 1-2\lambda_1 & \lambda_1\\ 0 & 0 & 0 & 0 & \cdots & \lambda_1 & 1-\lambda_1\end{pmatrix}$
From the matlab docs:
Description: Tridiagonal matrix (sparse matrix)
Syntax:
A = gallery('tridiag',n) returns the sparse tridiagonal matrix of size n-by-n with subdiagonal elements -1, diagonal elements 2, and superdiagonal elements -1. This matrix has eigenvalues 2 + 2cos(kpi/(n+1)), where k = 1:n.
The generated matrix is a symmetric positive definite M-matrix with real nonnegative eigenvalues. This matrix is also the negative of the second difference matrix.
A = gallery('tridiag',c,d,e) returns the tridiagonal matrix with subdiagonal c, diagonal d, and superdiagonal e defined by the vectors c, d, and e. The length of vectors c and e must be length(d)-1.
A = gallery('tridiag',n,c,d,e), where c, d, and e are all scalars, yields the Toeplitz tridiagonal matrix of size n-by-n with subdiagonal elements c, diagonal elements d, and superdiagonal elements e. This matrix has eigenvalues $d + 2 \sqrt{ce} \cos(k\pi/(n+1))$, where $k = 1:n$.
so in your case
A = gallery('tridiag', lambda_1 * ones(n-1), 1 + 2 * lambda_1 * ones(n), lambda_1 * ones(n-1) );B = gallery('tridiag', lambda_1 * ones(n-1), 1 - 2 * lambda_1 * ones(n), lambda_1 * ones(n-1) );Then you would just have to manually set
A(1, 1), A(n, n), B(1, 1), B(n, n).