Solve tridiagonal matrix system by thomas algorithm

19 次查看(过去 30 天)
hi
im trying to solve tridiagonal matrix system by thomas algorithm. my question is: it's obligatory to define the matrix elements or not ?

采纳的回答

John D'Errico
John D'Errico 2019-5-8
编辑:John D'Errico 2019-5-8
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?

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by