# Loop Construction

**URL:** <https://community.wolfram.com/t/loop-construction/4411>\
**Category:** Questions\
**Tags:** mathematica, algebra, numerical-computation\
**Created:** [December 27, 2014, 4:01pm UTC](https://community.wolfram.com/t/loop-construction/4411 "2014-12-27T16:01:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![selahittincinar214](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/selahittincinar214/32/13199_2.png) [@selahittincinar214](https://community.wolfram.com/u/selahittincinar214)\
**Post date:** [December 27, 2014, 4:01pm UTC](https://community.wolfram.com/t/loop-construction/4411/1 "2014-12-27T16:01:34Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![jbaldwin](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jbaldwin](https://community.wolfram.com/u/jbaldwin)\
**Post date:** [December 28, 2014, 8:54am UTC](https://community.wolfram.com/t/loop-construction/4411/2 "2014-12-28T08:54:07Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![selahittincinar214](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/selahittincinar214/32/13199_2.png) [@selahittincinar214](https://community.wolfram.com/u/selahittincinar214)\
**Post date:** [January 9, 2015, 8:45am UTC](https://community.wolfram.com/t/loop-construction/4411/3 "2015-01-09T08:45:39Z")

</div>

Awesome!! Thanks..

---

<div class="post-metadata">

**Author:** ![selahittincinar214](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/selahittincinar214/32/13199_2.png) [@selahittincinar214](https://community.wolfram.com/u/selahittincinar214)\
**Post date:** [January 9, 2015, 9:25am UTC](https://community.wolfram.com/t/loop-construction/4411/4 "2015-01-09T09:25:46Z")

</div>

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}];

```

---

<div class="post-metadata">

**Author:** ![jbaldwin](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jbaldwin](https://community.wolfram.com/u/jbaldwin)\
**Post date:** [January 9, 2015, 3:35pm UTC](https://community.wolfram.com/t/loop-construction/4411/5 "2015-01-09T15:35:05Z")

</div>

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]];

```
