We begin with an easy case one in which the factorization is almost trivial. This case is tridiagonal matrices A - that is A has non-zero entries only on the diagonal the super-diagonal and the sub-diagonal. Such matrices arise frequently in the study of numerical differential equations.
Because of the structure of A we expect L to have non-zero entries only on the diagonal and sub-diagonal, and U to have non-zero entries only on the diagonal and super-diagonal. In fact a little thought will tell you that the diagonal on either L or U could be chosen to be 1's; we will choose U to have this form. Thus
This yields:
The solution is now easy consisting of forward and backward substitution.
This algorithm is called the Thomas algorithm (esp. in engineering circles).
Following is a subroutine to solve a tridiagonal system. NOTE: i have designed this to be fairly flexible regarding starting and ending indices; you need to pass them to the code as I and L. Use it as part of a code to solve linear systems. Test out the code on easy problems (i.e., design problems for which you know the exact solution. For example, test it on the 3X3 system with A= (1 0 1 , 0 1 1, 1 1 2) and b=(2 2 4).)
Now solve the NXN tridiagonal system with -2 on the diagonal, 1 on the sub- and super-diagonals, and a rhs b = (1,1,1,...,1,1). Send the code with this problem to the grader.