Loop Construction

Hi all,

I would like to solve a linear system using the Thomas algorithm, as explained in the attachment below. I tried to solve it writing each entry using its definition (3.53). But, if the matrix is larger, it takes too long. Is there any way to use Do loop (or another loop) to solve this problem? I attached my codes.

Thanks..

Something like this?

n = 4;
s = Delete[Table[i, {i, -10, 10}], 11];
{a, b, c, f} = {RandomChoice[s, n], RandomChoice[s, n - 1], 
  RandomChoice[s, n - 1], RandomChoice[s, n]}
A = SparseArray[{Band[{1, 1}] -> a, Band[{2, 1}] -> b, 
    Band[{1, 2}] -> c}, {n, n}];
MatrixForm[A]

(* Calculate coefficients \[Alpha] and \[Beta] *)
alpha = Table[Symbol["\[Alpha]" <> ToString[i]], {i, n}];
beta = Table[Symbol["\[Beta]" <> ToString[i]], {i, 2, n}];
alpha[[1]] = a[[1]];
Do[beta[[i - 1]] = b[[i - 1]]/alpha[[i - 1]];
 alpha[[i]] = a[[i]] - beta[[i - 1]] c[[i - 1]], {i, 2, n}]

L = SparseArray[{Band[{1, 1}] -> 1, Band[{2, 1}] -> beta}, {n, n}];
U = SparseArray[{Band[{1, 1}] -> alpha, Band[{1, 2}] -> c}, {n, n}];

(* Check to see that L.U \[Equal] A *)
L.U // MatrixForm

LinearSolve[U, LinearSolve[L, f]] // MatrixForm
LinearSolve[A, f] // MatrixForm

Awesome!! Thanks..

why it is giving error without this part, I mean why we need it..

 alpha = Table[Symbol["\[Alpha]" <> ToString[i]], {i, n}];
beta = Table[Symbol["\[Beta]" <> ToString[i]], {i, 2, n}];

Those statements create the lists to hold the alpha and beta values. Without those statements those lists don’t exist and you’ll then get errors such as

Set::noval: Symbol alpha in part assignment does not have an immediate value. >>

Part::partd: Part specification alpha[[1]] is longer than depth of object. >>

when you try to make an assignment such as

alpha[[1]] = a[[1]];