Solve tridiagonal matrix system by thomas algorithm

19 views (last 30 days)
hi
im trying to solve tridiagonal matrix system by thomas algorithm. my question is: it's obligatory to define the matrix elements or not ?

Accepted Answer

John D'Errico
John D'Errico on 8 May 2019
Edited: John D'Errico on 8 May 2019
Is it obligatory to create a matrix with those specific elements? Of course not. As long as you know what elements have which value, then nothing is obligatory. You are the one who is writing the code, no?
If your goal is to solve a psecific problem that is not homework, then it is usully simpler to just use sparse matrices and use backslash. The virtue of using sparse matrices is you do not need to write code to solve the problem.
I would also point out that the decomposition function is provided in MATLAB, which allows you to specify a banded matrix. These are things you would need to test and compare the time required. But again, a sparse use of backslash is FAST.
For example, on a 1e6x1e6 tridiagonal problem, the time required for a solve is tiny:
n = 1000000;
A = spdiags(rand(n,3),1:1,n,n) + speye(n)*2;
B = rand(n,1);
timeit(@() A\B)
ans =
0.010033
Why would you want to write a looped code that probably runs more slowly?
Now, maybe your question is if you can apply the Thomas algorithm to a totally general tridiagonal matrix with symbolic coefficients. Well, yes, but why?

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!